groups_field
285 строк · 7.7 Кб
1import 'package:flutter/material.dart';
2import 'package:groups_field/groups_field.dart';
3
4void main() {
5runApp(const MyApp());
6}
7
8class MyApp extends StatelessWidget {
9const MyApp({Key? key}) : super(key: key);
10
11// This widget is the root of your application.
12@override
13Widget build(BuildContext context) {
14return MaterialApp(
15title: 'Flutter Demo',
16theme: ThemeData(
17// This is the theme of your application.
18//
19// Try running your application with "flutter run". You'll see the
20// application has a blue toolbar. Then, without quitting the app, try
21// changing the primarySwatch below to Colors.green and then invoke
22// "hot reload" (press "r" in the console where you ran "flutter run",
23// or simply save your changes to "hot reload" in a Flutter IDE).
24// Notice that the counter didn't reset back to zero; the application
25// is not restarted.
26primarySwatch: Colors.blue,
27),
28home: const MyHomePage(title: 'Flutter Demo Home Page'),
29);
30}
31}
32
33class Tag {
34int id;
35String name;
36
37Tag({
38required this.id,
39required this.name,
40});
41}
42
43class MyHomePage extends StatefulWidget {
44const MyHomePage({
45required this.title,
46Key? key,
47}) : super(key: key);
48
49// This widget is the home page of your application. It is stateful, meaning
50// that it has a State object (defined below) that contains fields that affect
51// how it looks.
52
53// This class is the configuration for the state. It holds the values (in this
54// case the title) provided by the parent (in this case the App widget) and
55// used by the build method of the State. Fields in a Widget subclass are
56// always marked "final".
57
58final String title;
59
60@override
61_MyHomePageState createState() => _MyHomePageState();
62}
63
64class _MyHomePageState extends State<MyHomePage> {
65late final Group<String> simpleGroup;
66late final Group<Tag> tagsGroup;
67
68@override
69void initState() {
70super.initState();
71
72simpleGroup = Group<String>(
73attribute: "",
74getString: (value) => value,
75onCreate: (text) async => text,
76existFields: [
77"First simple group",
78"Second simple group",
79],
80fieldBuilder: (value) {
81return Container(
82margin: EdgeInsets.symmetric(
83horizontal: 2,
84vertical: 3,
85),
86decoration: BoxDecoration(
87borderRadius: BorderRadius.circular(6),
88color: Color(0xFF9eabc0),
89),
90child: Row(
91mainAxisSize: MainAxisSize.min,
92children: [
93Container(
94padding: EdgeInsets.only(
95top: 3,
96right: 3,
97bottom: 3,
98left: 6,
99),
100decoration: BoxDecoration(
101color: Colors.white.withOpacity(0.3),
102),
103child: Text(
104"Simple group! ",
105style: TextStyle(
106color: Color(0xFFffffff),
107),
108),
109),
110Container(
111padding: EdgeInsets.symmetric(
112horizontal: 6,
113vertical: 3,
114),
115child: Text(
116value,
117style: TextStyle(
118color: Color(0xFFffffff),
119fontWeight: FontWeight.bold,
120),
121),
122),
123],
124),
125);
126});
127
128int _idForCreatedTag = 2;
129
130final tagsSuggestions = [
131Tag(id: 1, name: "First tag"),
132Tag(id: 2, name: "Second tag"),
133];
134
135tagsGroup = Group<Tag>(
136attribute: "#",
137getString: (value) => value.name,
138onCreate: (text) async => Tag(id: _idForCreatedTag++, name: text),
139existFields: [
140Tag(id: 0, name: "0"),
141Tag(id: 1, name: "1"),
142],
143suggestions: tagsSuggestions,
144suggestionBuilder: (tag) {
145return Container(
146margin: EdgeInsets.symmetric(
147horizontal: 6,
148vertical: 3,
149),
150padding: EdgeInsets.symmetric(
151horizontal: 6,
152vertical: 3,
153),
154decoration: BoxDecoration(
155borderRadius: BorderRadius.circular(6),
156color: Color(0xFF53b9ea),
157),
158child: Text(
159tag.name,
160style: TextStyle(
161color: Color(0xFFffffff),
162),
163),
164);
165},
166fieldBuilder: (value) {
167return Container(
168margin: EdgeInsets.symmetric(
169horizontal: 2,
170vertical: 3,
171),
172padding: EdgeInsets.symmetric(
173horizontal: 6,
174vertical: 3,
175),
176decoration: BoxDecoration(
177borderRadius: BorderRadius.circular(6),
178color: Color(0xFF53b9ea),
179),
180child: Text(
181"#" + value.name,
182style: TextStyle(
183color: Color(0xFFffffff),
184),
185),
186);
187},
188);
189}
190
191@override
192Widget build(BuildContext context) {
193return Scaffold(
194body: Container(
195margin: const EdgeInsets.symmetric(
196horizontal: 6,
197vertical: 3,
198),
199padding: const EdgeInsets.symmetric(
200horizontal: 12,
201vertical: 12,
202),
203decoration: BoxDecoration(
204color: Colors.white,
205border: Border.all(
206color: Colors.lightBlue,
207),
208),
209child: Column(
210children: [
211Row(
212children: [
213Container(
214margin: EdgeInsets.only(right: 12),
215child: const Text(
216"Search:",
217style: TextStyle(
218fontWeight: FontWeight.bold,
219),
220),
221),
222Expanded(
223child: GroupsField(
224isScrollable: true,
225groups: [
226simpleGroup,
227tagsGroup,
228],
229),
230),
231],
232),
233Row(
234children: [
235Container(
236margin: EdgeInsets.only(right: 12),
237child: const Text(
238"Search:",
239style: TextStyle(
240fontWeight: FontWeight.bold,
241),
242),
243),
244Expanded(
245child: GroupsField(
246isScrollable: false,
247groups: [
248simpleGroup,
249tagsGroup,
250],
251),
252),
253],
254),
255Row(
256children: [
257Container(
258margin: EdgeInsets.only(right: 12),
259child: const Text(
260"Search:",
261style: TextStyle(
262fontWeight: FontWeight.bold,
263),
264),
265),
266Expanded(
267child: GroupsField(
268isScrollable: false,
269textFieldDecoration: InputDecoration(
270hintText: 'Some hint?',
271),
272groups: [
273simpleGroup,
274tagsGroup,
275],
276),
277),
278],
279),
280],
281),
282),
283);
284}
285}
286