1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6
7/// Flutter code sample for [DeletableChipAttributes.onDeleted].
8
9void main() => runApp(const OnDeletedExampleApp());
10
11class OnDeletedExampleApp extends StatelessWidget {
12 const OnDeletedExampleApp({super.key});
13
14 @override
15 Widget build(BuildContext context) {
16 return MaterialApp(
17 home: Scaffold(
18 appBar: AppBar(title: const Text('DeletableChipAttributes.onDeleted Sample')),
19 body: const Center(child: OnDeletedExample()),
20 ),
21 );
22 }
23}
24
25class Actor {
26 const Actor(this.name, this.initials);
27 final String name;
28 final String initials;
29}
30
31class CastList extends StatefulWidget {
32 const CastList({super.key});
33
34 @override
35 State createState() => CastListState();
36}
37
38class CastListState extends State<CastList> {
39 final List<Actor> _cast = <Actor>[
40 const Actor('Aaron Burr', 'AB'),
41 const Actor('Alexander Hamilton', 'AH'),
42 const Actor('Eliza Hamilton', 'EH'),
43 const Actor('James Madison', 'JM'),
44 ];
45
46 Iterable<Widget> get actorWidgets {
47 return _cast.map((Actor actor) {
48 return Padding(
49 padding: const EdgeInsets.all(4.0),
50 child: Chip(
51 avatar: CircleAvatar(child: Text(actor.initials)),
52 label: Text(actor.name),
53 onDeleted: () {
54 setState(() {
55 _cast.removeWhere((Actor entry) {
56 return entry.name == actor.name;
57 });
58 });
59 },
60 ),
61 );
62 });
63 }
64
65 @override
66 Widget build(BuildContext context) {
67 return Wrap(children: actorWidgets.toList());
68 }
69}
70
71class OnDeletedExample extends StatefulWidget {
72 const OnDeletedExample({super.key});
73
74 @override
75 State<OnDeletedExample> createState() => _OnDeletedExampleState();
76}
77
78class _OnDeletedExampleState extends State<OnDeletedExample> {
79 @override
80 Widget build(BuildContext context) {
81 return const CastList();
82 }
83}
84

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com