forked from eatvio/eatvio_app
Autocomplete class
This commit is contained in:
82
lib/widgets/custom_autocomplete.dart
Normal file
82
lib/widgets/custom_autocomplete.dart
Normal file
@@ -0,0 +1,82 @@
|
||||
import 'package:eatvio_app/core/app_export.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class CustomTextFormField extends StatelessWidget {
|
||||
// ...
|
||||
|
||||
final AutocompleteOptions<String> autocompleteOptions;
|
||||
|
||||
const CustomTextFormField({
|
||||
// ... (остальные параметры)
|
||||
required this.autocompleteOptions,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return alignment != null
|
||||
? Align(
|
||||
alignment: alignment ?? Alignment.center,
|
||||
child: autocompleteFieldWidget,
|
||||
)
|
||||
: autocompleteFieldWidget;
|
||||
}
|
||||
|
||||
Widget get autocompleteFieldWidget => SizedBox(
|
||||
width: width ?? double.maxFinite,
|
||||
child: Autocomplete<String>(
|
||||
optionsBuilder: (TextEditingValue textEditingValue) {
|
||||
if (textEditingValue.text == '') {
|
||||
return const Iterable<String>.empty();
|
||||
}
|
||||
return autocompleteOptions.options.where((String option) {
|
||||
return option
|
||||
.toLowerCase()
|
||||
.contains(textEditingValue.text.toLowerCase());
|
||||
});
|
||||
},
|
||||
onSelected: (String value) {
|
||||
controller?.text = value;
|
||||
onChanged?.call(value);
|
||||
},
|
||||
fieldViewBuilder: (BuildContext context,
|
||||
TextEditingController textEditingController,
|
||||
FocusNode focusNode,
|
||||
VoidCallback onFieldSubmitted) {
|
||||
return TextFormField(
|
||||
controller: textEditingController,
|
||||
focusNode: focusNode,
|
||||
autofocus: autofocus!,
|
||||
style: textStyle ?? theme.textTheme.titleMedium,
|
||||
obscureText: obscureText!,
|
||||
textInputAction: textInputAction,
|
||||
maxLines: maxLines ?? 1,
|
||||
decoration: decoration,
|
||||
validator: validator,
|
||||
keyboardType: keyboardType,
|
||||
onSaved: onSaved,
|
||||
onChanged: onChanged,
|
||||
inputFormatters: inputFormatters,
|
||||
initialValue: initialValue,
|
||||
onFieldSubmitted: (_) => onFieldSubmitted(),
|
||||
);
|
||||
},
|
||||
optionsViewBuilder: (BuildContext context,
|
||||
AutocompleteOnSelected<String> onSelected,
|
||||
Iterable<String> options) {
|
||||
return Material(
|
||||
elevation: 4.0,
|
||||
child: ListView(
|
||||
children: options.map((String option) {
|
||||
return ListTile(
|
||||
title: Text(option),
|
||||
onTap: () {
|
||||
onSelected(option);
|
||||
},
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user