/
main.dart
143 lines (133 loc) · 3.75 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
import 'package:editable/editable.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Editable example',
theme: ThemeData(
primarySwatch: Colors.blue,
primaryColor: Colors.blue,
accentColor: Colors.white,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Editable example'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
/// Create a Key for EditableState
final _editableKey = GlobalKey<EditableState>();
List rows = [
{
"name": 'James LongName Joe',
"date": '23/09/2020',
"month": 'June',
"status": 'completed'
},
{
"name": 'Daniel Paul',
"month": 'March',
"status": 'new',
"date": '12/4/2020',
},
{
"month": 'May',
"name": 'Mark Zuckerberg',
"date": '09/4/1993',
"status": 'expert'
},
{
"name": 'Jack',
"status": 'legend',
"date": '01/7/1820',
"month": 'December',
},
];
List cols = [
{"title": 'Name', 'widthFactor': 0.2, 'key': 'name', 'editable': false},
{"title": 'Date', 'widthFactor': 0.2, 'key': 'date'},
{"title": 'Month', 'widthFactor': 0.2, 'key': 'month'},
{"title": 'Status', 'key': 'status'},
];
/// Function to add a new row
/// Using the global key assigined to Editable widget
/// Access the current state of Editable
void _addNewRow() {
setState(() {
_editableKey.currentState.createRow();
});
}
///Print only edited rows.
void _printEditedRows() {
List editedRows = _editableKey.currentState.editedRows;
print(editedRows);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leadingWidth: 200,
leading: TextButton.icon(
onPressed: () => _addNewRow(),
icon: Icon(Icons.add),
label: Text(
'Add',
style: TextStyle(fontWeight: FontWeight.bold),
)),
title: Text(widget.title),
actions: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextButton(
onPressed: () => _printEditedRows(),
child: Text('Print Edited Rows',
style: TextStyle(fontWeight: FontWeight.bold))),
)
],
),
body: Editable(
key: _editableKey,
columns: cols,
rows: rows,
zebraStripe: true,
stripeColor1: Colors.blue[50],
stripeColor2: Colors.grey[200],
onRowSaved: (value) {
print(value);
},
onSubmitted: (value) {
print(value);
},
borderColor: Colors.blueGrey,
tdStyle: TextStyle(fontWeight: FontWeight.bold),
trHeight: 80,
thStyle: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
thAlignment: TextAlign.center,
thVertAlignment: CrossAxisAlignment.end,
thPaddingBottom: 3,
showSaveIcon: true,
saveIconColor: Colors.black,
showCreateButton: true,
tdAlignment: TextAlign.left,
tdEditableMaxLines: 100, // don't limit and allow data to wrap
tdPaddingTop: 0,
tdPaddingBottom: 14,
tdPaddingLeft: 10,
tdPaddingRight: 8,
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blue),
borderRadius: BorderRadius.all(Radius.circular(0))),
),
);
}
}