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 | |
5 | import 'package:flutter/material.dart'; |
6 | |
7 | /// Flutter code sample for [IndexedStack]. |
8 | |
9 | void main() => runApp(const IndexedStackApp()); |
10 | |
11 | class IndexedStackApp extends StatelessWidget { |
12 | const IndexedStackApp({super.key}); |
13 | |
14 | @override |
15 | Widget build(BuildContext context) { |
16 | return MaterialApp( |
17 | home: Scaffold( |
18 | appBar: AppBar(title: const Text('IndexedStack Sample' )), |
19 | body: const IndexedStackExample(), |
20 | ), |
21 | ); |
22 | } |
23 | } |
24 | |
25 | class IndexedStackExample extends StatefulWidget { |
26 | const IndexedStackExample({super.key}); |
27 | |
28 | @override |
29 | State<IndexedStackExample> createState() => _IndexedStackExampleState(); |
30 | } |
31 | |
32 | class _IndexedStackExampleState extends State<IndexedStackExample> { |
33 | List<String> names = <String>['Dash' , 'John' , 'Mary' ]; |
34 | int index = 0; |
35 | final TextEditingController fieldText = TextEditingController(); |
36 | |
37 | @override |
38 | Widget build(BuildContext context) { |
39 | return Column( |
40 | mainAxisAlignment: MainAxisAlignment.center, |
41 | children: <Widget>[ |
42 | SizedBox( |
43 | width: 300, |
44 | child: TextField( |
45 | decoration: const InputDecoration( |
46 | border: OutlineInputBorder(), |
47 | hintText: 'Enter the name for a person to track' , |
48 | ), |
49 | onSubmitted: (String value) { |
50 | setState(() { |
51 | names.add(value); |
52 | }); |
53 | fieldText.clear(); |
54 | }, |
55 | controller: fieldText, |
56 | ), |
57 | ), |
58 | const SizedBox(height: 50), |
59 | Row( |
60 | mainAxisAlignment: MainAxisAlignment.center, |
61 | children: <Widget>[ |
62 | GestureDetector( |
63 | onTap: () { |
64 | setState(() { |
65 | if (index == 0) { |
66 | index = names.length - 1; |
67 | } else { |
68 | index -= 1; |
69 | } |
70 | }); |
71 | }, |
72 | child: const Icon(Icons.chevron_left, key: Key('gesture1' )), |
73 | ), |
74 | Column( |
75 | mainAxisAlignment: MainAxisAlignment.center, |
76 | children: <Widget>[ |
77 | IndexedStack( |
78 | index: index, |
79 | children: <Widget>[for (final String name in names) PersonTracker(name: name)], |
80 | ), |
81 | ], |
82 | ), |
83 | GestureDetector( |
84 | onTap: () { |
85 | setState(() { |
86 | if (index == names.length - 1) { |
87 | index = 0; |
88 | } else { |
89 | index += 1; |
90 | } |
91 | }); |
92 | }, |
93 | child: const Icon(Icons.chevron_right, key: Key('gesture2' )), |
94 | ), |
95 | ], |
96 | ), |
97 | ], |
98 | ); |
99 | } |
100 | } |
101 | |
102 | class PersonTracker extends StatefulWidget { |
103 | const PersonTracker({super.key, required this.name}); |
104 | final String name; |
105 | @override |
106 | State<PersonTracker> createState() => _PersonTrackerState(); |
107 | } |
108 | |
109 | class _PersonTrackerState extends State<PersonTracker> { |
110 | int counter = 0; |
111 | @override |
112 | Widget build(BuildContext context) { |
113 | return Container( |
114 | key: Key(widget.name), |
115 | decoration: BoxDecoration( |
116 | color: const Color.fromARGB(255, 239, 248, 255), |
117 | border: Border.all(color: const Color.fromARGB(255, 54, 60, 244)), |
118 | borderRadius: const BorderRadius.all(Radius.circular(10)), |
119 | ), |
120 | padding: const EdgeInsets.all(16.0), |
121 | child: Column( |
122 | children: <Widget>[ |
123 | Text('Name: ${widget.name}' ), |
124 | Text('Score: $counter' ), |
125 | TextButton.icon( |
126 | key: Key('increment ${widget.name}' ), |
127 | icon: const Icon(Icons.add), |
128 | onPressed: () { |
129 | setState(() { |
130 | counter += 1; |
131 | }); |
132 | }, |
133 | label: const Text('Increment' ), |
134 | ), |
135 | ], |
136 | ), |
137 | ); |
138 | } |
139 | } |
140 | |