Skip to content

Commit baeaa03

Browse files
committed
Merge practice
2 parents eaa472a + 12941c6 commit baeaa03

File tree

7 files changed

+303
-109
lines changed

7 files changed

+303
-109
lines changed

lib/add_item.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_sqlite/sqlite_helper.dart';
3+
4+
class AddItemForm extends StatefulWidget {
5+
AddItemForm({
6+
Key? key,
7+
required this.onChange
8+
}) : super(key: key);
9+
final Function onChange;
10+
11+
@override
12+
State<AddItemForm> createState() => _AddItemFormState();
13+
}
14+
15+
class _AddItemFormState extends State<AddItemForm> {
16+
bool showLoader = false;
17+
TextEditingController controller = TextEditingController();
18+
19+
@override
20+
Widget build(BuildContext context) {
21+
return showLoader
22+
? Center(child: CircularProgressIndicator())
23+
: Column(
24+
mainAxisSize: MainAxisSize.min,
25+
children: [
26+
Text('Add to local DB'),
27+
TextFormField(
28+
controller: controller,
29+
),
30+
ElevatedButton(
31+
onPressed: () async {
32+
setState(() {
33+
showLoader = true;
34+
});
35+
String text = controller.text;
36+
Map<String, dynamic> dataToAdd = {
37+
'title': text,
38+
'status': 0
39+
};
40+
bool added = await SqliteHelper().insert(dataToAdd);
41+
setState(() {
42+
showLoader = false;
43+
});
44+
45+
if (added) {
46+
widget.onChange();
47+
Navigator.of(context).pop();
48+
}
49+
},
50+
child: Text('Save'))
51+
],
52+
);
53+
}
54+
}

lib/each_item.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_sqlite/item_details.dart';
3+
import 'package:flutter_sqlite/sqlite_helper.dart';
4+
5+
class EachItem extends StatefulWidget {
6+
const EachItem({Key? key, required this.item, required this.onChange})
7+
: super(key: key);
8+
9+
final Map item;
10+
final Function onChange;
11+
12+
@override
13+
State<EachItem> createState() => _EachItemState();
14+
}
15+
16+
class _EachItemState extends State<EachItem> {
17+
late bool status;
18+
bool deleting = false;
19+
SqliteHelper sqliteHelper = SqliteHelper();
20+
21+
initState() {
22+
super.initState();
23+
status = widget.item['status'] == 1 ? true : false;
24+
}
25+
26+
@override
27+
Widget build(BuildContext context) {
28+
return ListTile(
29+
onTap: () {
30+
Navigator.of(context).push(MaterialPageRoute(
31+
builder: (context) => ItemDetails(widget.item['id'])));
32+
},
33+
title: Text(widget.item['title']),
34+
leading: Checkbox(
35+
value: status,
36+
onChanged: (bool? value) {
37+
Map<String, dynamic> dataToUpdate = {'status': value! ? 1 : 0};
38+
sqliteHelper.update(widget.item['id'], dataToUpdate);
39+
40+
setState(() {
41+
status = value;
42+
});
43+
},
44+
),
45+
trailing: deleting
46+
? Container(height: 40, width: 40, child: CircularProgressIndicator())
47+
: IconButton(
48+
icon: Icon(Icons.delete),
49+
onPressed: () async {
50+
setState(() {
51+
deleting = true;
52+
});
53+
bool deleted = await sqliteHelper.delete(widget.item['id']);
54+
if (deleted) {
55+
widget.onChange();
56+
}
57+
setState(() {
58+
deleting = false;
59+
});
60+
},
61+
),
62+
);
63+
}
64+
}

lib/item_details.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_sqlite/sqlite_helper.dart';
3+
4+
class ItemDetails extends StatelessWidget {
5+
ItemDetails(this.id,{Key? key}) : super(key: key){
6+
_futureData=SqliteHelper().fetchOne(id);
7+
}
8+
late Future<Map> _futureData;
9+
int id;
10+
@override
11+
Widget build(BuildContext context) {
12+
return Scaffold(
13+
appBar: AppBar(title: Text('Details'),),
14+
body: Padding(
15+
padding: const EdgeInsets.all(8.0),
16+
child: FutureBuilder<Map>(
17+
future: _futureData,
18+
builder: (context,snapshot){
19+
if(snapshot.hasError)
20+
{
21+
return Center(child: Text(snapshot.error.toString()));
22+
}
23+
24+
if(snapshot.hasData)
25+
{
26+
Map item=snapshot.data!;
27+
return Column(
28+
crossAxisAlignment: CrossAxisAlignment.stretch,
29+
children: [
30+
Text(item['title']),
31+
Text(item['status']==1?'Purchased':'To purchase'),
32+
],
33+
);
34+
}
35+
36+
return Center(child: CircularProgressIndicator());
37+
38+
}),
39+
),
40+
);
41+
}
42+
}

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: 5 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
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/item_list.dart';
5+
import 'package:flutter_sqlite/sqlite_helper.dart';
26

37
void main() {
48
runApp(const MyApp());
@@ -24,92 +28,11 @@ class MyApp extends StatelessWidget {
2428
// is not restarted.
2529
primarySwatch: Colors.blue,
2630
),
27-
home: const MyHomePage(title: 'Flutter Demo Home Page'),
31+
home: const ItemList(title: 'Flutter Demo Home Page'),
2832
);
2933
}
3034
}
3135

32-
class MyHomePage extends StatefulWidget {
33-
const MyHomePage({Key? key, required this.title}) : super(key: key);
3436

35-
// This widget is the home page of your application. It is stateful, meaning
36-
// that it has a State object (defined below) that contains fields that affect
37-
// how it looks.
3837

39-
// This class is the configuration for the state. It holds the values (in this
40-
// case the title) provided by the parent (in this case the App widget) and
41-
// used by the build method of the State. Fields in a Widget subclass are
42-
// always marked "final".
4338

44-
final String title;
45-
46-
@override
47-
State<MyHomePage> createState() => _MyHomePageState();
48-
}
49-
50-
class _MyHomePageState extends State<MyHomePage> {
51-
int _counter = 0;
52-
53-
void _incrementCounter() {
54-
setState(() {
55-
// This call to setState tells the Flutter framework that something has
56-
// changed in this State, which causes it to rerun the build method below
57-
// so that the display can reflect the updated values. If we changed
58-
// _counter without calling setState(), then the build method would not be
59-
// called again, and so nothing would appear to happen.
60-
_counter++;
61-
});
62-
}
63-
64-
@override
65-
Widget build(BuildContext context) {
66-
// This method is rerun every time setState is called, for instance as done
67-
// by the _incrementCounter method above.
68-
//
69-
// The Flutter framework has been optimized to make rerunning build methods
70-
// fast, so that you can just rebuild anything that needs updating rather
71-
// than having to individually change instances of widgets.
72-
return Scaffold(
73-
appBar: AppBar(
74-
// Here we take the value from the MyHomePage object that was created by
75-
// the App.build method, and use it to set our appbar title.
76-
title: Text(widget.title),
77-
),
78-
body: Center(
79-
// Center is a layout widget. It takes a single child and positions it
80-
// in the middle of the parent.
81-
child: Column(
82-
// Column is also a layout widget. It takes a list of children and
83-
// arranges them vertically. By default, it sizes itself to fit its
84-
// children horizontally, and tries to be as tall as its parent.
85-
//
86-
// Invoke "debug painting" (press "p" in the console, choose the
87-
// "Toggle Debug Paint" action from the Flutter Inspector in Android
88-
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
89-
// to see the wireframe for each widget.
90-
//
91-
// Column has various properties to control how it sizes itself and
92-
// how it positions its children. Here we use mainAxisAlignment to
93-
// center the children vertically; the main axis here is the vertical
94-
// axis because Columns are vertical (the cross axis would be
95-
// horizontal).
96-
mainAxisAlignment: MainAxisAlignment.center,
97-
children: <Widget>[
98-
const Text(
99-
'You have pushed the button this many times:',
100-
),
101-
Text(
102-
'$_counter',
103-
style: Theme.of(context).textTheme.headline4,
104-
),
105-
],
106-
),
107-
),
108-
floatingActionButton: FloatingActionButton(
109-
onPressed: _incrementCounter,
110-
tooltip: 'Increment',
111-
child: const Icon(Icons.add),
112-
), // This trailing comma makes auto-formatting nicer for build methods.
113-
);
114-
}
115-
}

0 commit comments

Comments
 (0)