75 lines
2.2 KiB
Dart
75 lines
2.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class AutocompleteInput extends StatelessWidget {
|
|
final List<String> options;
|
|
final String hintText;
|
|
final String? errorText;
|
|
final String? initialValue;
|
|
final ValueChanged<String>? onChanged;
|
|
|
|
const AutocompleteInput({
|
|
super.key,
|
|
required this.options,
|
|
required this.hintText,
|
|
required this.errorText,
|
|
required this.initialValue,
|
|
required this.onChanged,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return RawAutocomplete<String>(
|
|
initialValue: TextEditingValue(text: initialValue ?? ''),
|
|
optionsBuilder: (TextEditingValue textEditingValue) => options.where((String option) =>option.contains(textEditingValue.text.toLowerCase())),
|
|
fieldViewBuilder: (
|
|
BuildContext context,
|
|
TextEditingController textEditingController,
|
|
FocusNode focusNode,
|
|
VoidCallback onFieldSubmitted,
|
|
) {
|
|
return TextFormField(
|
|
controller: textEditingController,
|
|
focusNode: focusNode,
|
|
decoration: InputDecoration(
|
|
hintText: hintText,
|
|
errorText: errorText
|
|
),
|
|
onFieldSubmitted: (String value) {
|
|
onFieldSubmitted();
|
|
},
|
|
onChanged: onChanged,
|
|
);
|
|
},
|
|
optionsViewBuilder: (
|
|
BuildContext context,
|
|
AutocompleteOnSelected<String> onSelected,
|
|
Iterable<String> options,
|
|
) {
|
|
return Align(
|
|
alignment: Alignment.topLeft,
|
|
child: Material(
|
|
elevation: 4.0,
|
|
child: SizedBox(
|
|
height: 200.0,
|
|
child: ListView.builder(
|
|
padding: const EdgeInsets.all(8.0),
|
|
itemCount: options.length,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
final String option = options.elementAt(index);
|
|
return GestureDetector(
|
|
onTap: () {
|
|
onSelected(option);
|
|
},
|
|
child: ListTile(
|
|
title: Text(option),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
} |