Skip to content

Commit 838565e

Browse files
committed
Better selection handling in search dialog
1 parent 3ee3750 commit 838565e

2 files changed

Lines changed: 88 additions & 48 deletions

File tree

lib/component/statics.dart

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

pubspec.lock

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -298,18 +298,18 @@ packages:
298298
description:
299299
path: "."
300300
ref: HEAD
301-
resolved-ref: "15be0548d9235278be072c4b8743979bee4576cd"
301+
resolved-ref: "3f3847d4e14f114b20385149f6a17f3ab7c2593a"
302302
url: "https://github.com/aluedtke7/hid_listener_fork.git"
303303
source: git
304304
version: "2.0.1"
305305
hooks:
306306
dependency: transitive
307307
description:
308308
name: hooks
309-
sha256: e79ed1e8e1929bc6ecb6ec85f0cb519c887aa5b423705ded0d0f2d9226def388
309+
sha256: "025f060e86d2d4c3c47b56e33caf7f93bf9283340f26d23424ebcfccf34f621e"
310310
url: "https://pub.dev"
311311
source: hosted
312-
version: "1.0.2"
312+
version: "1.0.3"
313313
html:
314314
dependency: transitive
315315
description:
@@ -678,6 +678,14 @@ packages:
678678
url: "https://pub.dev"
679679
source: hosted
680680
version: "2.2.0"
681+
record_use:
682+
dependency: transitive
683+
description:
684+
name: record_use
685+
sha256: "2551bd8eecfe95d14ae75f6021ad0248be5c27f138c2ec12fcb52b500b3ba1ed"
686+
url: "https://pub.dev"
687+
source: hosted
688+
version: "0.6.0"
681689
riverpod:
682690
dependency: transitive
683691
description:
@@ -863,10 +871,10 @@ packages:
863871
dependency: transitive
864872
description:
865873
name: sqflite
866-
sha256: e2297b1da52f127bc7a3da11439985d9b536f75070f3325e62ada69a5c585d03
874+
sha256: "564cfed0746fe53140c23b70b308e045c3b31f17778f2f326ccb7d804ea0250a"
867875
url: "https://pub.dev"
868876
source: hosted
869-
version: "2.4.2"
877+
version: "2.4.2+1"
870878
sqflite_android:
871879
dependency: transitive
872880
description:
@@ -879,10 +887,10 @@ packages:
879887
dependency: transitive
880888
description:
881889
name: sqflite_common
882-
sha256: "6ef422a4525ecc601db6c0a2233ff448c731307906e92cabc9ba292afaae16a6"
890+
sha256: f8a08a13fb8f0f8c590df89d745000bed44a673ed94bac846739e1a016875c21
883891
url: "https://pub.dev"
884892
source: hosted
885-
version: "2.5.6"
893+
version: "2.5.7"
886894
sqflite_darwin:
887895
dependency: transitive
888896
description:
@@ -935,10 +943,10 @@ packages:
935943
dependency: transitive
936944
description:
937945
name: synchronized
938-
sha256: c254ade258ec8282947a0acbbc90b9575b4f19673533ee46f2f6e9b3aeefd7c0
946+
sha256: "63896c27e81b28f8cb4e69ead0d3e8f03f1d1e5fc531a3e579cabed6a2c7c9e5"
939947
url: "https://pub.dev"
940948
source: hosted
941-
version: "3.4.0"
949+
version: "3.4.0+1"
942950
term_glyph:
943951
dependency: transitive
944952
description:
@@ -1071,10 +1079,10 @@ packages:
10711079
dependency: transitive
10721080
description:
10731081
name: url_launcher_web
1074-
sha256: d0412fcf4c6b31ecfdb7762359b7206ffba3bbffd396c6d9f9c4616ece476c1f
1082+
sha256: "85c81589622fbc87c1c683aaea164d3604a7777495a79d91e39ffcdec39ddb34"
10751083
url: "https://pub.dev"
10761084
source: hosted
1077-
version: "2.4.2"
1085+
version: "2.4.3"
10781086
url_launcher_windows:
10791087
dependency: transitive
10801088
description:
@@ -1103,10 +1111,10 @@ packages:
11031111
dependency: transitive
11041112
description:
11051113
name: vm_service
1106-
sha256: "046d3928e16fa4dc46e8350415661755ab759d9fc97fc21b5ab295f71e4f0499"
1114+
sha256: "0016aef94fc66495ac78af5859181e3f3bf2026bd8eecc72b9565601e19ab360"
11071115
url: "https://pub.dev"
11081116
source: hosted
1109-
version: "15.1.0"
1117+
version: "15.2.0"
11101118
watcher:
11111119
dependency: transitive
11121120
description:
@@ -1188,5 +1196,5 @@ packages:
11881196
source: hosted
11891197
version: "3.1.3"
11901198
sdks:
1191-
dart: ">=3.10.3 <4.0.0"
1199+
dart: ">=3.11.0 <4.0.0"
11921200
flutter: ">=3.38.4"

0 commit comments

Comments
 (0)