mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

This example was incorrectly throwing away results from a query when multiple queries were pending at once. Thanks to @sun-jiao in https://github.com/flutter/flutter/pull/127019#issuecomment-1552347037 for pointing this out. I also added a quick `Text` widget explaining what to do to use the examples. Since there are only three small possible `options`, it's easy to type into the field and not get any results and wonder what's wrong.
91 lines
2.4 KiB
Dart
91 lines
2.4 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// Flutter code sample for [Autocomplete].
|
|
|
|
void main() => runApp(const AutocompleteExampleApp());
|
|
|
|
class AutocompleteExampleApp extends StatelessWidget {
|
|
const AutocompleteExampleApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
home: Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Autocomplete Basic User'),
|
|
),
|
|
body: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Text('Type below to autocomplete the following possible results: ${AutocompleteBasicUserExample._userOptions}.'),
|
|
const AutocompleteBasicUserExample(),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
@immutable
|
|
class User {
|
|
const User({
|
|
required this.email,
|
|
required this.name,
|
|
});
|
|
|
|
final String email;
|
|
final String name;
|
|
|
|
@override
|
|
String toString() {
|
|
return '$name, $email';
|
|
}
|
|
|
|
@override
|
|
bool operator ==(Object other) {
|
|
if (other.runtimeType != runtimeType) {
|
|
return false;
|
|
}
|
|
return other is User && other.name == name && other.email == email;
|
|
}
|
|
|
|
@override
|
|
int get hashCode => Object.hash(email, name);
|
|
}
|
|
|
|
class AutocompleteBasicUserExample extends StatelessWidget {
|
|
const AutocompleteBasicUserExample({super.key});
|
|
|
|
static const List<User> _userOptions = <User>[
|
|
User(name: 'Alice', email: 'alice@example.com'),
|
|
User(name: 'Bob', email: 'bob@example.com'),
|
|
User(name: 'Charlie', email: 'charlie123@gmail.com'),
|
|
];
|
|
|
|
static String _displayStringForOption(User option) => option.name;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Autocomplete<User>(
|
|
displayStringForOption: _displayStringForOption,
|
|
optionsBuilder: (TextEditingValue textEditingValue) {
|
|
if (textEditingValue.text == '') {
|
|
return const Iterable<User>.empty();
|
|
}
|
|
return _userOptions.where((User option) {
|
|
return option.toString().contains(textEditingValue.text.toLowerCase());
|
|
});
|
|
},
|
|
onSelected: (User selection) {
|
|
debugPrint('You just selected ${_displayStringForOption(selection)}');
|
|
},
|
|
);
|
|
}
|
|
}
|