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"
package="com.example.ifarm">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:label="ifarm"
android:name="${applicationName}"
......
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 {
FoodBucketSchedule({Key key}) : super(key: key);
......@@ -7,9 +10,86 @@ class FoodBucketSchedule extends StatefulWidget {
State<FoodBucketSchedule> createState() => _FoodBucketScheduleState();
}
TextEditingController dateandtime = TextEditingController();
class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
@override
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:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:ifarm/AB/Screens/FoodBucket/FoodBucketList.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 {
Home({Key key}) : super(key: key);
......@@ -15,15 +19,11 @@ class Home extends StatefulWidget {
String t = '29';
bool tempgood = true;
bool foodswitch = false;
bool waterswitch = false;
bool environmentswitch = false;
bool securityswitch = false;
class _HomeState extends State<Home> {
@override
void initState() {
getdata();
gettemp();
tz.initializeTimeZones();
super.initState();
}
......@@ -34,27 +34,13 @@ class _HomeState extends State<Home> {
.onValue
.listen((event) {
setState(() {
t = event.snapshot.value.toString();
t = double.parse(event.snapshot.value).round().toString();
});
});
}
getdata() async {
print('login');
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;
});
}
final cron = Cron();
ScheduledTask scheduledTask;
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
......@@ -120,14 +106,12 @@ class _HomeState extends State<Home> {
//Food
Expanded(
child: GestureDetector(
onTap: foodswitch
? () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => foodbuckets()));
}
: null,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => foodbuckets()));
},
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 15, vertical: 40),
......@@ -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> {
BottomNavigationBarItem(
label: 'Home', icon: FaIcon(FontAwesomeIcons.house)),
BottomNavigationBarItem(
label: 'Statistic', icon: FaIcon(FontAwesomeIcons.chartLine)),
label: 'Statistic', icon: FaIcon(FontAwesomeIcons.calendar)),
BottomNavigationBarItem(
label: 'Profile', icon: FaIcon(FontAwesomeIcons.user)),
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';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ifarm/AB/Utils/NotificationService.dart';
import 'package:ifarm/AB/Utils/ThemeProvider.dart';
import 'package:provider/provider.dart';
......@@ -11,6 +12,7 @@ import 'AB/Screens/Common/SplashScreen.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
NotificationService().initNotification();
await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
......
......@@ -127,6 +127,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.2"
cron:
dependency: "direct main"
description:
name: cron
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.1"
crypto:
dependency: transitive
description:
......@@ -244,6 +251,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
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:
dependency: "direct main"
description:
......@@ -264,21 +278,21 @@ packages:
name: flutter_local_notifications
url: "https://pub.dartlang.org"
source: hosted
version: "12.0.3"
flutter_local_notifications_linux:
version: "6.1.1"
flutter_local_notifications_platform_interface:
dependency: transitive
description:
name: flutter_local_notifications_linux
name: flutter_local_notifications_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
flutter_local_notifications_platform_interface:
dependency: transitive
version: "4.0.1"
flutter_native_timezone:
dependency: "direct main"
description:
name: flutter_local_notifications_platform_interface
name: flutter_native_timezone
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.0"
version: "2.0.0"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
......@@ -310,6 +324,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "10.2.1"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.5"
http_parser:
dependency: transitive
description:
......@@ -394,6 +415,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
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:
dependency: transitive
description:
......@@ -401,6 +443,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
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:
dependency: transitive
description:
......@@ -450,6 +499,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.3"
rxdart:
dependency: "direct main"
description:
name: rxdart
url: "https://pub.dartlang.org"
source: hosted
version: "0.27.6"
shared_preferences:
dependency: "direct main"
description:
......@@ -561,12 +617,12 @@ packages:
source: hosted
version: "0.4.12"
timezone:
dependency: transitive
dependency: "direct main"
description:
name: timezone
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.0"
version: "0.7.0"
typed_data:
dependency: transitive
description:
......
......@@ -28,7 +28,15 @@ dependencies:
font_awesome_flutter: ^10.2.1
provider: ^6.0.3
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:
......
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