2021-09-07 00:05:33 +02:00
|
|
|
import 'package:animated_size_and_fade/animated_size_and_fade.dart';
|
2021-12-01 23:09:53 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2021-09-07 00:05:33 +02:00
|
|
|
|
2022-01-19 02:10:05 +01:00
|
|
|
import '../../services/kanji_suggestions.dart';
|
2021-12-01 23:09:53 +01:00
|
|
|
import 'kanji_search_body/kanji_grid.dart';
|
|
|
|
import 'kanji_search_body/kanji_search_bar.dart';
|
|
|
|
import 'kanji_search_body/kanji_search_options_bar.dart';
|
2021-09-07 00:05:33 +02:00
|
|
|
|
|
|
|
class KanjiSearchBody extends StatefulWidget {
|
2021-12-01 23:09:53 +01:00
|
|
|
const KanjiSearchBody({Key? key}) : super(key: key);
|
2021-09-07 00:05:33 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
_KanjiSearchBodyState createState() => _KanjiSearchBodyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _KanjiSearchBodyState extends State<KanjiSearchBody>
|
|
|
|
with SingleTickerProviderStateMixin {
|
|
|
|
late final AnimationController _controller;
|
|
|
|
late final Animation _searchbarMovementAnimation;
|
|
|
|
final FocusNode focus = FocusNode();
|
|
|
|
final GlobalKey<KanjiSearchBarState> _kanjiSearchBarState =
|
|
|
|
GlobalKey<KanjiSearchBarState>();
|
|
|
|
List<String> suggestions = [];
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
_controller = AnimationController(
|
|
|
|
vsync: this,
|
2021-12-01 23:09:53 +01:00
|
|
|
duration: const Duration(milliseconds: 200),
|
2021-09-07 00:05:33 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
_searchbarMovementAnimation = AlignmentTween(
|
|
|
|
begin: Alignment.center,
|
|
|
|
end: Alignment.topCenter,
|
2021-12-01 23:09:53 +01:00
|
|
|
).animate(
|
|
|
|
CurvedAnimation(
|
|
|
|
parent: _controller,
|
|
|
|
curve: Curves.easeInOut,
|
|
|
|
),
|
|
|
|
);
|
2021-09-07 00:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_controller.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return WillPopScope(
|
|
|
|
onWillPop: () async {
|
|
|
|
if (_controller.value == 1) {
|
|
|
|
focus.unfocus();
|
|
|
|
_kanjiSearchBarState.currentState!.clearText();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2022-01-19 02:10:05 +01:00
|
|
|
child: InkWell(
|
2021-09-07 00:05:33 +02:00
|
|
|
onTap: () => FocusScope.of(context).unfocus(),
|
|
|
|
child: Container(
|
2021-12-01 23:09:53 +01:00
|
|
|
decoration: const BoxDecoration(),
|
2021-09-07 00:05:33 +02:00
|
|
|
alignment: Alignment.center,
|
2021-12-01 23:09:53 +01:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 20),
|
2021-09-07 00:05:33 +02:00
|
|
|
child: AnimatedBuilder(
|
|
|
|
animation: _searchbarMovementAnimation,
|
2021-12-01 23:09:53 +01:00
|
|
|
builder: (context, _) {
|
2021-09-07 00:05:33 +02:00
|
|
|
return Container(
|
|
|
|
alignment: _searchbarMovementAnimation.value,
|
2021-12-01 23:09:53 +01:00
|
|
|
padding: const EdgeInsets.symmetric(vertical: 10.0),
|
2021-09-07 00:05:33 +02:00
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
Focus(
|
|
|
|
focusNode: focus,
|
|
|
|
onFocusChange: (hasFocus) {
|
|
|
|
if (hasFocus)
|
|
|
|
_controller.forward();
|
|
|
|
else
|
|
|
|
_controller.reverse();
|
|
|
|
},
|
|
|
|
child: KanjiSearchBar(
|
|
|
|
key: _kanjiSearchBarState,
|
|
|
|
onChanged: (text) => setState(() {
|
2021-12-01 23:09:53 +01:00
|
|
|
suggestions = kanjiSuggestions(text);
|
2021-09-07 00:05:33 +02:00
|
|
|
}),
|
|
|
|
),
|
|
|
|
),
|
2022-01-19 02:10:05 +01:00
|
|
|
const SizedBox(height: 20),
|
2021-09-07 00:05:33 +02:00
|
|
|
AnimatedSizeAndFade(
|
|
|
|
fadeDuration: const Duration(milliseconds: 200),
|
|
|
|
sizeDuration: const Duration(milliseconds: 300),
|
2022-01-23 21:00:45 +01:00
|
|
|
child: (_controller.value == 1 && suggestions.isNotEmpty)
|
2021-12-01 23:09:53 +01:00
|
|
|
? KanjiGrid(suggestions: suggestions)
|
2022-01-23 21:00:45 +01:00
|
|
|
: (_controller.value == 1)
|
|
|
|
? const Text(
|
|
|
|
'Type a kanji to start searching',
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16,
|
|
|
|
color: Colors.grey,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: const KanjiSearchOptionsBar(),
|
2021-09-07 00:05:33 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|