@@ -117,41 +117,10 @@ class Statics {
117117 }
118118
119119 static Future <String ?> showSearchDialog (BuildContext context, String title, String initValue) async {
120- final controller = TextEditingController ();
121- controller.text = initValue;
122-
123120 return showDialog <String ?>(
124- context: context,
125- builder: (ctx) {
126- var textFormField = TextFormField (
127- controller: controller,
128- autofocus: true ,
129- decoration: InputDecoration (
130- labelText: i18n (context).com_search,
131- suffixIcon: IconButton (
132- onPressed: () => controller.clear (),
133- icon: const Icon (Icons .clear),
134- ),
135- ),
136- keyboardType: TextInputType .text,
137- onEditingComplete: () => Navigator .of (ctx).pop (controller.text),
138- );
139-
140- return AlertDialog (
141- title: Text (title),
142- content: textFormField,
143- actions: < Widget > [
144- ElevatedButton (
145- onPressed: () => Navigator .of (ctx).pop (null ),
146- child: Text (i18n (context).com_cancel),
147- ),
148- ElevatedButton (
149- onPressed: () => Navigator .of (ctx).pop (controller.text),
150- child: Text (i18n (context).com_ok),
151- ),
152- ],
153- );
154- });
121+ context: context,
122+ builder: (ctx) => _SearchDialog (title: title, initValue: initValue),
123+ );
155124 }
156125
157126 static Future <String ?> showGeniusTokenDialog (BuildContext context, String title, String info, String initVal) async {
@@ -245,3 +214,66 @@ class Statics {
245214 static Color shadeColor (Color color, double factor) => Color .fromRGBO (
246215 shadeValue (color.r.toInt (), factor), shadeValue (color.g.toInt (), factor), shadeValue (color.b.toInt (), factor), 1 );
247216}
217+
218+ class _SearchDialog extends StatefulWidget {
219+ const _SearchDialog ({required this .title, required this .initValue});
220+
221+ final String title;
222+ final String initValue;
223+
224+ @override
225+ State <_SearchDialog > createState () => _SearchDialogState ();
226+ }
227+
228+ class _SearchDialogState extends State <_SearchDialog > {
229+ late final TextEditingController _controller;
230+ late final FocusNode _focusNode;
231+
232+ @override
233+ void initState () {
234+ super .initState ();
235+ _controller = TextEditingController (text: widget.initValue);
236+ _controller.selection = TextSelection (baseOffset: 0 , extentOffset: widget.initValue.length);
237+ _focusNode = FocusNode ();
238+ WidgetsBinding .instance.addPostFrameCallback ((_) {
239+ if (mounted) _focusNode.requestFocus ();
240+ });
241+ }
242+
243+ @override
244+ void dispose () {
245+ _controller.dispose ();
246+ _focusNode.dispose ();
247+ super .dispose ();
248+ }
249+
250+ @override
251+ Widget build (BuildContext context) {
252+ return AlertDialog (
253+ title: Text (widget.title),
254+ content: TextFormField (
255+ controller: _controller,
256+ focusNode: _focusNode,
257+ decoration: InputDecoration (
258+ labelText: i18n (context).com_search,
259+ suffixIcon: IconButton (
260+ onPressed: () => _controller.clear (),
261+ icon: const Icon (Icons .clear),
262+ ),
263+ ),
264+ keyboardType: TextInputType .text,
265+ onEditingComplete: () => Navigator .of (context).pop (_controller.text),
266+ ),
267+ actions: < Widget > [
268+ ElevatedButton (
269+ onPressed: () => Navigator .of (context).pop (null ),
270+ child: Text (i18n (context).com_cancel),
271+ ),
272+ ElevatedButton (
273+ onPressed: () => Navigator .of (context).pop (_controller.text),
274+ child: Text (i18n (context).com_ok),
275+ ),
276+ ],
277+ );
278+ }
279+ }
0 commit comments