-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
199 lines (173 loc) · 5.26 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
195
196
197
198
199
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
static const String t = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: t,
home: const MyHomePage(title: t),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late PageDataHolder h;
late SizedBox b;
late CustomButton button;
late ThemeData t;
_MyHomePageState() {
h = PageDataHolder(4, onSelectChangedCallback);
b = SizedBox(width: double.infinity, child: SingleChildScrollView(child: CustomDataTable(rows: h.getRows())));
button = CustomButton(onTap: onPressButton);
}
@override
Widget build(BuildContext context) {
t = Theme.of(context);
h.applyColorTheme(t);
b = SizedBox(width: double.infinity, child: SingleChildScrollView(child: CustomDataTable(rows: h.getRows())));
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: b,
floatingActionButton: button,
);
}
void onSelectChangedCallback(int i) {
print(i);
setState(() {
h.updateSelected(i);
});
h.updateRows();
b = SizedBox(width: double.infinity, child: SingleChildScrollView(child: CustomDataTable(rows: h.getRows())));
}
void onPressButton() {
setState(() {
h.addItem();
});
b = SizedBox(width: double.infinity, child: SingleChildScrollView(child: CustomDataTable(rows: h.getRows())));
}
}
class CustomDataTable extends StatelessWidget {
const CustomDataTable({Key? key, required this.rows}) : super(key: key);
final List<CustomDataRow> rows;
@override
Widget build(BuildContext context) {
return DataTable(
columns: const <DataColumn>[
DataColumn(label: Text('A')),
DataColumn(label: Text('B')),
],
rows: rows,
);
}
}
class CustomDataRow extends DataRow {
CustomDataRow(RowDataHolder data) : super.byIndex(
index: data.getIndex(),
selected: data.getSelected(),
cells: <DataCell>[
DataCell(Text(data.getValueA())),
DataCell(Text(data.getValueB())),
],
onSelectChanged: data.onSelectChangedHook,
);
CustomDataRow.withColorTheme(RowDataHolder data, ThemeData t) : super.byIndex(
index: data.getIndex(),
selected: data.getSelected(),
cells: <DataCell>[
DataCell(Text(data.getValueA())),
DataCell(Text(data.getValueB())),
],
onSelectChanged: data.onSelectChangedHook,
color: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> s) {
if (s.contains(MaterialState.selected)) {
return t.colorScheme.primary.withOpacity(0.08);
}
if (data.getIndex().isEven) {
return Colors.grey.withOpacity(0.3);
}
return null;
}),
);
}
class CustomButton extends StatelessWidget {
const CustomButton({Key? key, required this.onTap}) : super(key: key);
final Function()? onTap;
@override
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: onTap,
child: const Icon(Icons.add),
);
}
}
class PageDataHolder {
late int numItems;
late Function(int) onSelectChangedCallback;
late List<RowDataHolder> rowHolders;
late List<CustomDataRow> rows;
late ThemeData? theme;
List<CustomDataRow> getRows() => rows;
PageDataHolder(int n, Function(int) c) {
numItems = n;
onSelectChangedCallback = c;
rowHolders = List<RowDataHolder>.generate(numItems, (int i) => RowDataHolder(i, false, 'A $i', 'B $i', c));
rows = List<CustomDataRow>.generate(numItems, (int i) => CustomDataRow(rowHolders[i]));
theme = null;
}
void applyColorTheme(ThemeData t) {
theme = t;
rows = List<CustomDataRow>.generate(numItems, (int i) => CustomDataRow.withColorTheme(rowHolders[i], theme!));
}
void updateSelected(int i) => rowHolders[i].setSelected(!rowHolders[i].getSelected());
void updateRows() {
if (theme == null) {
rows = List<CustomDataRow>.generate(numItems, (int i) => CustomDataRow(rowHolders[i]));
}
else {
rows = List<CustomDataRow>.generate(numItems, (int i) => CustomDataRow.withColorTheme(rowHolders[i], theme!));
}
}
void addItem() {
numItems++;
int i = numItems - 1;
rowHolders.add(RowDataHolder(i, false, 'A $i', 'B $i', onSelectChangedCallback));
rows.add(CustomDataRow(rowHolders[i]));
if (theme == null) {
rows.add(CustomDataRow(rowHolders[i]));
}
else {
rows.add(CustomDataRow.withColorTheme(rowHolders[i], theme!));
}
}
}
class RowDataHolder {
late int index;
late bool selected;
late String valueA;
late String valueB;
late Function(int) onSelectChangedCallback;
void setSelected(bool v) {
selected = v;
}
int getIndex() => index;
bool getSelected() => selected;
String getValueA() => valueA;
String getValueB() => valueB;
RowDataHolder(int i, bool s, String a, String b, Function(int) c) {
index = i;
selected = s;
valueA = a;
valueB = b;
onSelectChangedCallback = c;
}
void onSelectChangedHook(bool? v) {
onSelectChangedCallback(index);
}
}