Skip to content

Commit 12941c6

Browse files
committed
Flutter sqlite tutorial
1 parent 23fb727 commit 12941c6

File tree

3 files changed

+107
-101
lines changed

3 files changed

+107
-101
lines changed

lib/item_list.dart

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_sqlite/add_item.dart';
3+
import 'package:flutter_sqlite/each_item.dart';
4+
import 'package:flutter_sqlite/sqlite_helper.dart';
5+
6+
class ItemList extends StatefulWidget {
7+
const ItemList({Key? key, required this.title}) : super(key: key);
8+
9+
10+
final String title;
11+
12+
@override
13+
State<ItemList> createState() => _ItemListState();
14+
}
15+
16+
class _ItemListState extends State<ItemList> {
17+
int _counter = 0;
18+
bool _deletingAll=false;
19+
bool _orderDesc=false;
20+
Future<List<Map>> _futureData=SqliteHelper().fetchAll();
21+
22+
void _addItem() {
23+
showModalBottomSheet(
24+
context: context,
25+
builder: (BuildContext context) {
26+
return AddItemForm(onChange: refreshItems,);
27+
});
28+
}
29+
30+
@override
31+
Widget build(BuildContext context) {
32+
return Scaffold(
33+
appBar: AppBar(
34+
title: Text('Shopping SQLite'),
35+
actions: [
36+
IconButton(onPressed: (){
37+
_orderDesc=!_orderDesc;
38+
orderByID(_orderDesc);
39+
}, icon: Icon(Icons.short_text)),
40+
_deletingAll?CircularProgressIndicator():IconButton(onPressed: () async {
41+
setState((){
42+
_deletingAll=true;
43+
});
44+
bool deleted=await SqliteHelper().deleteAll();
45+
if(deleted)
46+
{
47+
refreshItems();
48+
}
49+
setState((){
50+
_deletingAll=false;
51+
});
52+
}, icon: Icon(Icons.delete))
53+
],
54+
),
55+
body: FutureBuilder<List<Map>>(
56+
future: _futureData,
57+
builder: (context, snapshot) {
58+
if (snapshot.hasError) {
59+
return Center(
60+
child: Text('${snapshot.error}'),
61+
);
62+
}
63+
64+
if (snapshot.hasData) {
65+
List<Map> items = snapshot.data!;
66+
return ListView.builder(
67+
itemCount: items.length,
68+
itemBuilder: (context, index) {
69+
Map item = items[index];
70+
return EachItem(item: item,onChange:refreshItems);
71+
});
72+
}
73+
return CircularProgressIndicator();
74+
},
75+
),
76+
floatingActionButton: FloatingActionButton(
77+
onPressed: _addItem,
78+
tooltip: 'Add item',
79+
child: const Icon(Icons.add),
80+
), // This trailing comma makes auto-formatting nicer for build methods.
81+
);
82+
}
83+
84+
refreshItems(){
85+
setState((){
86+
_futureData=SqliteHelper().fetchAll();
87+
});
88+
}
89+
90+
orderByID(bool desc){
91+
Future<List<Map>> items;
92+
if(desc)
93+
{
94+
items=SqliteHelper().fetchAllOrderedByID();
95+
}
96+
else
97+
{
98+
items=SqliteHelper().fetchAll();
99+
}
100+
setState((){
101+
_futureData=items;
102+
});
103+
}
104+
}

lib/main.dart

Lines changed: 2 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_sqlite/add_item.dart';
33
import 'package:flutter_sqlite/each_item.dart';
4+
import 'package:flutter_sqlite/item_list.dart';
45
import 'package:flutter_sqlite/sqlite_helper.dart';
56

67
void main() {
@@ -27,110 +28,11 @@ class MyApp extends StatelessWidget {
2728
// is not restarted.
2829
primarySwatch: Colors.blue,
2930
),
30-
home: const MyHomePage(title: 'Flutter Demo Home Page'),
31+
home: const ItemList(title: 'Flutter Demo Home Page'),
3132
);
3233
}
3334
}
3435

35-
class MyHomePage extends StatefulWidget {
36-
const MyHomePage({Key? key, required this.title}) : super(key: key);
3736

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-
}
13537

13638

lib/sqlite_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class SqliteHelper{
6767
return numOfRows>0;
6868
}
6969

70-
Future<List<Map>> fetchAllOrderedByID({bool desc=false}) async {
70+
Future<List<Map>> fetchAllOrderedByID({bool desc=true}) async {
7171
/*todo 5: Fetch all items from the db*/
7272
await open();
7373
List<Map> items=await _database.query(tableName,orderBy: 'id ${desc?'desc':'asc'}');

0 commit comments

Comments
 (0)