Skip to content

Commit fb72f56

Browse files
committed
Add search function in controller
1 parent 777951e commit fb72f56

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/src/widgets/controller.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ class QuillController extends ChangeNotifier {
299299
notifyListeners();
300300
}
301301

302+
/// Search the whole document for any substring matching the pattern
303+
/// Returns the offsets that matches the pattern
304+
List<int> search(Pattern other) {
305+
return document.search(other);
306+
}
307+
302308
@override
303309
void addListener(VoidCallback listener) {
304310
// By using `_isDisposed`, make sure that `addListener` won't be called on a

lib/src/widgets/toolbar/search_button.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,20 @@ class SearchButton extends StatelessWidget {
4848
Future<void> _onPressedHandler(BuildContext context) async {
4949
await showDialog<String>(
5050
context: context,
51-
builder: (_) => _SearchDialog(dialogTheme: dialogTheme, text: ''),
51+
builder: (_) => _SearchDialog(
52+
controller: controller, dialogTheme: dialogTheme, text: ''),
5253
).then(_searchSubmitted);
5354
}
5455

5556
void _searchSubmitted(String? value) {}
5657
}
5758

5859
class _SearchDialog extends StatefulWidget {
59-
const _SearchDialog({this.dialogTheme, this.text, Key? key})
60+
const _SearchDialog(
61+
{required this.controller, this.dialogTheme, this.text, Key? key})
6062
: super(key: key);
6163

64+
final QuillController controller;
6265
final QuillDialogTheme? dialogTheme;
6366
final String? text;
6467

@@ -90,11 +93,15 @@ class _SearchDialogState extends State<_SearchDialog> {
9093
labelStyle: widget.dialogTheme?.labelTextStyle,
9194
floatingLabelStyle: widget.dialogTheme?.labelTextStyle),
9295
autofocus: true,
96+
onChanged: _textChanged,
9397
controller: _controller,
9498
),
9599
actions: [
96100
TextButton(
97-
onPressed: () {},
101+
onPressed: () {
102+
final offsets = widget.controller.document.search(_text);
103+
debugPrint(offsets.toString());
104+
},
98105
child: Text(
99106
'Ok'.i18n,
100107
style: widget.dialogTheme?.labelTextStyle,
@@ -103,4 +110,10 @@ class _SearchDialogState extends State<_SearchDialog> {
103110
],
104111
);
105112
}
113+
114+
void _textChanged(String value) {
115+
setState(() {
116+
_text = value;
117+
});
118+
}
106119
}

0 commit comments

Comments
 (0)