Autocomplete
sample.packages.flutter.lib.src.material.autocomplete.16.
This example shows how to create a very basic Autocomplete widget using the default UI.
import 'package:flutter/material.dart';
void main() => runApp(const AutocompleteExampleApp());
class AutocompleteExampleApp extends StatelessWidget {
const AutocompleteExampleApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Autocomplete Basic'),
),
body: const Center(
child: AutocompleteBasicExample(),
),
),
);
}
}
class AutocompleteBasicExample extends StatelessWidget {
const AutocompleteBasicExample({Key? key}) : super(key: key);
static const List _kOptions = [
'aardvark',
'bobcat',
'chameleon',
];
@override
Widget build(BuildContext context) {
return Autocomplete(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text == '') {
return const Iterable.empty();
}
return _kOptions.where((String option) {
return option.contains(textEditingValue.text.toLowerCase());
});
},
onSelected: (String selection) {
debugPrint('You just selected $selection');
},
);
}
}
SHARE: