Долго вожусь с уведомлениями и никак не могу реализовать. В коде пытался, чтобы при нажатии на кнопку стартовала задержка в 10 секунд, а потом показывалось уведомление(у меня не работает). В идеале пытаюсь добавить checkbox, который при активном состоянии будет выводить уведомления автоматически каждый час.
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:todo/main.dart';
import 'package:todo/theme.dart';
import 'package:provider/provider.dart';
class ChangeTheme extends StatefulWidget{
ChangeTheme({Key key, this.title}): super(key: key);
final String title;
@override
MyState createState() => new MyState();
}
class MyState extends State<ChangeTheme>{
bool val = false;
String message = "смена темы";
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
@override
void initState() {
super.initState();
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
var iOS = new IOSInitializationSettings();
var initSetttings = new InitializationSettings(android, iOS);
flutterLocalNotificationsPlugin.initialize(initSetttings,
onSelectNotification: onSelectNotification);
}
// Future onSelectNotification(String payload) async {
// if (payload != null) {
// print('notification payload: ' + payload);
// }
//
// await Navigator.push(
// context,
// MaterialPageRoute(builder: (context) => SecondScreen(payload)),
// );
// }
Future onSelectNotification(String payload) async {
await Navigator.push(
context,
MaterialPageRoute(builder: (context) => TodoList()),
);
// debugPrint("payload : $payload");
// showDialog(
// context: context,
// builder: (_) => new AlertDialog(
// title: new Text('Notification'),
// content: new Text('$payload'),
// ),
// );
}
// await flutterLocalNotificationsPlugin.show(
// 0, 'plain title', 'plain body', platformChannelSpecifics,
// payload: 'item x');
// String trendingNewsId = '5';
// await flutterLocalNotificationsPlugin.show(
// 0, 'Trending News', 'Donald trump says windmill cause cancer. ', platformChannelSpecifics,
// payload: trendingNewsId);
// }
@override
Widget build(BuildContext context){
// _getPrefs();
return new Scaffold(
appBar: AppBar(
title: Text('Настройки'),
),
body: Container(
child: Column(
children: <Widget>[
new Switch(
value: val,
onChanged: (bool e) => something(e),
),
new Text (message),
new RaisedButton(
onPressed: lol,
child: new Text(
'Demo',
style: Theme.of(context).textTheme.headline,
),
),
],
),
),
// floatingActionButton: FloatingActionButton(
// //onPressed: _incrementCounter,
// onPressed: _showNotification,
// tooltip: 'Increment',
// child: Icon(Icons.add),
// ), // Thi
);
}
void something(bool e) {
ThemeChanger _themeChanger = Provider.of<ThemeChanger>(context);
//_setPrefs();
setState(() {
if (e){
_themeChanger.setTheme(ThemeData.dark());
message = "Темнаяя тема";
val = e;
} else {
_themeChanger.setTheme(ThemeData.light());
message = "Светлая тема";
val = e;
}
});
}
// showNotification() async {
// var android = new AndroidNotificationDetails(
// 'channel id', 'channel NAME', 'CHANNEL DESCRIPTION',
// priority: Priority.High,importance: Importance.Max
// );
// var iOS = new IOSNotificationDetails();
// var platform = new NotificationDetails(android, iOS);
// await flutterLocalNotificationsPlugin.show(
// 0, 'Список задач', 'остались невыполненные задачи!', platform,
// payload: 'Nitish Kumar Singh is part time Youtuber');
// }
lol() async {
var scheduledNotificationDateTime =
new DateTime.now().add(new Duration(seconds: 10));
var androidPlatformChannelSpecifics =
new AndroidNotificationDetails('your other channel id',
'your other channel name', 'your other channel description');
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.schedule(
0,
'scheduled title',
'scheduled body',
scheduledNotificationDateTime,
platformChannelSpecifics);
}
// void _setPrefs() async {
// SharedPreferences prefs = await SharedPreferences.getInstance();
// prefs.setBool('Switch', val);
// }
// void _getPrefs() async {
// SharedPreferences prefs = await SharedPreferences.getInstance();
// if (prefs.getBool('Switch') != null)
// val = prefs.getBool('Switch');
// }
}