Skip to content

Commit 01553db

Browse files
committed
wip RawAutocomplete: Let caller control options directly
This isn't nearly ready for merge; it's just to demonstrate a direction that might be helpful for zulip/zulip-flutte#226, and perhaps also for: flutter#123377 (comment) The idea (for zulip/zulip-flutte#226) is to move to a new class RawAutocompleteController the state value for the current list of autocomplete options. Then, to let RawAutocomplete's caller pass an instance of that class, giving the caller control over when that state gets updated. For us, that's better than accepting RawAutocomplete's old behavior, where a change in the TextEditingController was the only trigger for an update. (We want to trigger updates when our MentionAutocompleteView changes.) Ideally, we could add this capability to RawAutocomplete without removing any of its current API: maybe we could make the `controller` param optional, and I guess add asserts to try to prevent buggy mixtures of the old and this new opt-in behavior. Anyway, that that was way too tricky for this demo, so here I've just ripped out all the parts where RawAutocomplete did anything with the TextEditingController. I think our zulip/zulip-flutter#226 doesn't create a need to control the "selection" state, but that state is also moved to the RawAutocompleteController class anyway. Why? - Well, with RawAutocomplete being unaware of the text-input value, as mentioned in the previous paragraph, it can't listen for changes in that value, which it was doing before, to null out the selection state when the text changed. So, now we do that. - Also, I think there's a proposal for class like this RawAutocompleteController to contain the selection state: flutter#123377 (comment) That comment would also have it include the highlighted state, I think (which the auther calls the "hovered" state). Since those are closely related to the options-list state, might as well put them there along with it. For this demo, I hacked down the Material `Autocomplete` widget so that zulip-flutter would build, and I haven't touched any of the Flutter framework tests.
1 parent 4be6bf4 commit 01553db

2 files changed

Lines changed: 34 additions & 317 deletions

File tree

‎packages/flutter/lib/src/material/autocomplete.dart‎

Lines changed: 8 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:flutter/scheduler.dart';
65
import 'package:flutter/widgets.dart';
76

8-
import 'ink_well.dart';
9-
import 'material.dart';
107
import 'text_form_field.dart';
11-
import 'theme.dart';
128

