2021-03-05 22:08:34 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
2022-01-23 23:56:26 +01:00
|
|
|
import '../../../settings.dart';
|
|
|
|
|
2021-03-05 22:08:34 +01:00
|
|
|
class KanjiSearchBar extends StatefulWidget {
|
2021-09-07 00:05:33 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-07 00:05:33 +02:00
|
|
|
enum TextFieldButton { clear, paste }
|
2021-03-05 22:08:34 +01:00
|
|
|
|
2021-09-07 00:05:33 +02: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
|
|
|
|
2021-09-07 00:05:33 +02: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),
|
2021-09-07 00:05:33 +02:00
|
|
|
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),
|
2021-09-07 00:05:33 +02:00
|
|
|
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: (_) => {},
|
2022-01-23 23:56:26 +01:00
|
|
|
style: japaneseFont.textStyle,
|
2021-12-01 23:09:53 +01:00
|
|
|
decoration: InputDecoration(
|
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,
|
2021-09-07 00:05:33 +02:00
|
|
|
suffixIcon:
|
|
|
|
(button == TextFieldButton.clear) ? clearButton : pasteButton,
|
2021-03-05 22:08:34 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-09-07 00:05:33 +02:00
|
|
|
}
|