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

This extracts the sample code out from the API doc comments, and places them in separate files on disk, allowing running of the examples locally, testing them, and building of slightly larger examples.
107 lines
3.2 KiB
Dart
107 lines
3.2 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.
|
|
|
|
// Template: dev/snippets/config/templates/freeform.tmpl
|
|
//
|
|
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
|
|
// of samples, and may be ignored if you are just exploring the sample.
|
|
|
|
// Flutter code sample for Autocomplete
|
|
//
|
|
//***************************************************************************
|
|
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
|
|
|
// This example shows how to create an Autocomplete widget with a custom type.
|
|
// Try searching with text from the name or email field.
|
|
|
|
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
|
//***************************************************************************
|
|
|
|
//*************************************************************************
|
|
//* ▼▼▼▼▼▼▼▼ code-main ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
|
|
|
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 User'),
|
|
),
|
|
body: const Center(
|
|
child: 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 => hashValues(email, name);
|
|
}
|
|
|
|
class AutocompleteBasicUserExample extends StatelessWidget {
|
|
const AutocompleteBasicUserExample({Key? key}) : super(key: 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) {
|
|
print('You just selected ${_displayStringForOption(selection)}');
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
//* ▲▲▲▲▲▲▲▲ code-main ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
|
//*************************************************************************
|