/
main.dart
194 lines (174 loc) · 4.87 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:item_selector/item_selector.dart';
void main() => runApp(ExampleApp());
final pages = <PageData>[
PageData(
title: 'Column',
icon: Icons.view_agenda,
builder: (_) => ColumnPage(),
),
PageData(
title: 'ListView',
icon: Icons.view_headline,
builder: (_) => ListViewPage(),
),
PageData(
title: 'GridView',
icon: Icons.view_module,
builder: (_) => GridViewPage(),
),
];
class PageData {
const PageData({this.title, this.icon, this.builder});
final String? title;
final IconData? icon;
final WidgetBuilder? builder;
}
class ExampleApp extends StatefulWidget {
@override
_ExampleAppState createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
int currentPage = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Item Selector',
home: Scaffold(
appBar: AppBar(
title: Text('Item Selector + ${pages[currentPage].title}'),
),
body: AnimatedSwitcher(
duration: Duration(milliseconds: 246),
child: Container(
key: ValueKey<int>(currentPage),
child: pages[currentPage].builder!(context),
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentPage,
onTap: (index) => setState(() => currentPage = index),
items: pages
.map((page) => BottomNavigationBarItem(
icon: Icon(page.icon),
title: Text(page.title!),
))
.toList(),
),
),
);
}
}
Widget buildListItem(BuildContext context, int index, bool selected) {
return Card(
margin: EdgeInsets.all(10),
elevation: selected ? 2 : 10,
child: ListTile(
leading: FlutterLogo(),
contentPadding: EdgeInsets.all(10),
title: Text(index.toString()),
),
);
}
class ColumnPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ItemSelectionController(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(4, (index) {
return ItemSelectionBuilder(
index: index,
builder: buildListItem,
);
}),
),
);
}
}
class ListViewPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ItemSelectionController(
child: ListView.builder(
itemCount: 100,
itemBuilder: (BuildContext context, int index) {
return ItemSelectionBuilder(
index: index,
builder: buildListItem,
);
},
),
);
}
}
Widget buildGridItem(BuildContext context, int index, bool selected) {
return Card(
margin: EdgeInsets.all(10),
elevation: selected ? 2 : 10,
child: GridTile(
child: Center(child: FlutterLogo()),
footer: Padding(
padding: const EdgeInsets.all(2),
child: Text(
index.toString(),
textAlign: TextAlign.end,
),
),
),
);
}
class GridViewPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final selection = RectSelection(4);
return ItemSelectionController(
selection: selection,
child: GridView.count(
crossAxisCount: 4,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
children: List.generate(100, (int index) {
return ItemSelectionBuilder(
index: index,
builder: buildGridItem,
);
}),
),
onSelectionStart: selection.start,
onSelectionUpdate: selection.update,
);
}
}
class RectSelection extends ItemSelection {
RectSelection(this.columns);
final int columns;
ItemSelection oldSelection = ItemSelection();
int rowAt(int index) => index ~/ columns;
int columnAt(int index) => index % columns;
int indexAt(int row, int column) => column + row * columns;
bool start(int start, int end) {
oldSelection = ItemSelection(start, end);
return false;
}
bool update(int start, int end) {
// calculate rectangular selection bounds
final startRow = rowAt(min(start, end));
final endRow = rowAt(max(start, end));
final startColumn = columnAt(min(start, end));
final endColumn = columnAt(max(start, end));
// construct new rectangular selection row by row
final newSelection = ItemSelection();
for (int r = startRow; r <= endRow; ++r) {
final startIndex = indexAt(r, startColumn);
final endIndex = indexAt(r, endColumn);
newSelection.add(startIndex, endIndex);
}
// apply selection changes
addAll(ItemSelection.copy(newSelection)..removeAll(oldSelection));
removeAll(ItemSelection.copy(oldSelection)..removeAll(newSelection));
oldSelection = newSelection;
return true;
}
}