Jisho-Study-Tool/lib/components/kanji/kanji_search_body/kanji_search_bar.dart

68 lines
1.7 KiB
Dart
Raw Normal View History

2021-03-05 22:08:34 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class KanjiSearchBar extends StatefulWidget {
final Function(String)? onChanged;
2021-07-19 01:47:12 +02:00
2021-12-01 23:09:53 +01:00
const KanjiSearchBar({this.onChanged, Key? key}) : super(key: key);
2021-07-19 01:47:12 +02:00
2021-03-05 22:08:34 +01:00
@override
2021-12-04 05:22:58 +01:00
KanjiSearchBarState createState() => KanjiSearchBarState();
2021-03-05 22:08:34 +01:00
}
enum TextFieldButton { clear, paste }
2021-03-05 22:08:34 +01:00
class KanjiSearchBarState extends State<KanjiSearchBar> {
2021-12-01 23:09:53 +01:00
final TextEditingController textController = TextEditingController();
2021-03-05 22:08:34 +01:00
TextFieldButton button = TextFieldButton.paste;
@override
void initState() {
super.initState();
}
2022-01-19 02:10:05 +01:00
void onChanged() => widget.onChanged?.call(textController.text);
2021-03-05 22:08:34 +01:00
void clearText() {
2021-03-05 22:08:34 +01:00
textController.text = '';
2022-01-19 02:10:05 +01:00
onChanged();
2021-03-05 22:08:34 +01:00
}
2021-12-01 23:09:53 +01:00
Future<void> pasteText() async {
final ClipboardData? clipboardData = await Clipboard.getData('text/plain');
2021-07-26 21:39:17 +02:00
if (clipboardData != null && clipboardData.text != null) {
textController.text = clipboardData.text!;
2022-01-19 02:10:05 +01:00
onChanged();
2021-07-26 21:39:17 +02:00
}
2021-03-05 22:08:34 +01:00
}
@override
Widget build(BuildContext context) {
2021-12-01 23:09:53 +01:00
final IconButton clearButton = IconButton(
icon: const Icon(Icons.clear),
onPressed: () => clearText(),
2021-03-05 22:08:34 +01:00
);
2021-12-01 23:09:53 +01:00
final IconButton pasteButton = IconButton(
icon: const Icon(Icons.content_paste),
onPressed: () => pasteText(),
2021-03-05 22:08:34 +01:00
);
return TextField(
controller: textController,
2022-01-19 02:10:05 +01:00
onChanged: (text) => onChanged(),
2021-08-03 22:02:42 +02:00
onSubmitted: (_) => {},
2021-12-01 23:09:53 +01:00
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search),
2021-07-17 12:19:03 +02:00
hintText: 'Search',
2021-03-05 22:08:34 +01:00
border: OutlineInputBorder(
2021-07-17 12:19:03 +02:00
borderRadius: BorderRadius.circular(10.0),
2021-03-05 22:08:34 +01:00
),
isDense: false,
suffixIcon:
(button == TextFieldButton.clear) ? clearButton : pasteButton,
2021-03-05 22:08:34 +01:00
),
);
}
}