diff --git a/lib/src/widgets/controller.dart b/lib/src/widgets/controller.dart index db6637a5..0eed7fc7 100644 --- a/lib/src/widgets/controller.dart +++ b/lib/src/widgets/controller.dart @@ -299,6 +299,12 @@ class QuillController extends ChangeNotifier { notifyListeners(); } + /// Search the whole document for any substring matching the pattern + /// Returns the offsets that matches the pattern + List search(Pattern other) { + return document.search(other); + } + @override void addListener(VoidCallback listener) { // By using `_isDisposed`, make sure that `addListener` won't be called on a diff --git a/lib/src/widgets/toolbar/search_button.dart b/lib/src/widgets/toolbar/search_button.dart index f0b079a2..79153fda 100644 --- a/lib/src/widgets/toolbar/search_button.dart +++ b/lib/src/widgets/toolbar/search_button.dart @@ -48,7 +48,8 @@ class SearchButton extends StatelessWidget { Future _onPressedHandler(BuildContext context) async { await showDialog( context: context, - builder: (_) => _SearchDialog(dialogTheme: dialogTheme, text: ''), + builder: (_) => _SearchDialog( + controller: controller, dialogTheme: dialogTheme, text: ''), ).then(_searchSubmitted); } @@ -56,9 +57,11 @@ class SearchButton extends StatelessWidget { } class _SearchDialog extends StatefulWidget { - const _SearchDialog({this.dialogTheme, this.text, Key? key}) + const _SearchDialog( + {required this.controller, this.dialogTheme, this.text, Key? key}) : super(key: key); + final QuillController controller; final QuillDialogTheme? dialogTheme; final String? text; @@ -90,11 +93,15 @@ class _SearchDialogState extends State<_SearchDialog> { labelStyle: widget.dialogTheme?.labelTextStyle, floatingLabelStyle: widget.dialogTheme?.labelTextStyle), autofocus: true, + onChanged: _textChanged, controller: _controller, ), actions: [ TextButton( - onPressed: () {}, + onPressed: () { + final offsets = widget.controller.document.search(_text); + debugPrint(offsets.toString()); + }, child: Text( 'Ok'.i18n, style: widget.dialogTheme?.labelTextStyle, @@ -103,4 +110,10 @@ class _SearchDialogState extends State<_SearchDialog> { ], ); } + + void _textChanged(String value) { + setState(() { + _text = value; + }); + } }