1
1
import 'package:flutter/material.dart' ;
2
2
import 'package:flutter_sqlite/add_item.dart' ;
3
3
import 'package:flutter_sqlite/each_item.dart' ;
4
+ import 'package:flutter_sqlite/item_list.dart' ;
4
5
import 'package:flutter_sqlite/sqlite_helper.dart' ;
5
6
6
7
void main () {
@@ -27,110 +28,11 @@ class MyApp extends StatelessWidget {
27
28
// is not restarted.
28
29
primarySwatch: Colors .blue,
29
30
),
30
- home: const MyHomePage (title: 'Flutter Demo Home Page' ),
31
+ home: const ItemList (title: 'Flutter Demo Home Page' ),
31
32
);
32
33
}
33
34
}
34
35
35
- class MyHomePage extends StatefulWidget {
36
- const MyHomePage ({Key ? key, required this .title}) : super (key: key);
37
36
38
- // This widget is the home page of your application. It is stateful, meaning
39
- // that it has a State object (defined below) that contains fields that affect
40
- // how it looks.
41
-
42
- // This class is the configuration for the state. It holds the values (in this
43
- // case the title) provided by the parent (in this case the App widget) and
44
- // used by the build method of the State. Fields in a Widget subclass are
45
- // always marked "final".
46
-
47
- final String title;
48
-
49
- @override
50
- State <MyHomePage > createState () => _MyHomePageState ();
51
- }
52
-
53
- class _MyHomePageState extends State <MyHomePage > {
54
- int _counter = 0 ;
55
- bool _deletingAll= false ;
56
- bool _orderDesc= false ;
57
- Future <List <Map >> _futureData= SqliteHelper ().fetchAll ();
58
-
59
- void _addItem () {
60
- /*todo 10: Add an item*/
61
- showModalBottomSheet (
62
- context: context,
63
- builder: (BuildContext context) {
64
- TextEditingController controller = TextEditingController ();
65
- return AddItemForm (onChange: refreshItems,);
66
- });
67
- }
68
-
69
- @override
70
- Widget build (BuildContext context) {
71
- return Scaffold (
72
- appBar: AppBar (
73
- title: Text ('Shopping SQLite' ),
74
- actions: [
75
- IconButton (onPressed: (){
76
- _orderDesc= ! _orderDesc;
77
- orderByID (_orderDesc);
78
- }, icon: Icon (Icons .short_text)),
79
- _deletingAll? CircularProgressIndicator (): IconButton (onPressed: () async {
80
- setState ((){
81
- _deletingAll= true ;
82
- });
83
- bool deleted= await SqliteHelper ().deleteAll ();
84
- if (deleted)
85
- {
86
- refreshItems ();
87
- }
88
- setState ((){
89
- _deletingAll= false ;
90
- });
91
- }, icon: Icon (Icons .delete))
92
- ],
93
- ),
94
- body: FutureBuilder <List <Map >>(
95
- future: _futureData,
96
- builder: (context, snapshot) {
97
- if (snapshot.hasError) {
98
- return Center (
99
- child: Text ('${snapshot .error }' ),
100
- );
101
- }
102
-
103
- if (snapshot.hasData) {
104
- List <Map > items = snapshot.data! ;
105
- return ListView .builder (
106
- itemCount: items.length,
107
- itemBuilder: (context, index) {
108
- Map item = items[index];
109
- return EachItem (item: item,onChange: refreshItems);
110
- });
111
- }
112
- return CircularProgressIndicator ();
113
- },
114
- ),
115
- floatingActionButton: FloatingActionButton (
116
- onPressed: _addItem,
117
- tooltip: 'Add item' ,
118
- child: const Icon (Icons .add),
119
- ), // This trailing comma makes auto-formatting nicer for build methods.
120
- );
121
- }
122
-
123
- refreshItems (){
124
- setState ((){
125
- _futureData= SqliteHelper ().fetchAll ();
126
- });
127
- }
128
-
129
- orderByID (bool desc){
130
- setState ((){
131
- _futureData= SqliteHelper ().fetchAllOrderedByID (desc: desc);
132
- });
133
- }
134
- }
135
37
136
38
0 commit comments