139
/// {@macro flutter.widgets.RawAutocomplete.RawAutocomplete}
1410
///
@@ -60,36 +56,25 @@ class Autocomplete<T extends Object> extends StatelessWidget {
6056
/// Creates an instance of [Autocomplete].
6157
const Autocomplete({
6258
super.key,
63-
required this.optionsBuilder,
64-
this.displayStringForOption = RawAutocomplete.defaultStringForOption,
59+
required this.controller,
6560
this.fieldViewBuilder = _defaultFieldViewBuilder,
66-
this.onSelected,
6761
this.optionsMaxHeight = 200.0,
68-
this.optionsViewBuilder,
62+
required this.optionsViewBuilder,
6963
this.optionsViewOpenDirection = OptionsViewOpenDirection.down,
7064
this.initialValue,
7165
});
7266

73-
/// {@macro flutter.widgets.RawAutocomplete.displayStringForOption}
74-
final AutocompleteOptionToString<T> displayStringForOption;
67+
/// The controller.
68+
final RawAutocompleteController<T> controller;
7569

7670
/// {@macro flutter.widgets.RawAutocomplete.fieldViewBuilder}
7771
///
7872
/// If not provided, will build a standard Material-style text field by
7973
/// default.
8074
final AutocompleteFieldViewBuilder fieldViewBuilder;
8175

82-
/// {@macro flutter.widgets.RawAutocomplete.onSelected}
83-
final AutocompleteOnSelected<T>? onSelected;
84-
85-
/// {@macro flutter.widgets.RawAutocomplete.optionsBuilder}
86-
final AutocompleteOptionsBuilder<T> optionsBuilder;
87-
8876
/// {@macro flutter.widgets.RawAutocomplete.optionsViewBuilder}
89-
///
90-
/// If not provided, will build a standard Material-style list of results by
91-
/// default.
92-
final AutocompleteOptionsViewBuilder<T>? optionsViewBuilder;
77+
final AutocompleteOptionsViewBuilder<T> optionsViewBuilder;
9378

9479
/// {@macro flutter.widgets.RawAutocomplete.optionsViewOpenDirection}
9580
final OptionsViewOpenDirection optionsViewOpenDirection;
@@ -105,31 +90,19 @@ class Autocomplete<T extends Object> extends StatelessWidget {
10590
/// {@macro flutter.widgets.RawAutocomplete.initialValue}
10691
final TextEditingValue? initialValue;
10792

108-
static Widget _defaultFieldViewBuilder(BuildContext context, TextEditingController textEditingController, FocusNode focusNode, VoidCallback onFieldSubmitted) {
93+
static Widget _defaultFieldViewBuilder(BuildContext context, FocusNode focusNode) {
10994
return _AutocompleteField(
11095
focusNode: focusNode,
111-
textEditingController: textEditingController,
112-
onFieldSubmitted: onFieldSubmitted,
11396
);
11497
}
11598

11699
@override
117100
Widget build(BuildContext context) {
118101
return RawAutocomplete<T>(
119-
displayStringForOption: displayStringForOption,
102+
controller: controller,
120103
fieldViewBuilder: fieldViewBuilder,
121-
initialValue: initialValue,
122-
optionsBuilder: optionsBuilder,
123104
optionsViewOpenDirection: optionsViewOpenDirection,
124-
optionsViewBuilder: optionsViewBuilder ?? (BuildContext context, AutocompleteOnSelected<T> onSelected, Iterable<T> options) {
125-
return _AutocompleteOptions<T>(
126-
displayStringForOption: displayStringForOption,
127-
onSelected: onSelected,
128-
options: options,
129-
maxOptionsHeight: optionsMaxHeight,
130-
);
131-
},
132-
onSelected: onSelected,
105+
optionsViewBuilder: optionsViewBuilder,
133106
);
134107
}
135108
}
@@ -138,83 +111,14 @@ class Autocomplete<T extends Object> extends StatelessWidget {
138111
class _AutocompleteField extends StatelessWidget {
139112
const _AutocompleteField({
140113
required this.focusNode,
141-
required this.textEditingController,
142-
required this.onFieldSubmitted,
143114
});
144115

145116
final FocusNode focusNode;
146117

147-
final VoidCallback onFieldSubmitted;
148-
149-
final TextEditingController textEditingController;
150-
151118
@override
152119
Widget build(BuildContext context) {
153120
return TextFormField(
154-
controller: textEditingController,
155121
focusNode: focusNode,
156-
onFieldSubmitted: (String value) {
157-
onFieldSubmitted();
158-
},
159-
);
160-
}
161-
}
162-
163-
// The default Material-style Autocomplete options.
164-
class _AutocompleteOptions<T extends Object> extends StatelessWidget {
165-
const _AutocompleteOptions({
166-
super.key,
167-
required this.displayStringForOption,
168-
required this.onSelected,
169-
required this.options,
170-
required this.maxOptionsHeight,
171-
});
172-
173-
final AutocompleteOptionToString<T> displayStringForOption;
174-
175-
final AutocompleteOnSelected<T> onSelected;
176-
177-
final Iterable<T> options;
178-
final double maxOptionsHeight;
179-
180-
@override
181-
Widget build(BuildContext context) {
182-
return Align(
183-
alignment: Alignment.topLeft,
184-
child: Material(
185-
elevation: 4.0,
186-
child: ConstrainedBox(
187-
constraints: BoxConstraints(maxHeight: maxOptionsHeight),
188-
child: ListView.builder(
189-
padding: EdgeInsets.zero,
190-
shrinkWrap: true,
191-
itemCount: options.length,
192-
itemBuilder: (BuildContext context, int index) {
193-
final T option = options.elementAt(index);
194-
return InkWell(
195-
onTap: () {
196-
onSelected(option);
197-
},
198-
child: Builder(
199-
builder: (BuildContext context) {
200-
final bool highlight = AutocompleteHighlightedOption.of(context) == index;
201-
if (highlight) {
202-
SchedulerBinding.instance.addPostFrameCallback((Duration timeStamp) {
203-
Scrollable.ensureVisible(context, alignment: 0.5);
204-
});
205-
}
206-
return Container(
207-
color: highlight ? Theme.of(context).focusColor : null,
208-
padding: const EdgeInsets.all(16.0),
209-
child: Text(displayStringForOption(option)),
210-
);
211-
}
212-
),
213-
);
214-
},
215-
),
216-
),
217-
),
218122
);
219123
}
220124
}

0 commit comments

Comments
 (0)