Implement search button

pull/899/head
X Code 3 years ago
parent fb72f56e40
commit dd5794f781
  1. 111
      lib/src/widgets/toolbar/search_button.dart

@ -72,48 +72,109 @@ class _SearchDialog extends StatefulWidget {
class _SearchDialogState extends State<_SearchDialog> { class _SearchDialogState extends State<_SearchDialog> {
late String _text; late String _text;
late TextEditingController _controller; late TextEditingController _controller;
late List<int>? _offsets;
late int _index;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_text = widget.text ?? ''; _text = widget.text ?? '';
_offsets = null;
_index = 0;
_controller = TextEditingController(text: _text); _controller = TextEditingController(text: _text);
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return AlertDialog( return StatefulBuilder(builder: (context, setState) {
backgroundColor: widget.dialogTheme?.dialogBackgroundColor, var label = '';
content: TextField( if (_offsets != null) {
keyboardType: TextInputType.multiline, label = '${_offsets!.length} ${'matches'.i18n}';
maxLines: null, if (_offsets!.isNotEmpty) {
style: widget.dialogTheme?.inputTextStyle, label += ', ${'showing match'.i18n} ${_index + 1}';
decoration: InputDecoration( }
labelText: 'Search'.i18n, }
labelStyle: widget.dialogTheme?.labelTextStyle, return AlertDialog(
floatingLabelStyle: widget.dialogTheme?.labelTextStyle), backgroundColor: widget.dialogTheme?.dialogBackgroundColor,
autofocus: true, content: Container(
onChanged: _textChanged, height: 100,
controller: _controller, child: Column(
), children: [
actions: [ TextField(
TextButton( keyboardType: TextInputType.multiline,
onPressed: () { style: widget.dialogTheme?.inputTextStyle,
final offsets = widget.controller.document.search(_text); decoration: InputDecoration(
debugPrint(offsets.toString()); labelText: 'Search'.i18n,
}, labelStyle: widget.dialogTheme?.labelTextStyle,
child: Text( floatingLabelStyle: widget.dialogTheme?.labelTextStyle),
'Ok'.i18n, autofocus: true,
style: widget.dialogTheme?.labelTextStyle, onChanged: _textChanged,
controller: _controller,
),
if (_offsets != null)
Padding(
padding: const EdgeInsets.all(8),
child: Text(label, textAlign: TextAlign.left),
),
],
), ),
), ),
], actions: [
); if (_offsets != null && _offsets!.isNotEmpty && _index > 0)
TextButton(
onPressed: () {
setState(() {
_index -= 1;
});
widget.controller.moveCursorToPosition(_offsets![_index]);
},
child: Text(
'Prev'.i18n,
style: widget.dialogTheme?.labelTextStyle,
),
),
if (_offsets != null &&
_offsets!.isNotEmpty &&
_index < _offsets!.length - 1)
TextButton(
onPressed: () {
setState(() {
_index += 1;
});
widget.controller.moveCursorToPosition(_offsets![_index]);
},
child: Text(
'Next'.i18n,
style: widget.dialogTheme?.labelTextStyle,
),
),
if (_offsets == null)
TextButton(
onPressed: () {
setState(() {
_offsets = widget.controller.document.search(_text);
_index = 0;
debugPrint(_offsets.toString());
});
if (_offsets!.isNotEmpty) {
widget.controller.moveCursorToPosition(_offsets![0]);
}
},
child: Text(
'Ok'.i18n,
style: widget.dialogTheme?.labelTextStyle,
),
),
],
);
});
} }
void _textChanged(String value) { void _textChanged(String value) {
setState(() { setState(() {
_text = value; _text = value;
_offsets = null;
_index = 0;
}); });
} }
} }

Loading…
Cancel
Save