Commit 00a24151 authored by ayodyabanuka's avatar ayodyabanuka

Notification and Schedule basic added

parent 5d558f83
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ifarm"> package="com.example.ifarm">
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application <application
android:label="ifarm" android:label="ifarm"
android:name="${applicationName}" android:name="${applicationName}"
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:ifarm/AB/Utils/Colors.dart';
import 'package:intl/intl.dart';
class FoodBucketSchedule extends StatefulWidget { class FoodBucketSchedule extends StatefulWidget {
FoodBucketSchedule({Key key}) : super(key: key); FoodBucketSchedule({Key key}) : super(key: key);
...@@ -7,9 +10,86 @@ class FoodBucketSchedule extends StatefulWidget { ...@@ -7,9 +10,86 @@ class FoodBucketSchedule extends StatefulWidget {
State<FoodBucketSchedule> createState() => _FoodBucketScheduleState(); State<FoodBucketSchedule> createState() => _FoodBucketScheduleState();
} }
TextEditingController dateandtime = TextEditingController();
class _FoodBucketScheduleState extends State<FoodBucketSchedule> { class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container(); return Padding(
padding: const EdgeInsets.all(25),
child: Column(
children: [
const Center(
child: Text(
"Schedule your Food dispense Here!",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20),
)),
const SizedBox(
height: 40,
),
Row(
children: const [
Text(
"Pick Date and Time here",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 15),
),
],
),
const SizedBox(
height: 10,
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15), color: cardColor),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: TextFormField(
readOnly: true,
onTap: () {
DatePicker.showDateTimePicker(context,
showTitleActions: true,
minTime: DateTime(2022, 11, 5),
maxTime: DateTime(2019, 6, 7), onChanged: (date) {
print(
'change ${DateFormat('yyyy-MM-dd kk:mm').format(date)}');
}, onConfirm: (date) {
print(
'confirm ${DateFormat('yyyy-MM-dd kk:mm').format(date)}');
setState(() {
dateandtime.text =
DateFormat('yyyy-MM-dd – kk:mm').format(date);
});
}, currentTime: DateTime.now(), locale: LocaleType.en);
},
controller: dateandtime,
style:
const TextStyle(color: Color.fromARGB(255, 255, 255, 255)),
cursorColor: const Color.fromARGB(255, 0, 0, 0),
decoration: const InputDecoration(
hintText: "Pick Date and Time",
border: InputBorder.none,
hintStyle:
TextStyle(color: Color.fromARGB(255, 255, 255, 255))),
),
),
),
Expanded(
child: ListView.builder(
itemCount: 2,
itemBuilder: (context, index) {
return Container(
padding: const EdgeInsets.all(15),
decoration: BoxDecoration(
color: cardColor,
borderRadius: BorderRadius.circular(15)),
);
}))
],
),
);
} }
} }
import 'package:cron/cron.dart';
import 'package:firebase_database/firebase_database.dart'; import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:ifarm/AB/Screens/FoodBucket/FoodBucketList.dart'; import 'package:ifarm/AB/Screens/FoodBucket/FoodBucketList.dart';
import 'package:ifarm/AB/Utils/Colors.dart'; import 'package:ifarm/AB/Utils/Colors.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:ifarm/AB/Utils/NotificationService.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
class Home extends StatefulWidget { class Home extends StatefulWidget {
Home({Key key}) : super(key: key); Home({Key key}) : super(key: key);
...@@ -15,15 +19,11 @@ class Home extends StatefulWidget { ...@@ -15,15 +19,11 @@ class Home extends StatefulWidget {
String t = '29'; String t = '29';
bool tempgood = true; bool tempgood = true;
bool foodswitch = false;
bool waterswitch = false;
bool environmentswitch = false;
bool securityswitch = false;
class _HomeState extends State<Home> { class _HomeState extends State<Home> {
@override @override
void initState() { void initState() {
getdata(); gettemp();
tz.initializeTimeZones();
super.initState(); super.initState();
} }
...@@ -34,27 +34,13 @@ class _HomeState extends State<Home> { ...@@ -34,27 +34,13 @@ class _HomeState extends State<Home> {
.onValue .onValue
.listen((event) { .listen((event) {
setState(() { setState(() {
t = event.snapshot.value.toString(); t = double.parse(event.snapshot.value).round().toString();
}); });
}); });
} }
getdata() async { final cron = Cron();
print('login'); ScheduledTask scheduledTask;
SharedPreferences localStorage = await SharedPreferences.getInstance();
var foodswitch1 = localStorage.getBool('food') as bool;
var waterswitch1 = localStorage.getBool('water') as bool;
var environmentswitch1 = localStorage.getBool('env') as bool;
var securityswitch1 = localStorage.getBool('security') as bool;
setState(() {
foodswitch = foodswitch1;
waterswitch1 = waterswitch1;
environmentswitch = environmentswitch1;
securityswitch = securityswitch1;
});
}
int _currentIndex = 0; int _currentIndex = 0;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
...@@ -120,14 +106,12 @@ class _HomeState extends State<Home> { ...@@ -120,14 +106,12 @@ class _HomeState extends State<Home> {
//Food //Food
Expanded( Expanded(
child: GestureDetector( child: GestureDetector(
onTap: foodswitch onTap: () {
? () { Navigator.push(
Navigator.push( context,
context, MaterialPageRoute(
MaterialPageRoute( builder: (context) => foodbuckets()));
builder: (context) => foodbuckets())); },
}
: null,
child: Container( child: Container(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
horizontal: 15, vertical: 40), horizontal: 15, vertical: 40),
...@@ -262,10 +246,81 @@ class _HomeState extends State<Home> { ...@@ -262,10 +246,81 @@ class _HomeState extends State<Home> {
), ),
], ],
), ),
GestureDetector(
onTap: () {
NotificationService()
.showNotification(1, "title", "body", 3);
},
child: Container(
height: 40,
width: 200,
color: Colors.green,
child: Center(
child: Text("Show Notification"),
),
),
),
GestureDetector(
onTap: () {
NotificationService().cancelAllNotifications();
},
child: Container(
height: 40,
width: 200,
color: Colors.red,
child: Center(
child: Text(
"Cancel All Notifications",
),
),
),
),
GestureDetector(
onTap: () {
scheduleTask();
},
child: Container(
height: 40,
width: 200,
color: Color.fromARGB(255, 255, 255, 255),
child: Center(
child: Text(
"Schedule",
),
),
),
),
GestureDetector(
onTap: () {
cancelSheduleTask();
},
child: Container(
height: 40,
width: 200,
color: Color.fromARGB(255, 255, 255, 255),
child: Center(
child: Text(
"Cancel Schedule",
),
),
),
),
], ],
) ),
], ],
)), )),
); );
} }
void scheduleTask() async {
scheduledTask = cron.schedule(Schedule.parse("*/1 * * * *"), () async {
NotificationService().showNotification(2, "Food Dispensing",
"Food dispensing now ${DateTime.now().toString()}", 1);
});
}
void cancelSheduleTask() async {
scheduledTask.cancel();
print("cancel");
}
} }
...@@ -63,7 +63,7 @@ class _NavbarState extends State<Navbar> { ...@@ -63,7 +63,7 @@ class _NavbarState extends State<Navbar> {
BottomNavigationBarItem( BottomNavigationBarItem(
label: 'Home', icon: FaIcon(FontAwesomeIcons.house)), label: 'Home', icon: FaIcon(FontAwesomeIcons.house)),
BottomNavigationBarItem( BottomNavigationBarItem(
label: 'Statistic', icon: FaIcon(FontAwesomeIcons.chartLine)), label: 'Statistic', icon: FaIcon(FontAwesomeIcons.calendar)),
BottomNavigationBarItem( BottomNavigationBarItem(
label: 'Profile', icon: FaIcon(FontAwesomeIcons.user)), label: 'Profile', icon: FaIcon(FontAwesomeIcons.user)),
BottomNavigationBarItem( BottomNavigationBarItem(
......
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
class NotificationService {
static final NotificationService _notificationService =
NotificationService._internal();
factory NotificationService() {
return _notificationService;
}
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
NotificationService._internal();
Future<void> initNotification() async {
AndroidInitializationSettings initializationSettingsAndroid =
const AndroidInitializationSettings('@drawable/ifarm_icon_blue');
IOSInitializationSettings initializationSettingsIOS =
const IOSInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
);
final InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
Future<void> showNotification(
int id, String title, String body, int seconds) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
tz.TZDateTime.now(tz.local).add(Duration(seconds: seconds)),
const NotificationDetails(
android: AndroidNotificationDetails(
'main_channel', 'Main Channel', 'Main channel notifications',
playSound: false,
importance: Importance.max,
priority: Priority.max,
icon: '@drawable/ifarm_icon_blue'),
iOS: IOSNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
),
),
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true,
);
}
Future<void> cancelAllNotifications() async {
await flutterLocalNotificationsPlugin.cancelAll();
}
}
...@@ -3,6 +3,7 @@ import 'dart:io'; ...@@ -3,6 +3,7 @@ import 'dart:io';
import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:ifarm/AB/Utils/NotificationService.dart';
import 'package:ifarm/AB/Utils/ThemeProvider.dart'; import 'package:ifarm/AB/Utils/ThemeProvider.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
...@@ -11,6 +12,7 @@ import 'AB/Screens/Common/SplashScreen.dart'; ...@@ -11,6 +12,7 @@ import 'AB/Screens/Common/SplashScreen.dart';
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(); await Firebase.initializeApp();
NotificationService().initNotification();
await SystemChrome.setPreferredOrientations([ await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp, DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown, DeviceOrientation.portraitDown,
......
...@@ -127,6 +127,13 @@ packages: ...@@ -127,6 +127,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.2" version: "1.2.2"
cron:
dependency: "direct main"
description:
name: cron
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.1"
crypto: crypto:
dependency: transitive dependency: transitive
description: description:
...@@ -244,6 +251,13 @@ packages: ...@@ -244,6 +251,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.0" version: "2.0.0"
flutter_datetime_picker:
dependency: "direct main"
description:
name: flutter_datetime_picker
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.1"
flutter_launcher_icons: flutter_launcher_icons:
dependency: "direct main" dependency: "direct main"
description: description:
...@@ -264,21 +278,21 @@ packages: ...@@ -264,21 +278,21 @@ packages:
name: flutter_local_notifications name: flutter_local_notifications
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "12.0.3" version: "6.1.1"
flutter_local_notifications_linux: flutter_local_notifications_platform_interface:
dependency: transitive dependency: transitive
description: description:
name: flutter_local_notifications_linux name: flutter_local_notifications_platform_interface
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.0" version: "4.0.1"
flutter_local_notifications_platform_interface: flutter_native_timezone:
dependency: transitive dependency: "direct main"
description: description:
name: flutter_local_notifications_platform_interface name: flutter_native_timezone
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "6.0.0" version: "2.0.0"
flutter_plugin_android_lifecycle: flutter_plugin_android_lifecycle:
dependency: transitive dependency: transitive
description: description:
...@@ -310,6 +324,13 @@ packages: ...@@ -310,6 +324,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "10.2.1" version: "10.2.1"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.5"
http_parser: http_parser:
dependency: transitive dependency: transitive
description: description:
...@@ -394,6 +415,27 @@ packages: ...@@ -394,6 +415,27 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.2" version: "1.8.2"
path_provider:
dependency: "direct main"
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
path_provider_android:
dependency: transitive
description:
name: path_provider_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.21"
path_provider_ios:
dependency: transitive
description:
name: path_provider_ios
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
path_provider_linux: path_provider_linux:
dependency: transitive dependency: transitive
description: description:
...@@ -401,6 +443,13 @@ packages: ...@@ -401,6 +443,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.7" version: "2.1.7"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
path_provider_platform_interface: path_provider_platform_interface:
dependency: transitive dependency: transitive
description: description:
...@@ -450,6 +499,13 @@ packages: ...@@ -450,6 +499,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "6.0.3" version: "6.0.3"
rxdart:
dependency: "direct main"
description:
name: rxdart
url: "https://pub.dartlang.org"
source: hosted
version: "0.27.6"
shared_preferences: shared_preferences:
dependency: "direct main" dependency: "direct main"
description: description:
...@@ -561,12 +617,12 @@ packages: ...@@ -561,12 +617,12 @@ packages:
source: hosted source: hosted
version: "0.4.12" version: "0.4.12"
timezone: timezone:
dependency: transitive dependency: "direct main"
description: description:
name: timezone name: timezone
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.9.0" version: "0.7.0"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
......
...@@ -28,7 +28,15 @@ dependencies: ...@@ -28,7 +28,15 @@ dependencies:
font_awesome_flutter: ^10.2.1 font_awesome_flutter: ^10.2.1
provider: ^6.0.3 provider: ^6.0.3
connectivity_plus: ^2.3.0 connectivity_plus: ^2.3.0
flutter_local_notifications: ^12.0.3 flutter_local_notifications: ^6.0.0
timezone: ^0.7.0
rxdart: ^0.27.5
path_provider: ^2.0.11
http: ^0.13.4
flutter_native_timezone: ^2.0.0
cron: ^0.5.0
flutter_datetime_picker: ^1.5.1
intl: ^0.15.7
dev_dependencies: dev_dependencies:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment