Я настраиваю локальное уведомление о флаттере, все идет хорошо, но я хочу сохранить выбранное значение в тексте, чтобы при перезапуске приложения оно отображало выбранное значение, а не сбрасывалось до значения по умолчанию. Например, если я выберу уведомление в 08:08:08, поэтому после его выбора и перезапуска приложения при каждом переходе ко времени выбора уведомления должно отображаться 08:08:08 (последнее выбранное значение).
я пытался установить состояние() {};но значение всегда сбрасывается после перезапуска приложения. вот мой код.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:fluttertoast/fluttertoast.dart';
class SettingPage extends StatefulWidget {
@override
_SettingPage createState() => _SettingPage();
}
class _SettingPage extends State<SettingPage> {
///Flutter Local Notification
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
String notifyTime ="your time";
String nhour ="";
String nminutes ="";
String nseconds ="";
@override
initState() {
super.initState();
// initialise the plugin. app_icon needs to be a added as a drawable
resource to the Android head project
// If you have skipped STEP 3 then change app_icon to @mipmap/ic_launcher
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
Future _showNotificationWithDefaultSound() async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max, priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'Learn Local Notification',
'A Local Notification On Button Click',
platformChannelSpecifics,
payload: 'Default_Sound',
);
}
Future scheuleAtParticularTime(DateTime timee) async {
var time = Time(timee.hour, timee.minute, timee.second);
print(time.toString());
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'repeatDailyAtTime channel id',
'repeatDailyAtTime channel name',
'repeatDailyAtTime description');
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
flutterLocalNotificationsPlugin.showDailyAtTime(0, 'Hey! Check your
today`s horoscope ',
'Login now to see your today`s horoscope !', time,
platformChannelSpecifics);
print('scheduled');
Fluttertoast.showToast(
msg:
"Notification Scheduled for ${time.hour} : ${time.minute} :
${time.second}",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
// also possible "TOP" and "CENTER"
backgroundColor: Colors.grey,
textColor: Colors.white);
setState(() {
nhour = time.hour.toString();
nminutes=time.minute.toString();
nseconds=time.second.toString();
notifyTime=nhour+" : "+nminutes+" : "+nseconds;
});
}
//function ends
@override
Widget build(BuildContext context) {
return Scaffold(backgroundColor: Theme.of(context).primaryColor,
appBar: AppBar(backgroundColor: Theme.of(context).primaryColor,
leading: IconButton(icon: Icon(FontAwesomeIcons.arrowLeft,
//backgroundColor: Theme.of(context).primaryColor,
), onPressed: () => {
Navigator.pop(context),}),
title: Text('Setting'),
),
body: Theme( data: Theme.of(context).copyWith(
canvasColor: Theme.of(context).primaryColor, //This will change the drawer background to blue.),
child: Center(
child: Container(
height: MediaQuery
.of(context)
.size
.height - 60.0,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 60.0),
child: RaisedButton(
color: Color(0xffffffff),
child: Text('Notification time - $notifyTime',////NEED TO STORE LAST VALUE HERE////////
style: TextStyle(
color: Color(0xff6200ee),
),),
onPressed: () {
DatePicker.showTimePicker(context, showTitleActions: true,
onChanged: (date) {
print('change $date');
}, onConfirm: (date) {
print('confirm $date');
scheuleAtParticularTime(
DateTime.fromMillisecondsSinceEpoch(
date.millisecondsSinceEpoch));
}, currentTime: DateTime.now(), locale: LocaleType.en);
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
30.0),)
),
),
],),
),
);
}
}