forked from openfoodfacts/smooth-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: openfoodfacts#2380 - autocompletion for emb codes, labels, cate…
…gories and countries (openfoodfacts#2506) New file: * `autocomplete.dart`: The default Material-style Autocomplete options. Impacted files: * `simple_input_page.dart`: now using an autocomplete widget for whoever supports it. * `simple_input_page_helpers.dart`: new method in order to get the `TagType` for autocompletion.
- Loading branch information
1 parent
ab4624b
commit a1048bb
Showing
3 changed files
with
151 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/scheduler.dart'; | ||
|
||
/// The default Material-style Autocomplete options. | ||
/// | ||
/// Was copied from material/autocomplete.dart as they were not kind enough | ||
/// to make it public. | ||
/// Inspiration was found in https://stackoverflow.com/questions/66935362 | ||
class AutocompleteOptions<T extends Object> extends StatelessWidget { | ||
const AutocompleteOptions({ | ||
Key? key, | ||
required this.displayStringForOption, | ||
required this.onSelected, | ||
required this.options, | ||
required this.maxOptionsHeight, | ||
}) : super(key: key); | ||
|
||
final AutocompleteOptionToString<T> displayStringForOption; | ||
|
||
final AutocompleteOnSelected<T> onSelected; | ||
|
||
final Iterable<T> options; | ||
final double maxOptionsHeight; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Align( | ||
alignment: Alignment.topLeft, | ||
child: Material( | ||
elevation: 4.0, | ||
child: ConstrainedBox( | ||
constraints: BoxConstraints(maxHeight: maxOptionsHeight), | ||
child: ListView.builder( | ||
padding: EdgeInsets.zero, | ||
shrinkWrap: true, | ||
itemCount: options.length, | ||
itemBuilder: (BuildContext context, int index) { | ||
final T option = options.elementAt(index); | ||
return InkWell( | ||
onTap: () { | ||
onSelected(option); | ||
}, | ||
child: Builder(builder: (BuildContext context) { | ||
final bool highlight = | ||
AutocompleteHighlightedOption.of(context) == index; | ||
if (highlight) { | ||
SchedulerBinding.instance | ||
.addPostFrameCallback((Duration timeStamp) { | ||
Scrollable.ensureVisible(context, alignment: 0.5); | ||
}); | ||
} | ||
return Container( | ||
color: highlight ? Theme.of(context).focusColor : null, | ||
padding: const EdgeInsets.all(16.0), | ||
child: Text(displayStringForOption(option)), | ||
); | ||
}), | ||
); | ||
}, | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters