I can't change the controller. Clicking on an element should trigger the onTapItem function, but it doesn't.
NastyaZiss opened this issue · 0 comments
import 'package:flutter/material.dart';
import 'package:flutter_async_autocomplete/flutter_async_autocomplete.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@OverRide
State createState() => _MyAppState();
}
class _MyAppState extends State {
var autoController = TextEditingController();
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: SafeArea(
child: Scaffold(
appBar: AppBar(title: Text('Example')),
body: Container(
padding: EdgeInsets.all(10),
alignment: Alignment.center,
child: AsyncAutocomplete(
controller: autoController,
onTapItem: (Country country) {
autoController.text = country.name;
print("selected Country ${country.name}");
},
suggestionBuilder: (data) => ListTile(
title: Text(data.name),
subtitle: Text(data.code),
),
asyncSuggestions: (searchValue) => getCountry(searchValue),
scrollBarController: ScrollController(),
),
),
),
),
);
}
Future<List> getCountry(String search) async {
List countryList = [
Country(name: 'Afghanistan', code: 'AF'),
Country(name: 'Åland Islands', code: 'AX'),
];
await Future.delayed(const Duration(microseconds: 500));
return countryList
.where((element) =>
element.name.toLowerCase().startsWith(search.toLowerCase()))
.toList();
}
onChange(value) {
// (value) {
setState(() {
autoController.text = value.name;
});
print('onChanged value: ${value.name}');
}
}
class Country {
String name;
String code;
Country({required this.code, required this.name});
}