Add search function in controller

pull/899/head
X Code 3 years ago
parent 777951e012
commit fb72f56e40
  1. 6
      lib/src/widgets/controller.dart
  2. 19
      lib/src/widgets/toolbar/search_button.dart

@ -299,6 +299,12 @@ class QuillController extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
/// Search the whole document for any substring matching the pattern
/// Returns the offsets that matches the pattern
List<int> search(Pattern other) {
return document.search(other);
}
@override @override
void addListener(VoidCallback listener) { void addListener(VoidCallback listener) {
// By using `_isDisposed`, make sure that `addListener` won't be called on a // By using `_isDisposed`, make sure that `addListener` won't be called on a

@ -48,7 +48,8 @@ class SearchButton extends StatelessWidget {
Future<void> _onPressedHandler(BuildContext context) async { Future<void> _onPressedHandler(BuildContext context) async {
await showDialog<String>( await showDialog<String>(
context: context, context: context,
builder: (_) => _SearchDialog(dialogTheme: dialogTheme, text: ''), builder: (_) => _SearchDialog(
controller: controller, dialogTheme: dialogTheme, text: ''),
).then(_searchSubmitted); ).then(_searchSubmitted);
} }
@ -56,9 +57,11 @@ class SearchButton extends StatelessWidget {
} }
class _SearchDialog extends StatefulWidget { 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); : super(key: key);
final QuillController controller;
final QuillDialogTheme? dialogTheme; final QuillDialogTheme? dialogTheme;
final String? text; final String? text;
@ -90,11 +93,15 @@ class _SearchDialogState extends State<_SearchDialog> {
labelStyle: widget.dialogTheme?.labelTextStyle, labelStyle: widget.dialogTheme?.labelTextStyle,
floatingLabelStyle: widget.dialogTheme?.labelTextStyle), floatingLabelStyle: widget.dialogTheme?.labelTextStyle),
autofocus: true, autofocus: true,
onChanged: _textChanged,
controller: _controller, controller: _controller,
), ),
actions: [ actions: [
TextButton( TextButton(
onPressed: () {}, onPressed: () {
final offsets = widget.controller.document.search(_text);
debugPrint(offsets.toString());
},
child: Text( child: Text(
'Ok'.i18n, 'Ok'.i18n,
style: widget.dialogTheme?.labelTextStyle, style: widget.dialogTheme?.labelTextStyle,
@ -103,4 +110,10 @@ class _SearchDialogState extends State<_SearchDialog> {
], ],
); );
} }
void _textChanged(String value) {
setState(() {
_text = value;
});
}
} }

Loading…
Cancel
Save