Skip to content

Commit 75780d9

Browse files
committed
On Back Press function added
1 parent 84c26cb commit 75780d9

File tree

1 file changed

+118
-107
lines changed

1 file changed

+118
-107
lines changed

lib/screens/home_screen.dart

Lines changed: 118 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/cupertino.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter/services.dart';
34
import 'package:task_manager/helpers/database_helper.dart';
45
import 'package:task_manager/models/task_model.dart';
56
import 'history_screen.dart';
@@ -29,6 +30,10 @@ class _HomeScreenState extends State<HomeScreen> {
2930
});
3031
}
3132

33+
Future<bool> onBackPressed() {
34+
return SystemNavigator.pop();
35+
}
36+
3237
Widget _buildTask(Task task) {
3338
return Padding(
3439
padding: EdgeInsets.symmetric(horizontal: 25.0),
@@ -83,125 +88,131 @@ class _HomeScreenState extends State<HomeScreen> {
8388

8489
@override
8590
Widget build(BuildContext context) {
86-
return Scaffold(
87-
floatingActionButton: FloatingActionButton(
88-
backgroundColor: Colors.white,
89-
foregroundColor: Colors.black,
90-
child: Icon(Icons.add_outlined),
91-
onPressed: () => Navigator.push(
92-
context,
93-
MaterialPageRoute(
94-
builder: (_) => AddTaskScreen(
95-
updateTaskList: _updateTaskList,
91+
return WillPopScope(
92+
onWillPop: onBackPressed,
93+
child: Scaffold(
94+
floatingActionButton: FloatingActionButton(
95+
backgroundColor: Colors.white,
96+
foregroundColor: Colors.black,
97+
child: Icon(Icons.add_outlined),
98+
onPressed: () => Navigator.push(
99+
context,
100+
MaterialPageRoute(
101+
builder: (_) => AddTaskScreen(
102+
updateTaskList: _updateTaskList,
103+
),
96104
),
97105
),
98106
),
99-
),
100-
appBar: AppBar(
101-
backgroundColor: Color.fromRGBO(250, 250, 250, 1),
102-
leading: IconButton(
103-
icon: Icon(
104-
Icons.apps,
105-
color: Colors.black,
106-
),
107-
onPressed: null),
108-
title: Row(
109-
children: [
110-
Text(
111-
"Task",
112-
style: const TextStyle(
113-
color: Colors.grey,
114-
fontSize: 20.0,
115-
fontWeight: FontWeight.normal,
116-
letterSpacing: -1.2,
107+
appBar: AppBar(
108+
backgroundColor: Color.fromRGBO(250, 250, 250, 1),
109+
leading: IconButton(
110+
icon: Icon(
111+
Icons.apps,
112+
color: Colors.black,
117113
),
118-
),
119-
Text(
120-
"Manager",
121-
style: const TextStyle(
122-
color: Colors.redAccent,
123-
fontSize: 20.0,
124-
fontWeight: FontWeight.normal,
125-
letterSpacing: 0,
114+
onPressed: null),
115+
title: Row(
116+
children: [
117+
Text(
118+
"Task",
119+
style: const TextStyle(
120+
color: Colors.grey,
121+
fontSize: 20.0,
122+
fontWeight: FontWeight.normal,
123+
letterSpacing: -1.2,
124+
),
126125
),
126+
Text(
127+
"Manager",
128+
style: const TextStyle(
129+
color: Colors.redAccent,
130+
fontSize: 20.0,
131+
fontWeight: FontWeight.normal,
132+
letterSpacing: 0,
133+
),
134+
)
135+
],
136+
),
137+
centerTitle: false,
138+
elevation: 0,
139+
actions: [
140+
Container(
141+
margin: const EdgeInsets.all(0),
142+
child: IconButton(
143+
icon: Icon(Icons.history_outlined),
144+
iconSize: 25.0,
145+
color: Colors.black,
146+
onPressed: () => Navigator.push(context,
147+
MaterialPageRoute(builder: (_) => HistoryScreen()))),
148+
),
149+
Container(
150+
margin: const EdgeInsets.all(6.0),
151+
child: IconButton(
152+
icon: Icon(Icons.settings_outlined),
153+
iconSize: 25.0,
154+
color: Colors.black,
155+
onPressed: () => Navigator.push(
156+
context, MaterialPageRoute(builder: (_) => Settings()))),
127157
)
128158
],
129159
),
130-
centerTitle: false,
131-
elevation: 0,
132-
actions: [
133-
Container(
134-
margin: const EdgeInsets.all(0),
135-
child: IconButton(
136-
icon: Icon(Icons.history_outlined),
137-
iconSize: 25.0,
138-
color: Colors.black,
139-
onPressed: () => Navigator.push(context,
140-
MaterialPageRoute(builder: (_) => HistoryScreen()))),
141-
),
142-
Container(
143-
margin: const EdgeInsets.all(6.0),
144-
child: IconButton(
145-
icon: Icon(Icons.settings_outlined),
146-
iconSize: 25.0,
147-
color: Colors.black,
148-
onPressed: () => Navigator.push(
149-
context, MaterialPageRoute(builder: (_) => Settings()))),
150-
)
151-
],
152-
),
153-
body: FutureBuilder(
154-
future: _taskList,
155-
builder: (context, snapshot) {
156-
if (!snapshot.hasData) {
157-
return Center(
158-
child: CircularProgressIndicator(),
159-
);
160-
}
160+
body: FutureBuilder(
161+
future: _taskList,
162+
builder: (context, snapshot) {
163+
if (!snapshot.hasData) {
164+
return Center(
165+
child: CircularProgressIndicator(),
166+
);
167+
}
161168

162-
final int completedTaskCount = snapshot.data
163-
.where((Task task) => task.status == 0)
164-
.toList()
165-
.length;
169+
final int completedTaskCount = snapshot.data
170+
.where((Task task) => task.status == 0)
171+
.toList()
172+
.length;
166173

167-
return ListView.builder(
168-
padding: EdgeInsets.symmetric(vertical: 0.0),
169-
itemCount: 1 + snapshot.data.length,
170-
itemBuilder: (BuildContext context, int index) {
171-
if (index == 0) {
172-
return Padding(
173-
padding: EdgeInsets.symmetric(horizontal: 0.0, vertical: 0.0),
174-
child: Column(
175-
crossAxisAlignment: CrossAxisAlignment.start,
176-
children: <Widget>[
177-
Container(
178-
margin: const EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 0.0),
179-
padding: const EdgeInsets.all(10.0),
180-
decoration: new BoxDecoration(
181-
shape: BoxShape.rectangle,
182-
color: Color.fromRGBO(240, 240, 240, 1.0),
183-
borderRadius: BorderRadius.all(Radius.circular(10.0)),
184-
),
185-
child: Center(
186-
child: Text(
187-
'You have [ $completedTaskCount ] pending task out of [ ${snapshot.data.length} ]',
188-
style: TextStyle(
189-
color: Colors.blueGrey,
190-
fontSize: 15.0,
191-
fontWeight: FontWeight.normal,
174+
return ListView.builder(
175+
padding: EdgeInsets.symmetric(vertical: 0.0),
176+
itemCount: 1 + snapshot.data.length,
177+
itemBuilder: (BuildContext context, int index) {
178+
if (index == 0) {
179+
return Padding(
180+
padding:
181+
EdgeInsets.symmetric(horizontal: 0.0, vertical: 0.0),
182+
child: Column(
183+
crossAxisAlignment: CrossAxisAlignment.start,
184+
children: <Widget>[
185+
Container(
186+
margin:
187+
const EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 0.0),
188+
padding: const EdgeInsets.all(10.0),
189+
decoration: new BoxDecoration(
190+
shape: BoxShape.rectangle,
191+
color: Color.fromRGBO(240, 240, 240, 1.0),
192+
borderRadius:
193+
BorderRadius.all(Radius.circular(10.0)),
194+
),
195+
child: Center(
196+
child: Text(
197+
'You have [ $completedTaskCount ] pending task out of [ ${snapshot.data.length} ]',
198+
style: TextStyle(
199+
color: Colors.blueGrey,
200+
fontSize: 15.0,
201+
fontWeight: FontWeight.normal,
202+
),
192203
),
193204
),
194-
),
195-
)
196-
],
197-
),
198-
);
199-
}
200-
return _buildTask(snapshot.data[index - 1]);
201-
},
202-
);
203-
},
205+
)
206+
],
207+
),
208+
);
209+
}
210+
return _buildTask(snapshot.data[index - 1]);
211+
},
212+
);
213+
},
214+
),
204215
),
205216
);
206217
}
207-
}
218+
}

0 commit comments

Comments
 (0)