I'm working currently on a flutter project
the app is working fine but i have some issues:
1-i get the notification but i don't get any sound from the notification, i don't know if this is flutter_local_notifications dependency problem because when i tried to update it to the latest version i got few errors from main page and alarm page
2-i can't access these options(Repeat,Sound,Title):
https://i.ibb.co/4fj7x16/s3.png
here is the alarm page:
import 'package:clock_app/alarm_helper.dart';
import 'package:clock_app/constants/theme_data.dart';
import 'package:clock_app/data.dart';
import 'package:clock_app/models/alarm_info.dart';
import 'package:dotted_border/dotted_border.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:intl/intl.dart';
import '../main.dart';
class AlarmPage extends StatefulWidget {
#override
_AlarmPageState createState() => _AlarmPageState();
}
class _AlarmPageState extends State<AlarmPage> {
DateTime _alarmTime;
String _alarmTimeString;
AlarmHelper _alarmHelper = AlarmHelper();
Future<List<AlarmInfo>> _alarms;
#override
void initState() {
_alarmTime = DateTime.now();
_alarmHelper.initializeDatabase().then((value) {
print('------database intialized');
loadAlarms();
});
super.initState();
}
void loadAlarms() {
_alarms = _alarmHelper.getAlarms();
if (mounted) setState(() {});
}
#override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 32, vertical: 64),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Alarm',
style: TextStyle(
fontFamily: 'avenir',
fontWeight: FontWeight.w700,
color: CustomColors.primaryTextColor,
fontSize: 24),
),
Expanded(
child: FutureBuilder<List<AlarmInfo>>(
future: _alarms,
builder: (context, snapshot) {
if (snapshot.hasData)
return ListView(
children: snapshot.data.map<Widget>((alarm) {
var alarmTime =
DateFormat('hh:mm aa').format(alarm.alarmDateTime);
var gradientColor = GradientTemplate
.gradientTemplate[alarm.gradientColorIndex].colors;
return Container(
margin: const EdgeInsets.only(bottom: 32),
padding: const EdgeInsets.symmetric(
horizontal: 16, vertical: 8),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: gradientColor,
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
boxShadow: [
BoxShadow(
color: gradientColor.last.withOpacity(0.4),
blurRadius: 8,
spreadRadius: 2,
offset: Offset(4, 4),
),
],
borderRadius: BorderRadius.all(Radius.circular(24)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Icon(
Icons.label,
color: Colors.white,
size: 24,
),
SizedBox(width: 8),
Text(
alarm.title,
style: TextStyle(
color: Colors.white,
fontFamily: 'avenir'),
),
],
),
Switch(
onChanged: (bool value) {},
value: true,
activeColor: Colors.white,
),
],
),
Text(
'Mon-Fri',
style: TextStyle(
color: Colors.white, fontFamily: 'avenir'),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
alarmTime,
style: TextStyle(
color: Colors.white,
fontFamily: 'avenir',
fontSize: 24,
fontWeight: FontWeight.w700),
),
IconButton(
icon: Icon(Icons.delete),
color: Colors.white,
onPressed: () {
_alarmHelper.delete(alarm.id);
},
),
],
),
],
),
);
}).followedBy([
if (alarms.length < 5)
DottedBorder(
strokeWidth: 2,
color: CustomColors.clockOutline,
borderType: BorderType.RRect,
radius: Radius.circular(24),
dashPattern: [5, 4],
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: CustomColors.clockBG,
borderRadius:
BorderRadius.all(Radius.circular(24)),
),
child: FlatButton(
padding: const EdgeInsets.symmetric(
horizontal: 32, vertical: 16),
onPressed: () {
_alarmTimeString =
DateFormat('HH:mm').format(DateTime.now());
showModalBottomSheet(
useRootNavigator: true,
context: context,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(24),
),
),
builder: (context) {
return StatefulBuilder(
builder: (context, setModalState) {
return Container(
padding: const EdgeInsets.all(32),
child: Column(
children: [
FlatButton(
onPressed: () async {
var selectedTime =
await showTimePicker(
context: context,
initialTime:
TimeOfDay.now(),
);
if (selectedTime != null) {
final now = DateTime.now();
var selectedDateTime =
DateTime(
now.year,
now.month,
now.day,
selectedTime.hour,
selectedTime
.minute);
_alarmTime =
selectedDateTime;
setModalState(() {
_alarmTimeString =
selectedTime
.toString();
});
}
},
child: Text(
_alarmTimeString,
style:
TextStyle(fontSize: 32),
),
),
ListTile(
title: Text('Repeat'),
trailing: Icon(
Icons.arrow_forward_ios),
),
ListTile(
title: Text('Sound'),
trailing: Icon(
Icons.arrow_forward_ios),
),
ListTile(
title: Text('Title'),
trailing: Icon(
Icons.arrow_forward_ios),
),
FloatingActionButton.extended(
onPressed: () async {
DateTime
scheduleAlarmDateTime;
if (_alarmTime
.isAfter(DateTime.now()))
scheduleAlarmDateTime =
_alarmTime;
else
scheduleAlarmDateTime =
_alarmTime.add(
Duration(days: 1));
var alarmInfo = AlarmInfo(
alarmDateTime:
scheduleAlarmDateTime,
gradientColorIndex:
alarms.length,
title: 'alarm',
);
_alarmHelper
.insertAlarm(alarmInfo);
scheduleAlarm(
scheduleAlarmDateTime);
},
icon: Icon(Icons.alarm),
label: Text('Save'),
),
],
),
);
},
);
},
);
// scheduleAlarm();
},
child: Column(
children: <Widget>[
Image.asset(
'assets/add_alarm.png',
scale: 1.5,
),
SizedBox(height: 8),
Text(
'Add Alarm',
style: TextStyle(
color: Colors.white,
fontFamily: 'avenir'),
),
],
),
),
),
)
else
Text('Only 5 alarms allowed!'),
]).toList(),
);
return Center(
child: Text(
'Loading..',
style: TextStyle(color: Colors.white),
),
);
},
),
),
],
),
);
}
void scheduleAlarm(DateTime scheduledNotificationDateTime) async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'alarm_notif',
'alarm_notif',
'Channel for Alarm notification',
icon: 'logo',
sound: RawResourceAndroidNotificationSound('a_long_cold_sting'),
largeIcon: DrawableResourceAndroidBitmap('logo'),
);
var iOSPlatformChannelSpecifics = IOSNotificationDetails(
sound: 'a_long_cold_sting.wav',
presentAlert: true,
presentBadge: true,
presentSound: true);
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.schedule(
0,
'Office',
'Good morning! Time for office.',
scheduledNotificationDateTime,
platformChannelSpecifics);
}
}
and here is the main page:
import 'package:clock_app/enums.dart';
import 'package:clock_app/models/menu_info.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:provider/provider.dart';
import 'views/homepage.dart';
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
var initializationSettingsAndroid =
AndroidInitializationSettings('logo');
var initializationSettingsIOS = IOSInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
onDidReceiveLocalNotification:
(int id, String title, String body, String payload) async {});
var initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
});
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: ChangeNotifierProvider<MenuInfo>(
create: (context) => MenuInfo(MenuType.clock),
child: HomePage(),
),
);
}
}
and the dependencies:
flutter_local_notifications: ^1.4.4+2
i am still learning and following a tutorial, so please if you know what is the problem and how to solve it explain to me
Thanks in advance
in flutter local notification, you can set the alarm to be insisted by setting extra flag 4, and thus keep the alarm repeat until user tap on it
for more detail click here
Related
am building a flutter app and am am working with a data from a API i got the list and evreything i did a search bar that works properly by typing either the number of the ticket or the description but am trying to add a new filter , a checkbox that filters the data i have 2 problems
1: when i press on a checkbox and type confirm it works and it shows but the checkbox dosent stay checked
2: how can i implement it so it filter the data from the api directly
and thank you
// ignore_for_file: use_key_in_widget_constructors, avoid_print, avoid_unnecessary_containers, curly_braces_in_flow_control_structures, prefer_const_constructors, non_constant_identifier_names, unnecessary_new, avoid_function_literals_in_foreach_calls, unused_import, avoid_types_as_parameter_names, unused_label, unused_element, file_names, library_private_types_in_public_api, unnecessary_string_interpolations
import 'dart:convert';
import 'dart:io';
import 'package:az/Actifs.dart';
import 'package:az/SR_Details.dart';
import 'package:az/main.dart';
import 'package:badges/badges.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'class/sr.dart';
import 'package:dropdown_search/dropdown_search.dart';
class DataFromAPI extends StatefulWidget {
#override
_DataFromAPIState createState() => _DataFromAPIState();
}
List<Sr> _MyAllData=[];
List<Sr> _SrForDsiplay=[];
Map<String, Color> map={
"QUEUED":Color.fromARGB(255, 255, 136, 0),
"CLOSED":Colors.grey,
"Rejected":Colors.red,
"INPROG":Colors.green,
"PENDING":Colors.blue,
"RESOLVED":Colors.green,
"NEW":Colors.blue,
};
Map<String, String> map1={
"1":"Urgent",
"2":"High",
"3":"Medium",
"4":"Low",
};
/*Map<String, Color> map3={
"Urgent":Colors.red,
"High":Color.fromARGB(255, 255, 139, 131),
"Medium":Colors.blue,
"Low":Colors.green,
"null":Colors.grey,
};*/
class Multiselect extends StatefulWidget {
final List<String> items;
const Multiselect({Key? key, required this.items}) : super(key: key);
#override
State<Multiselect> createState() => _MultiselectState();
}
class _MultiselectState extends State<Multiselect> {
final List<String> _selectedItems=[];
void _itemChange(String itemValue, bool isSelected) {
setState(() {
if (isSelected) {
_selectedItems.add(itemValue);
} else {
_selectedItems.remove(itemValue);
}
});
}
// this function is called when the Cancel button is pressed
void _cancel() {
Navigator.pop(context);
}
// this function is called when the Submit button is tapped
void _submit() {
Navigator.pop(context, _selectedItems);
}
#override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Select Topics'),
content: SingleChildScrollView(
child: ListBody(
children: widget.items
.map((item) => CheckboxListTile(
value: _selectedItems.contains(item),
title: Text(item),
controlAffinity: ListTileControlAffinity.leading,
onChanged: (isChecked) => _itemChange(item, isChecked!),
))
.toList(),
),
),
actions: [
TextButton(
onPressed: _cancel,
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: _submit,
child: const Text('Submit'),
),
],
);
}
}
class _DataFromAPIState extends State<DataFromAPI> {
List<String> _selectedItems=[];
String title_string = "Liste des SR :";
void _ShowMultiSelect() async{
final List<String> items=[
'QUEUED',
'CLOSED',
'Rejected',
'INPROG',
'PENDING',
'RESOLVED',
'NEW',
];
final List<String>? results =await showDialog(
context: context,
builder: (BuildContext context) {
return Multiselect(items: items);
},
);
if(results !=null){
setState(() {
_selectedItems=results;
});
}
}
#override
void initState() {
loadData().then((value) {
_MyAllData.clear();
setState(() {
_MyAllData.addAll(value);
_SrForDsiplay=_MyAllData;
});
});
super.initState();
}
Future<List<Sr>> loadData() async {
try {
var response = await http.get(Uri.parse(
'http:MXSR/?_lid=&_lpwd=&_format=json'));
if (response.statusCode == 200) {
final jsonBody = json.decode(response.body);
Demandes data = Demandes.fromJson(jsonBody);
final srAttributes = data.queryMxsrResponse.mxsrSet.sr;
title_string='Liste des SR : ${srAttributes.length.toString()}';
return srAttributes;
}
} catch (e) {
throw Exception(e.toString());
}
throw Exception("");
}
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner:false,
home: new Scaffold(resizeToAvoidBottomInset: false ,
appBar: AppBar(
title: Text(title_string),
leading: IconButton(
icon: Icon(Icons.arrow_back), onPressed: () { Navigator.push(
context, MaterialPageRoute(builder: (context) => Home())); },
),
flexibleSpace: InkWell(onTap: () {
},),
),
body: FutureBuilder<List<Sr>?>(
future: loadData(),
builder: (context, snapshot) {
if (_MyAllData.isEmpty) {
return SizedBox(
height: MediaQuery.of(context).size.height / 1.3,
child: Center(
child: CircularProgressIndicator(),
),
);
} else { return
ListView(
children: <Widget>[Padding(
padding: const EdgeInsets.all(8.0),
child: Column(children:<Widget>[ElevatedButton(onPressed: _ShowMultiSelect,
child: const Text('Filter')),
const Divider(
height: 30,
),
Wrap(
children: _selectedItems
.map((e) => Chip(
label: Text(e),
))
.toList(),
),
Row(children:<Widget>[ Expanded(
child: TextField(
decoration:
InputDecoration( hintText: "Enter id or description"),
onChanged: (text) {
text=text.toLowerCase();
setState(() {
_SrForDsiplay =_MyAllData.where((srAttributes) {
var srDescription = srAttributes.attributes.description!.content.toString().toLowerCase();
var srID= srAttributes.attributes.ticketid.content.toString();
return srDescription.contains(text) || srID.contains(text);
}).toList();
title_string='Liste des SR : ${_SrForDsiplay.length.toString()}';
print(title_string);
}
);
},
)),])
],
),),
new ListView.builder(scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: _SrForDsiplay.length,
itemBuilder: ((_, index) {
return
new ListTile(
title: new Card(
margin: new EdgeInsets.symmetric(
vertical: 2.0, horizontal: 8.0),
elevation: 10,
child: new ListTile(
title: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(padding: new EdgeInsets.all(2.0)),
Row(children :[
Container(
decoration: BoxDecoration(
border: Border.all(
color: Color.fromARGB(255, 255, 255, 255),
),
color: map['${_SrForDsiplay[index].attributes.status.content}'],
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child:Text(' ${_SrForDsiplay[index].attributes.status.content} '),
),
Container(child: Text(' '),),
Container(
decoration: BoxDecoration(
border: Border.all(
color: Color.fromARGB(255, 255, 255, 255),
),
color:Colors.grey,
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child:Text( map1['${_SrForDsiplay[index].attributes.reportedpriority?.content}'] ?? " null "),
),
],
),
SizedBox(
height: 8,
),
Row(children: <Widget>[
Expanded( child: Container(child: Text('${_SrForDsiplay[index].attributes.description?.content}',style: GoogleFonts.openSans(
textStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600)),),),),
Expanded(child: Container(child:Text( ' ${_SrForDsiplay[index].attributes.ticketid.content}',style: GoogleFonts.openSans(
textStyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400)),)))
],),
new Divider(
color: Color.fromARGB(255, 110, 109, 109),
),
Text(
'Reported : ${DateFormat.yMMMMEEEEd().format(DateTime.parse('${_SrForDsiplay[index].attributes.statusdate.content}' ))}' ,
style: GoogleFonts.openSans(
textStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w400)),
),
new Text(
'Reported by : ${_SrForDsiplay[index].attributes.reportedby?.content}',style: GoogleFonts.openSans(
textStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w400)),
),
Row(children: [new Image(image: AssetImage('assets/courroi.png'), width: 20),
Text(
'${_SrForDsiplay[index].relatedMbos?.asset[0].attributes.descriptionasset?.content}'),], ),
Row(children: [new Image(image: AssetImage('assets/emp.png'), width: 20),
Text(
'${_SrForDsiplay[index].attributes.assetsiteid?.content}'),], ) ,
Divider(
color: Color.fromARGB(255, 110, 109, 109),
),
Row(children:[
Expanded(child: Badge(
position: BadgePosition.topEnd(top: -8, end: 20),
badgeColor: Colors.grey,
badgeContent: Text('1'),
child :IconButton(icon :Icon(Icons.file_present_rounded), padding: const EdgeInsets.all(0),
onPressed: () {
},
),
)
),
Expanded(child: IconButton (
icon: Icon(Icons.file_copy),
onPressed: () { },
),),
Expanded(child: IconButton(onPressed:() {
}, icon: Icon(Icons.delete)) )
],)
],
),
trailing: Icon(Icons.arrow_forward_ios_rounded),
),
),
onTap: () {
Navigator.push(
context,MaterialPageRoute(builder: (context) =>SrDetailsScreen(sr: _SrForDsiplay[index])),
);
}
);
}
),
)
]
);
}
},
),
),
);
}
}
hi i m using the rest API to bring data from the data base and when i make a change in the data base i want it so if i Go to the other screen and Pop to the previous screen i want the get request from the api to re build the list and the new items should appear but at the moment it dosen't refresh so only the old list appears any help and thank you
import 'dart:convert';
import 'dart:io';
import 'package:az/Actifs.dart';
import 'package:az/SR_Details.dart';
import 'package:az/main.dart';
import 'package:badges/badges.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'class/sr.dart';
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: DataFromAPI(),
);
}
}
class DataFromAPI extends StatefulWidget {
#override
_DataFromAPIState createState() => _DataFromAPIState();
}
List<Sr> _MyAllData=[];
List<Sr> _SrForDsiplay=[];
Map<String, Color> map={
"QUEUED":Color.fromARGB(255, 255, 136, 0),
"CLOSED":Colors.grey,
"Rejeced":Colors.red,
"INPROG":Colors.green,
"PENDING":Colors.blue,
"RESOLVED":Colors.green,
"NEW":Colors.blue,
};
Map<String, String> map1={
"1":"Urgent",
"2":"High",
"3":"Medium",
"4":"Low",
};
Map<String, Color> map3={
"Urgent":Colors.red,
"High":Color.fromARGB(255, 255, 139, 131),
"Medium":Colors.blue,
"Low":Colors.green,
"null":Colors.grey,
};
class _DataFromAPIState extends State<DataFromAPI> {
String title_string = "Liste des SR";
#override
void initState() {
loadData().then((value) {
setState(() {
_MyAllData.addAll(value);
_SrForDsiplay=_MyAllData;
});
});
super.initState();
}
Future<List<Sr>> loadData() async {
try {
var response = await http.get(Uri.parse(
'http://192.168.1.59:9080/maxrest/rest/mbo/sr/?_lid=maxadmin&_lpwd=maxadmin&_format=json'));
if (response.statusCode == 200) {
final jsonBody = json.decode(response.body);
Demandes data = Demandes.fromJson(jsonBody);
final srAttributes = data.srMboSet.sr;
title_string='Liste des SR : ${srAttributes.length.toString()}';
return srAttributes;
}
} catch (e) {
throw Exception(e.toString());
}
throw Exception("");
}
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner:false,
home: new Scaffold(resizeToAvoidBottomInset: false ,
appBar: AppBar(
title: Text(title_string),
leading: IconButton(
icon: Icon(Icons.arrow_back), onPressed: () { Navigator.push(
context, MaterialPageRoute(builder: (context) => Home())); },
),
),
body: FutureBuilder<List<Sr>?>(
future: loadData(),
builder: (context, snapshot) {
if (!(_MyAllData.isNotEmpty)) {
return SizedBox(
height: MediaQuery.of(context).size.height / 1.3,
child: Center(
child: CircularProgressIndicator(),
),
);
} else { return ListView(
children: <Widget>[Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration:
InputDecoration(icon: Icon(Icons.search), hintText: "Enter id or description"),
onChanged: (text) {
text=text.toLowerCase();
setState(() {
_SrForDsiplay =_MyAllData.where((srAttributes) {
var srDescription = srAttributes.attributes.description!.content.toString().toLowerCase();
var srID= srAttributes.attributes.ticketid.content.toString();
return srDescription.contains(text) || srID.contains(text);
}).toList();
title_string='Liste des SR : ${_SrForDsiplay.length.toString()}';
print(title_string);
}
);
},
),
),
new ListView.builder(scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: _SrForDsiplay.length,
itemBuilder: ((_, index) {
return
new ListTile(
title: new Card(
margin: new EdgeInsets.symmetric(
vertical: 2.0, horizontal: 8.0),
elevation: 10,
child: new ListTile(
title: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(padding: new EdgeInsets.all(2.0)),
Row(children :[
Container(
decoration: BoxDecoration(
border: Border.all(
color: Color.fromARGB(255, 255, 255, 255),
),
color: map['${_SrForDsiplay[index].attributes.status.content}'],
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child:Text(' ${_SrForDsiplay[index].attributes.status.content} '),
),
Container(child: Text(' '),),
Container(
decoration: BoxDecoration(
border: Border.all(
color: Color.fromARGB(255, 255, 255, 255),
),
color: map3[map1['${_SrForDsiplay[index].attributes.reportedpriority?.content}']],
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child:Text( map1['${_SrForDsiplay[index].attributes.reportedpriority?.content}'] ?? " null "),
),
],
),
SizedBox(
height: 8,
),
Row(children: <Widget>[
Expanded( child: Container(child: Text('${_SrForDsiplay[index].attributes.description?.content}',style: GoogleFonts.openSans(
textStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600)),),),),
Expanded(child: Container(child:Text( ' ${_SrForDsiplay[index].attributes.ticketid.content}',style: GoogleFonts.openSans(
textStyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400)),)))
],),
new Divider(
color: Color.fromARGB(255, 110, 109, 109),
),
Text(
'Reported : ${DateFormat.yMMMMEEEEd().format(DateTime.parse('${_SrForDsiplay[index].attributes.statusdate.content}' ))}' ,
style: GoogleFonts.openSans(
textStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w400)),
),
new Text(
'Reported by : ${_SrForDsiplay[index].attributes.reportedby?.content}',style: GoogleFonts.openSans(
textStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w400)),
),
Row(children: [new Image(image: AssetImage('assets/courroi.png'), width: 20),
Text(
'${_SrForDsiplay[index].attributes.assetnum?.content}'),], ),
Row(children: [new Image(image: AssetImage('assets/emp.png'), width: 20),
Text(
'${_SrForDsiplay[index].attributes.assetsiteid?.content}'),], ) ,
Divider(
color: Color.fromARGB(255, 110, 109, 109),
),
Row(children:[
Expanded(child: Badge(
position: BadgePosition.topEnd(top: -8, end: 20),
badgeColor: Colors.grey,
badgeContent: Text('1'),
child :IconButton(icon :Icon(Icons.file_present_rounded), padding: const EdgeInsets.all(0),
onPressed: () {
},),
)
),
Expanded(child: IconButton (
icon: Icon(Icons.file_copy),
onPressed: () { },
),),
Expanded(child: IconButton(onPressed:() {
}, icon: Icon(Icons.delete)) )
],)
],
),
trailing: Icon(Icons.arrow_forward_ios_rounded),
),
),
onTap: () {
/* Navigator.push(
context,MaterialPageRoute(builder: (context) =>SRDetail()),
);*/
}
);
}
),
)
]
);
}
},
),
),
);
}
}```
By the way, this is not good to call loadData() function in initState and Future.builder() at the same time.
Moreover, the common usage of Future is to handle HTTP calls. What you can listen to on a Future is its state. Whether it's done, finished with success, or had an error. But that's it.
A Future can't listen to a variable change. It's a one-time response. Instead, you'll need to use a Stream.
Evertime the setCountDown method is called i get an exception SetStateCalled during build ,,, please help! . I have to display this countdown in a previous screen too , Lets call it screen A and from A i pushReplaced and came to this Charging Screen which displays countdown and uses provider too . But everytime I update the provider value I get an error . It works fine in the debug mode but as soon as build a release it works weirdly . Please help!!!!
import 'dart:async';
import 'dart:developer';
import 'package:chargeapp_master/ChangeNotifiers/ChargeTimeNotifier.dart';
import 'package:chargeapp_master/Screens/main_map_screen.dart';
import 'package:chargeapp_master/Screens/navigation.dart';
import 'package:chargeapp_master/Screens/profile_screens/profile_screen.dart';
import 'package:chargeapp_master/Screens/wallet_screens/wallet_screen.dart';
import 'package:chargeapp_master/assistants/assistant_methods.dart';
import 'package:chargeapp_master/assistants/bluetooth_assistants.dart';
import 'package:chargeapp_master/widgets/bottbar.dart';
import 'package:circular_countdown_timer/circular_countdown_timer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:google_fonts/google_fonts.dart';
import '../User Details.dart';
import '../main.dart';
import 'Charging_summary_screen.dart';
import 'package:provider/provider.dart';
class Charging_screen extends StatefulWidget {
int duration;
String amount;
Charging_screen({Key? key, required this.duration, required this.amount})
: super(key: key);
#override
_Charging_screenState createState() => _Charging_screenState();
}
class _Charging_screenState extends State<Charging_screen> with ChangeNotifier {
double h(double height) {
return MediaQuery.of(context).size.height * height;
}
double w(double width) {
return MediaQuery.of(context).size.width * width;
}
void showNotification(String dat) {
flutterLocalNotificationsPlugin.show(
0,
"Powerstrip",
"Charging $dat",
NotificationDetails(
android: AndroidNotificationDetails(channel.id, channel.name,
importance: Importance.high,
color: Colors.blue,
playSound: true,
icon: '#mipmap/ic_launcher')));
}
CountDownController _controller = CountDownController();
Timer? countdownTimer;
Duration myDuration = Duration();
#override
void initState() {
super.initState();
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
Provider.of<ChargeTimeProvider>(context, listen: false)
.setDuration(Duration(minutes: widget.duration));
startTimer();
});
}
void setCountDown() {
final reduceSecondsBy = 1;
var seconds = Provider.of<ChargeTimeProvider>(context, listen: false)
.getDuration()
.inSeconds -
reduceSecondsBy;
log("got seconds from provider " + seconds.toString());
if (seconds < 0) {
countdownTimer?.cancel();
showNotification("Stopped");
log("$charging");
} else {
Provider.of<ChargeTimeProvider>(context, listen: false)
.setDuration(Duration(seconds: seconds));
// myDuration = Duration(seconds: seconds);
}
// setState(() {
// final seconds = myDuration.inSeconds - reduceSecondsBy;
// });
}
void startTimer() {
showNotification("Started");
context.read<ChargeTimeProvider>().setChargeStatus(true);
countdownTimer =
Timer.periodic(Duration(seconds: 1), (_) => setCountDown());
}
void stoptimer() {
Provider.of<ChargeTimeProvider>(context, listen: false)
.setChargeStatus(false);
// context.read<ChargeTimeProvider>().setChargeStatus(false);
log("$charging");
countdownTimer?.cancel();
}
String mins = "";
String sec = "";
var minutesChargedfor;
Future<bool> _onWillPop() async {
return (await showDialog(
barrierDismissible: true,
context: context,
builder: (context) => AlertDialog(
backgroundColor: Colors.black,
title: Text(
'Are you sure?',
style: GoogleFonts.sulphurPoint(color: Colors.white),
),
content: Text(
'Do you want to go to home screen',
style: GoogleFonts.sulphurPoint(color: Colors.white),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext) => Navigation()));
},
child: const Text('Yes'),
),
],
),
)) ??
false;
}
#override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () => _onWillPop(),
child: Scaffold(
backgroundColor: Color(0xff1B1D20),
body: Stack(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 70),
Center(
child: Container(
padding: EdgeInsets.all(12),
// margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(7),
),
child: Text(
"Congratulations, you've added Green miles🍀",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.green.shade600),
),
),
),
const SizedBox(
height: 20,
),
Center(
child: Column(
children: [
Center(
child: CircularCountDownTimer(
duration: widget.duration * 60,
controller: _controller,
width: MediaQuery.of(context).size.width / 2,
height: MediaQuery.of(context).size.height / 2,
ringColor: Colors.grey[400]!,
ringGradient: const LinearGradient(
colors: [Colors.white10, Colors.white70]),
fillColor: Colors.blue,
fillGradient: LinearGradient(colors: [
Colors.blue.shade300,
Colors.blue.shade900
]),
backgroundColor: Colors.grey[500],
backgroundGradient: LinearGradient(colors: [
Colors.grey.shade900,
Colors.grey.shade500
]),
strokeWidth: 20.0,
strokeCap: StrokeCap.round,
textStyle: const TextStyle(
fontSize: 33.0,
color: Colors.white,
fontWeight: FontWeight.bold),
// textFormat: CountdownTextFormat.MM_SS,
isTimerTextShown: true,
isReverse: false,
onComplete: () async {
var secondsChargedfor = widget.duration * 60;
var res = AssistantMethods.stopCharging(
secondsChargedfor, "0");
if (res != "failure") {
await Ble.disconnectDevice(connectedDevice);
stoptimer();
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => Charging_summary(
amount: widget.amount,
duration: widget.duration,
mins: mins,
sec: sec)));
} else {
Fluttertoast.showToast(msg: res);
}
Ble.disconnectDevice(connectedDevice);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (_) => Charging_summary(
amount: widget.amount,
duration: widget.duration,
mins: mins,
sec: sec)));
},
),
),
// SizedBox(height: 10,),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(
Icons.flash_on,
size: 30,
color: Colors.blue,
),
SizedBox(
width: 7,
),
Text(
"Charging",
style: TextStyle(
fontSize: 16,
color: Colors.white,
fontWeight: FontWeight.w400),
)
],
),
BuildTime(),
],
),
),
const SizedBox(
height: 40,
),
Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
child: Container(
padding: EdgeInsets.all(15),
// height: 130,
// width:MediaQuery.of(context).size.width ,
decoration: const BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.only(
topRight: Radius.circular(10.0),
topLeft: Radius.circular(10.0)),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
user_device_id.toString(),
style: const TextStyle(
fontSize: 14,
color: Colors.white,
fontWeight: FontWeight.w400),
),
],
),
device_type == 0
? Text(
"₹${widget.amount}",
style: const TextStyle(
fontWeight: FontWeight.w500,
fontSize: 18,
color: Colors.white),
)
: const Text(""),
],
),
const SizedBox(
height: 10,
),
Row(
children: [
Expanded(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
primary: Colors.red.shade900,
padding: const EdgeInsets.symmetric(
horizontal: 50, vertical: 15),
// minimumSize: Size(50,20 ),
),
child: const Text(
"Stop",
style: TextStyle(
fontSize: 15, color: Colors.white),
),
onPressed: () async {
var secondsChargedfor = widget.duration * 60;
secondsChargedfor = secondsChargedfor -
(int.parse(mins) * 60 + int.parse(sec));
log(secondsChargedfor.toString());
var res = AssistantMethods.stopCharging(
secondsChargedfor, "1");
if (res != "failure") {
await Ble.startStopTimer(services,
0.toString()); // to stop the charging
_controller.pause();
await Ble.disconnectDevice(connectedDevice);
stoptimer();
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
Charging_summary(
amount: widget.amount,
duration: widget.duration,
mins: mins,
sec: sec)));
} else {
Fluttertoast.showToast(msg: res);
}
},
),
),
],
),
],
),
),
),
],
),
],
),
//bottomNavigationBar: bottbar(),
),
);
}
Widget BuildTime() {
String twoDigits(int n) => n.toString().padLeft(2, '0');
final hours = twoDigits(
context.read<ChargeTimeProvider>().getDuration().inHours.remainder(24));
final minutes = twoDigits(context
.read<ChargeTimeProvider>()
.getDuration()
.inMinutes
.remainder(60));
final seconds = twoDigits(context
.read<ChargeTimeProvider>()
.getDuration()
.inSeconds
.remainder(60));
mins = minutes;
sec = seconds;
Provider.of<ChargeTimeProvider>(context, listen: true)
.setTime("$hours:$minutes:$seconds");
return Consumer<ChargeTimeProvider>(
builder: (context, chargetimenotifier, child) => Text(
"${chargetimenotifier.getTime()} Time Remaining",
style: TextStyle(fontSize: 14, color: Colors.white),
));
}
#override
void dispose() {
super.dispose();
}
}
Add Some Delay
Future.delayed(const Duration(milliseconds: 500), () {
// Here you can write your code
setState(() {
// Here you can write your code for open new view
});
});
i have a problem that when i close the application from the background, the mobile sends me a notification, but when i click on it, it doesn't take me to a notification screen t
he package
flutter_local_notifications
the database
sqlite
state mangment
get
notification screan
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../theme.dart';
class NotificationScreen extends StatefulWidget {
const NotificationScreen({Key? key, required this.payload}) : super(key: key);
final String payload;
#override
_NotificationScreenState createState() => _NotificationScreenState();
}
class _NotificationScreenState extends State<NotificationScreen> {
String _payload = '';
#override
void initState() {
super.initState();
_payload = widget.payload;
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: context.theme.backgroundColor,
appBar: AppBar(
leading: IconButton(
onPressed: () => Get.back(),
icon: Icon(
Icons.arrow_back_ios,
color: Get.isDarkMode ? Colors.white : darkGreyClr,
),
),
elevation: 0,
backgroundColor: context.theme.backgroundColor,
title: Text(
_payload.toString().split('|')[0],
style: TextStyle(color: Get.isDarkMode ? Colors.white : darkGreyClr),
),
),
body: SafeArea(
child: Column(
children: [
const SizedBox(height: 20),
Column(
children: [
Text(
'اهلا',
style: TextStyle(
fontSize: 26,
fontWeight: FontWeight.w900,
color: Get.isDarkMode ? Colors.white : darkGreyClr,
),
),
const SizedBox(height: 10),
Text(
'لديك تذكير جديد',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w300,
color: Get.isDarkMode ? Colors.grey[100] : darkGreyClr,
),
),
],
),
const SizedBox(height: 10),
Expanded(
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 30, vertical: 10),
margin: const EdgeInsets.symmetric(horizontal: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: primaryClr,
),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: const [
Icon(
Icons.text_format,
size: 35,
color: Colors.white,
),
SizedBox(width: 20),
Text(
'العنوان',
style: TextStyle(color: Colors.white, fontSize: 30),
),
],
),
const SizedBox(height: 20),
Text(
_payload.toString().split('|')[0],
style:
const TextStyle(color: Colors.white, fontSize: 20),
),
const SizedBox(height: 20),
Row(
children: const [
Icon(
Icons.description,
size: 35,
color: Colors.white,
),
SizedBox(width: 20),
Text(
'الوصف',
style: TextStyle(color: Colors.white, fontSize: 30),
),
],
),
const SizedBox(height: 20),
Text(
_payload.toString().split('|')[1],
style:
const TextStyle(color: Colors.white, fontSize: 20),
textAlign: TextAlign.justify,
),
const SizedBox(height: 20),
Row(
children: const [
Icon(
Icons.calendar_today_outlined,
size: 35,
color: Colors.white,
),
SizedBox(width: 20),
Text(
'التاريخ',
style: TextStyle(color: Colors.white, fontSize: 30),
),
],
),
const SizedBox(height: 20),
Text(
_payload.toString().split('|')[2],
style:
const TextStyle(color: Colors.white, fontSize: 20),
),
],
),
),
),
),
const SizedBox(height: 10),
],
),
),
);
}
}
notification_services
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:rxdart/rxdart.dart';
// ignore: depend_on_referenced_packages
import 'package:timezone/data/latest.dart' as tz;
// ignore: depend_on_referenced_packages
import 'package:timezone/timezone.dart' as tz;
import '/models/task.dart';
import '/ui/pages/notification_screen.dart';
class NotifyHelper {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin(); //
final BehaviorSubject<String> selectNotificationSubject =
BehaviorSubject<String>();
initializeNotification() async {
await _configureLocalTimeZone();
_configureSelectNotificationSubject();
final IOSInitializationSettings initializationSettingsIOS =
// ignore: prefer_const_constructors
IOSInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
final AndroidInitializationSettings initializationSettingsAndroid =
// ignore: prefer_const_constructors
AndroidInitializationSettings('#mipmap/ic_launcher');
final InitializationSettings initializationSettings =
InitializationSettings(
iOS: initializationSettingsIOS,
android: initializationSettingsAndroid,
);
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: (String? payload) async {
if (payload != null) {
debugPrint('notification payload: $payload');
}
selectNotificationSubject.add(payload!);
},
);
}
displayNotification({required String title, required String body}) async {
debugPrint('doing test');
var androidPlatformChannelSpecifics = const AndroidNotificationDetails(
'your channel id', 'your channel name',
channelDescription: 'your channel description',
importance: Importance.max,
priority: Priority.high);
var iOSPlatformChannelSpecifics = const IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
5,
title,
body,
platformChannelSpecifics,
payload: 'Default_Sound',
);
}
cancelNotification(Task task) async {
await flutterLocalNotificationsPlugin.cancel(task.id!);
}
cancelAllNotification() async {
await flutterLocalNotificationsPlugin.cancelAll();
}
scheduledNotification(int hour, int minutes, Task task) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
task.id!.toInt(),
task.title,
task.note!,
_convertTime(hour, minutes, task.repeat!, task.date!),
//tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
const NotificationDetails(
android: AndroidNotificationDetails(
'your channel id',
'your channel name',
channelDescription: 'your channel description',
),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
payload: '${task.title}|${task.note}|${task.startTime}|',
);
}
tz.TZDateTime _convertTime(
int hour, int minutes, String repeat, String date) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, minutes);
var formattedDate = DateFormat.yMd().parse(date);
if (scheduledDate.isBefore(now)) {
if (repeat == 'يوميا') {
scheduledDate = tz.TZDateTime(tz.local, now.year, now.month,
(formattedDate.day) + 1, hour, minutes);
}
if (repeat == 'اسبوعيا') {
scheduledDate = tz.TZDateTime(tz.local, now.year, now.month,
(formattedDate.day) + 7, hour, minutes);
}
if (repeat == 'شهريا') {
scheduledDate = tz.TZDateTime(tz.local, now.year,
(formattedDate.month) + 1, formattedDate.day, hour, minutes);
}
}
debugPrint('next scheduledDate = $scheduledDate');
return scheduledDate;
}
Future<void> _configureLocalTimeZone() async {
tz.initializeTimeZones();
final String timezone = await FlutterNativeTimezone.getLocalTimezone();
tz.setLocalLocation(
tz.getLocation(timezone),
);
}
void requestIOSPermissions() {
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
}
void _configureSelectNotificationSubject() {
selectNotificationSubject.stream.listen((String payload) async {
debugPrint('My payload is $payload');
await Get.to(() => NotificationScreen(payload: payload));
});
}
}
Future onDidReceiveLocalNotification(
int? id, String? title, String? body, String? payload) async {
Get.dialog(
const Text('welcome to flutter'),
);
}
add task page
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import '../../controllers/task_controller.dart';
import '../../models/task.dart';
import '../theme.dart';
import '../widgets/button.dart';
import '../widgets/input_field.dart';
class AddTaskPage extends StatefulWidget {
const AddTaskPage({Key? key}) : super(key: key);
#override
State<AddTaskPage> createState() => _AddTaskPageState();
}
class _AddTaskPageState extends State<AddTaskPage> {
final TaskController _taskController = Get.put(TaskController());
final TextEditingController _titleController = TextEditingController();
final TextEditingController _noteController = TextEditingController();
DateTime _selectedDate = DateTime.now();
String _startTime = DateFormat('hh:mm a')
.format(DateTime.now().add(const Duration(minutes: 1)))
.toString();
String _endTime = DateFormat('hh:mm a')
.format(DateTime.now().add(const Duration(minutes: 15)))
.toString();
String _selectedRepeat = 'بدون';
List<String> repeatList = ['بدون', 'يوميا', 'اسبوعيا', 'شهريا'];
int _selectedColor = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: context.theme.backgroundColor,
appBar: _appBar(),
body: Container(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: SingleChildScrollView(
child: Column(
children: [
Text('اضافة مهمة', style: headingStyle),
InputField(
title: 'عنوان',
hint: 'ادخل العنوان هنا',
controller: _titleController,
),
InputField(
title: 'ملاحظة',
hint: 'ادخل الملاحظة هنا',
controller: _noteController,
),
InputField(
title: 'التاريخ',
hint: DateFormat.yMd().format(_selectedDate),
widget: IconButton(
onPressed: () {
FocusScope.of(context).unfocus();
_getDateFromUser();
},
icon: const Icon(
Icons.calendar_today_outlined,
color: Colors.grey,
),
),
),
Row(
children: [
Expanded(
child: InputField(
title: 'بداية الوقت',
hint: _startTime,
widget: IconButton(
onPressed: () {
FocusScope.of(context).unfocus();
_getTimeFromUser(isStartTime: true);
},
icon: const Icon(
Icons.access_time_rounded,
color: Colors.grey,
),
),
),
),
const SizedBox(width: 12),
Expanded(
child: InputField(
title: 'نهاية الوقت',
hint: _endTime,
widget: IconButton(
onPressed: () {
FocusScope.of(context).unfocus();
_getTimeFromUser(isStartTime: false);
},
icon: const Icon(
Icons.access_time_rounded,
color: Colors.grey,
),
),
),
),
],
),
InputField(
title: 'اعادة',
hint: _selectedRepeat,
widget: Row(
children: [
DropdownButton(
dropdownColor: Colors.blueGrey,
borderRadius: BorderRadius.circular(10),
items: repeatList
.map<DropdownMenuItem<String>>(
(String value) => DropdownMenuItem<String>(
value: value,
child: Text(value,
style: const TextStyle(
color: Colors.white,
)),
),
)
.toList(),
icon: const Icon(Icons.keyboard_arrow_down,
color: Colors.grey),
iconSize: 32,
elevation: 4,
underline: Container(height: 0),
style: subTitleStyle,
onChanged: (String? newValue) {
setState(() {
_selectedRepeat = newValue!;
});
},
),
const SizedBox(width: 6),
],
),
),
const SizedBox(height: 13),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_colorPalette(),
MyButton(
label: 'اضافة',
onTap: () {
_validateDate();
},
),
],
),
const SizedBox(height: 8),
],
),
),
),
);
}
AppBar _appBar() {
return AppBar(
leading: IconButton(
onPressed: () => Get.back(),
icon: const Icon(
Icons.arrow_back_ios,
size: 24,
color: primaryClr,
),
),
elevation: 0,
backgroundColor: context.theme.backgroundColor,
actions: const [
CircleAvatar(
backgroundImage: AssetImage('images/person.jpeg'),
radius: 18,
),
SizedBox(width: 20),
],
);
}
_validateDate() {
FocusScope.of(context).unfocus();
if (_titleController.text.isNotEmpty && _noteController.text.isNotEmpty) {
_addTasksToDb();
Get.back();
} else if (_titleController.text.isEmpty || _noteController.text.isEmpty) {
Get.snackbar(
'مطلوب',
'!كل الحقول مطلوبة',
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.white,
colorText: pinkClr,
icon: const Icon(Icons.warning_amber_rounded, color: Colors.red),
);
} else {
debugPrint('############ SOMETHING BAD HAPPENED #################');
}
}
_addTasksToDb() async {
try {
//print('here is the selected month ${_selectedDate.month}');
//print('here is the selected day ${_selectedDate.day}');
int value = await _taskController.addTask(
task: Task(
title: _titleController.text,
note: _noteController.text,
isCompleted: 0,
date: DateFormat.yMd().format(_selectedDate),
startTime: _startTime,
endTime: _endTime,
color: _selectedColor,
repeat: _selectedRepeat,
),
);
debugPrint('id value = $value');
} catch (e) {
debugPrint('Error = $e');
}
}
Column _colorPalette() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('اللون', style: titleStyle),
const SizedBox(height: 8),
Wrap(
children: List<Widget>.generate(
3,
(index) => GestureDetector(
onTap: () {
setState(() {
_selectedColor = index;
});
},
child: Padding(
padding: const EdgeInsets.only(right: 8),
child: CircleAvatar(
child: _selectedColor == index
? const Icon(Icons.done, size: 16, color: Colors.white)
: null,
backgroundColor: index == 0
? primaryClr
: index == 1
? pinkClr
: orangeClr,
radius: 14,
),
),
),
),
),
],
);
}
_getDateFromUser() async {
DateTime? _pickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime(2030),
);
if (_pickedDate != null) {
setState(() => _selectedDate = _pickedDate);
} else {
debugPrint('It\'s null or something is wrong');
}
}
_getTimeFromUser({required bool isStartTime}) async {
TimeOfDay? _pickedTime = await showTimePicker(
initialEntryMode: TimePickerEntryMode.input,
context: context,
initialTime: isStartTime
? TimeOfDay.fromDateTime(DateTime.now())
: TimeOfDay.fromDateTime(
DateTime.now().add(const Duration(minutes: 15))),
);
String _formattedTime = _pickedTime!.format(context);
if (isStartTime) {
setState(() => _startTime = _formattedTime);
} else if (!isStartTime) {
setState(() => _endTime = _formattedTime);
} else {
debugPrint('time canceld or something is wrong');
}
}
}
main screan
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import '/ui/theme.dart';
import 'db/db_helper.dart';
import 'services/theme_services.dart';
import 'ui/pages/home_page.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await DBHelper.initDb();
await GetStorage.init();
runApp(
const MyApp(),
);
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return GetMaterialApp(
theme: Themes.light,
darkTheme: Themes.dark,
themeMode: ThemeServices().theme,
title: 'To Do',
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
I am using a function _showNotificationsAfterSecond to get notifications after a delay of 3 seconds. I tried to make a for loop to run this function 8 times so that I get 8 notifications after a delay of 3 seconds each. Upon pressing the "Start Reminders" button (which calls the function), I only get one notification and after that nothing.
What am I doing wrong?
Here's my code:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: LocalNotifications(),
);
}
}
class LocalNotifications extends StatefulWidget {
#override
_LocalNotificationsState createState() => _LocalNotificationsState();
}
class _LocalNotificationsState extends State<LocalNotifications> {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
AndroidInitializationSettings androidInitializationSettings;
IOSInitializationSettings iosInitializationSettings;
InitializationSettings initializationSettings;
#override
void initState() {
super.initState();
initializing();
}
void initializing() async {
androidInitializationSettings =
AndroidInitializationSettings('mipmap/ic_launcher');
iosInitializationSettings = IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
initializationSettings = InitializationSettings(
androidInitializationSettings, iosInitializationSettings);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
}
void _showNotificationsAfterSecond() async {
for( int i = 0 ; i < 8 ; i ++) {
notificationAfterSec();
}
}
Future<Null> delay(int milliseconds) {
return new Future.delayed(new Duration(milliseconds: milliseconds));
}
Future<void> notificationAfterSec() async {
var timeDelayed = DateTime.now().add(Duration(seconds: 3));
AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
'second id', 'second heading', 'second text',
priority: Priority.High,
importance: Importance.Max,
ticker: 'test');
IOSNotificationDetails iosNotificationDetails = IOSNotificationDetails();
NotificationDetails notificationDetails =
NotificationDetails(androidNotificationDetails, iosNotificationDetails);
await flutterLocalNotificationsPlugin.schedule(
1, 'yo', 'wfqawsfqsqacqfqcwefs', timeDelayed, notificationDetails);
}
Future<void> turnOffNotification(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
await flutterLocalNotificationsPlugin.cancelAll();
}
Future onSelectNotification(String payLoad) {
if (payLoad != null) {
print(payLoad);
}
// we can set navigator to navigate another screen
}
Future onDidReceiveLocalNotification(
int id, String title, String body, String payload) async {
return CupertinoAlertDialog(
title: Text(title),
content: Text(body),
actions: <Widget>[
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () {
print("");
},
child: Text("Okay")),
],
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Hydration"),
actions: <Widget>[
FlatButton(
textColor: Colors.white,
onPressed: () {
DialogUtils.showCustomDialog(context,
title: "Why should this be a priority?",
okBtnText: "Got It!",
okBtnFunction: () => Navigator.pop(context));
},
child: Icon(Icons.info_outline),
shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
),
],
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/wat.jfif'), fit: BoxFit.cover)),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
padding: EdgeInsets.all(20.0),
margin: EdgeInsets.fromLTRB(20.0, 30.0, 20.0, 0.0),
child: Column(
children: <Widget>[
Text(
'Water Intake Reminder',
style: TextStyle(color: Colors.white, fontSize: 30),
textAlign: TextAlign.center,
),
Padding(
padding: EdgeInsets.symmetric(
vertical: 20.0, horizontal: 20.0)),
Container(
child: FittedBox(
child: Image.asset(
'assets/drinking.jpg',
width: 300,
height: 300,
), //adjust this baad mein
fit: BoxFit.fill,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
),
),
],
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
color: Colors.blueAccent[100],
),
height: 400.0,
width: 300.0,
),
Padding(padding: EdgeInsets.fromLTRB(8.0, 20.0, 8.0, 8.0)),
Padding(padding: EdgeInsets.fromLTRB(8.0, 1.0, 8.0, 1.0)),
Row(
children: <Widget>[
Padding(padding: EdgeInsets.fromLTRB(9.0, 9.0, 9.0, 9.0)),
FloatingActionButton.extended(
heroTag: "btn1",
onPressed: _showNotificationsAfterSecond,
label: Text(
"Start Reminders",
style: TextStyle(fontSize: 16.0, color: Colors.white),
),
),
Padding(padding: EdgeInsets.fromLTRB(5.0, 5.0, 5.0, 5.0)),
FloatingActionButton.extended(
heroTag: "btn2",
onPressed: () {
turnOffNotification(flutterLocalNotificationsPlugin);
},
label: Text(
"Stop Reminders",
style: TextStyle(fontSize: 16.0, color: Colors.white),
),
),
Padding(padding: EdgeInsets.fromLTRB(9.0, 9.0, 9.0, 9.0)),
],
),
],
),
),
);
}
}
class DialogUtils {
static DialogUtils _instance = new DialogUtils.internal();
DialogUtils.internal();
factory DialogUtils() => _instance;
static void showCustomDialog(BuildContext context,
{#required String title,
String okBtnText = "hhegregrbge",
#required Function okBtnFunction}) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
backgroundColor: Colors.deepPurpleAccent[100],
title: Text(
title,
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
content: Text(
" When you're pregnant, you need more water than the average person in order to form amniotic fluid, produce extra blood, build newtissue, carry nutrients, enhance digestion, and flush out wastes and toxins.Since you need more water during pregnancy, how much is enough? It’s recommended that you drink 8-12 glasses of water a day, or 2.3 liters.",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white70),
),
actions: <Widget>[
FlatButton(
child: Text(
okBtnText,
style: TextStyle(color: Colors.white),
),
onPressed: okBtnFunction,
),
],
);
});
}
}
You can copy paste run full code below
You need to use different id, you can pass i as schedule id
code snippet
void _showNotificationsAfterSecond() async {
for (int i = 0; i < 8; i++) {
notificationAfterSec(i);
}
}
Future<void> notificationAfterSec(int id) async {
...
await flutterLocalNotificationsPlugin.schedule(id, '${id} yo',
'wfqawsfqsqacqfqcwefs', timeDelayed, notificationDetails);
working demo
full code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: LocalNotifications(),
);
}
}
class LocalNotifications extends StatefulWidget {
#override
_LocalNotificationsState createState() => _LocalNotificationsState();
}
class _LocalNotificationsState extends State<LocalNotifications> {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
AndroidInitializationSettings androidInitializationSettings;
IOSInitializationSettings iosInitializationSettings;
InitializationSettings initializationSettings;
#override
void initState() {
super.initState();
initializing();
}
void initializing() async {
androidInitializationSettings =
AndroidInitializationSettings('mipmap/ic_launcher');
iosInitializationSettings = IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
initializationSettings = InitializationSettings(
androidInitializationSettings, iosInitializationSettings);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
}
void _showNotificationsAfterSecond() async {
for (int i = 0; i < 8; i++) {
notificationAfterSec(i);
}
}
Future<Null> delay(int milliseconds) {
return new Future.delayed(new Duration(milliseconds: milliseconds));
}
Future<void> notificationAfterSec(int id) async {
var timeDelayed = DateTime.now().add(Duration(seconds: 3));
AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
'second id', 'second heading', 'second text',
priority: Priority.High,
importance: Importance.Max,
ticker: 'test');
IOSNotificationDetails iosNotificationDetails = IOSNotificationDetails();
NotificationDetails notificationDetails =
NotificationDetails(androidNotificationDetails, iosNotificationDetails);
await flutterLocalNotificationsPlugin.schedule(id, '${id} yo',
'wfqawsfqsqacqfqcwefs', timeDelayed, notificationDetails);
}
Future<void> turnOffNotification(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
await flutterLocalNotificationsPlugin.cancelAll();
}
Future onSelectNotification(String payLoad) {
if (payLoad != null) {
print(payLoad);
}
// we can set navigator to navigate another screen
}
Future onDidReceiveLocalNotification(
int id, String title, String body, String payload) async {
return CupertinoAlertDialog(
title: Text(title),
content: Text(body),
actions: <Widget>[
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () {
print("");
},
child: Text("Okay")),
],
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Hydration"),
actions: <Widget>[
FlatButton(
textColor: Colors.white,
onPressed: () {
DialogUtils.showCustomDialog(context,
title: "Why should this be a priority?",
okBtnText: "Got It!",
okBtnFunction: () => Navigator.pop(context));
},
child: Icon(Icons.info_outline),
shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
),
],
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage('https://picsum.photos/250?image=9'),
fit: BoxFit.cover)),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
padding: EdgeInsets.all(20.0),
margin: EdgeInsets.fromLTRB(20.0, 30.0, 20.0, 0.0),
child: Column(
children: <Widget>[
Text(
'Water Intake Reminder',
style: TextStyle(color: Colors.white, fontSize: 30),
textAlign: TextAlign.center,
),
Padding(
padding: EdgeInsets.symmetric(
vertical: 20.0, horizontal: 20.0)),
Container(
child: FittedBox(
child: Image.network(
'https://picsum.photos/250?image=9',
width: 300,
height: 300,
), //adjust this baad mein
fit: BoxFit.fill,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
),
),
],
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
color: Colors.blueAccent[100],
),
height: 400.0,
width: 300.0,
),
Padding(padding: EdgeInsets.fromLTRB(8.0, 20.0, 8.0, 8.0)),
Padding(padding: EdgeInsets.fromLTRB(8.0, 1.0, 8.0, 1.0)),
Row(
children: <Widget>[
Padding(padding: EdgeInsets.fromLTRB(9.0, 9.0, 9.0, 9.0)),
FloatingActionButton.extended(
heroTag: "btn1",
onPressed: _showNotificationsAfterSecond,
label: Text(
"Start Reminders",
style: TextStyle(fontSize: 16.0, color: Colors.white),
),
),
Padding(padding: EdgeInsets.fromLTRB(5.0, 5.0, 5.0, 5.0)),
FloatingActionButton.extended(
heroTag: "btn2",
onPressed: () {
turnOffNotification(flutterLocalNotificationsPlugin);
},
label: Text(
"Stop Reminders",
style: TextStyle(fontSize: 16.0, color: Colors.white),
),
),
Padding(padding: EdgeInsets.fromLTRB(9.0, 9.0, 9.0, 9.0)),
],
),
],
),
),
);
}
}
class DialogUtils {
static DialogUtils _instance = new DialogUtils.internal();
DialogUtils.internal();
factory DialogUtils() => _instance;
static void showCustomDialog(BuildContext context,
{#required String title,
String okBtnText = "hhegregrbge",
#required Function okBtnFunction}) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
backgroundColor: Colors.deepPurpleAccent[100],
title: Text(
title,
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
content: Text(
" When you're pregnant, you need more water than the average person in order to form amniotic fluid, produce extra blood, build newtissue, carry nutrients, enhance digestion, and flush out wastes and toxins.Since you need more water during pregnancy, how much is enough? It’s recommended that you drink 8-12 glasses of water a day, or 2.3 liters.",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white70),
),
actions: <Widget>[
FlatButton(
child: Text(
okBtnText,
style: TextStyle(color: Colors.white),
),
onPressed: okBtnFunction,
),
],
);
});
}
}