Dart 返回时,按按钮回拨功能在颤振状态下不工作
大家好,我一直在尝试在颤振中实现Willposcope,但此回调功能仍然不起作用,它仍然关闭或返回到上一屏幕,并且我希望在用户按下后退按钮时显示警报对话框,但它没有显示对话框,而是返回到上一屏幕。请帮助我哪里做错了 编辑代码Dart 返回时,按按钮回拨功能在颤振状态下不工作,dart,flutter,Dart,Flutter,大家好,我一直在尝试在颤振中实现Willposcope,但此回调功能仍然不起作用,它仍然关闭或返回到上一屏幕,并且我希望在用户按下后退按钮时显示警报对话框,但它没有显示对话框,而是返回到上一屏幕。请帮助我哪里做错了 编辑代码 import 'package:deep6/audio_player/click_player.dart'; import 'package:deep6/main_menu.dart'; import 'package:deep6/utility/countDown.dar
import 'package:deep6/audio_player/click_player.dart';
import 'package:deep6/main_menu.dart';
import 'package:deep6/utility/countDown.dart';
import 'package:deep6/utility/preference.dart';
import 'package:flutter/material.dart';
import
'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SceneStory extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
home: Scene(
character: null,
text: null,
mainTextColor: null,
avatar: null,
lbText: null,
lbTextTarget: null,
lbTextColor: null,
rbText: null,
rbTextTarget: null,
rbTextColor: null,
),
);
}
}
class Scene extends StatefulWidget {
List<String> character = new List<String>();
List<String> text = new List<String>();
List<List<int>> mainTextColor = new List<List<int>>();
List<String> avatar = new List<String>();
List<String> lbText = new List<String>();
List<int> lbTextTarget = new List<int>();
List<List<int>> lbTextColor = new List<List<int>>();
List<String> rbText = new List<String>();
List<int> rbTextTarget = new List<int>();
List<List<int>> rbTextColor = new List<List<int>>();
Scene(
{Key key,
this.character,
this.text,
this.mainTextColor,
this.avatar,
this.lbText,
this.lbTextTarget,
this.lbTextColor,
this.rbText,
this.rbTextTarget,
this.rbTextColor})
: super(key: key);
@override
SceneState createState() => new SceneState();
}
String name, text, buttonLeftText, buttonRightText;
String avatar = 'assets/images/profile_cure.png';
int sceneId, leftTarget, rightTarget;
class SceneState extends State<Scene> with TickerProviderStateMixin {
double width, height;
List<String> content = new List<String>();
CountDown customTimer;
PrefsKeys prefs;
int mainIndex = 0;
bool isEnabled = false;
List<String> character = new List<String>();
List<String> text = new List<String>();
List<List<int>> mainTextColor = new List<List<int>>();
List<String> avatar = new List<String>();
List<String> lbText = new List<String>();
List<int> lbTextTarget = new List<int>();
List<List<int>> lbTextColor = new List<List<int>>();
List<String> rbText = new List<String>();
List<int> rbTextTarget = new List<int>();
List<List<int>> rbTextColor = new List<List<int>>();
Image charImage;
Text charName, mainText, leftText, rightText;
MaterialButton leftButton, rightButton;
ClickPlayer cPlay;
AnimationController controller;
@override
void initState() {
super.initState();
character = widget.character;
text = widget.text;
mainTextColor = widget.mainTextColor;
avatar = widget.avatar;
lbText = widget.lbText;
lbTextTarget = widget.lbTextTarget;
lbTextColor = widget.lbTextColor;
rbText = widget.rbText;
rbTextTarget = widget.rbTextTarget;
rbTextColor = widget.rbTextColor;
animation();
cPlay = new ClickPlayer();
preference();
}
void preference() async {
SharedPreferences pref = await SharedPreferences.getInstance().then((pref)
{
pref.setBool(PrefsKeys.bool_continuation, true);
});
}
void animation() async {
controller = new AnimationController(
duration: const Duration(milliseconds: 4000), vsync: this);
await controller.forward();
controller.dispose();
}
Future onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => MenuScreen(),
),
);
print("Notifications is Clicked $payload");
}
void showNotification() async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
var initializationSettingsAndroid;
var initializationSettingsIOS;
var initializationSettings;
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
initializationSettingsAndroid =
new AndroidInitializationSettings('ic_launcher');
initializationSettingsIOS = new IOSInitializationSettings();
initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
selectNotification: onSelectNotification);
var scheduledNotificationDateTime =
new DateTime.now().add(new Duration(minutes: 2));
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'0', 'Anas', 'Notification',
importance: Importance.Max, priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.schedule(
0,
'DEEP 6',
'NOW YOU CAN CONTINUE.',
scheduledNotificationDateTime,
platformChannelSpecifics);
print("Inside Notifications class");
}
void leftButtonClick() async {
cPlay.playClick();
mainIndex = lbTextTarget[mainIndex];
print("$mainIndex mainindex");
if (mainIndex == 18) {
showNotification();
SharedPreferences pref =
await SharedPreferences.getInstance().then((pref) {
pref.setInt(
PrefsKeys.str_timestamp, new
DateTime.now().millisecondsSinceEpoch);
pref.setString(PrefsKeys.str_character, character[mainIndex]);
pref.setString(PrefsKeys.str_mainText, text[mainIndex]);
pref.setString(PrefsKeys.str_avatar, avatar[mainIndex]);
pref.setString(PrefsKeys.str_lbText, lbText[mainIndex]);
List<String> color = new List<String>();
color.add(mainTextColor[mainIndex][0].toString());
color.add(mainTextColor[mainIndex][1].toString());
color.add(mainTextColor[mainIndex][2].toString());
pref.setStringList(PrefsKeys.list_color, color);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => MainMenu(),
),
);
});
} else {
isEnabled = false;
setState(() {
charImage = null;
charName = null;
mainText = null;
leftText = null;
rightText = null;
charImage = new Image.asset(
avatar[mainIndex],
fit: BoxFit.contain,
height: height * 0.2,
);
charName = new Text(
character[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(mainTextColor[mainIndex][0],
mainTextColor[mainIndex][1], mainTextColor[mainIndex][2],
1.0),
fontSize: 20.0,
),
);
mainText = new Text(
text[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(mainTextColor[mainIndex][0],
mainTextColor[mainIndex][1], mainTextColor[mainIndex][2],
1.0),
fontSize: 22.0,
),
textAlign: TextAlign.start,
);
animation();
leftText = new Text(
lbText[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(lbTextColor[mainIndex][0],
lbTextColor[mainIndex][1], lbTextColor[mainIndex][2], 1.0)),
textAlign: TextAlign.center,
softWrap: true,
maxLines: 10,
);
if (rbText[mainIndex] == "") {
rightText = null;
rightButton = new MaterialButton(
key: null,
onPressed: () {},
color: Color.fromRGBO(0, 0, 0, 0.8),
height: 0.0,
minWidth: 0.0,
child: null,
);
} else {
rightText = new Text(
rbText[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(rbTextColor[mainIndex][0],
rbTextColor[mainIndex][1], rbTextColor[mainIndex][2], 1.0),
),
textAlign: TextAlign.center,
);
}
});
}
}
void rightButtonClick() async {
cPlay.playClick();
mainIndex = rbTextTarget[mainIndex];
print("$mainIndex mainindex");
if (mainIndex == 18) {
showNotification();
SharedPreferences pref =
await SharedPreferences.getInstance().then((pref) {
pref.setInt(
PrefsKeys.str_timestamp, new DateTime.now().millisecondsSinceEpoch);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => MainMenu(),
),
);
});
} else {
isEnabled = false;
setState(() {
charImage = null;
charName = null;
mainText = null;
leftText = null;
rightText = null;
charImage = new Image.asset(
avatar[mainIndex],
fit: BoxFit.contain,
height: height * 0.2,
);
charName = new Text(
character[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(mainTextColor[mainIndex][0],
mainTextColor[mainIndex][1], mainTextColor[mainIndex][2], 1.0),
fontSize: 22.0,
),
);
mainText = new Text(
text[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(mainTextColor[mainIndex][0],
mainTextColor[mainIndex][1], mainTextColor[mainIndex][2], 1.0),
fontSize: 20.0,
),
textAlign: TextAlign.start,
);
animation();
leftText = new Text(
lbText[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(lbTextColor[mainIndex][0],
lbTextColor[mainIndex][1], lbTextColor[mainIndex][2], 1.0)),
textAlign: TextAlign.center,
softWrap: true,
maxLines: 10,
);
if (rbText[mainIndex] == "") {
rightText = null;
rightButton = new MaterialButton(
key: null,
onPressed: () {},
color: Color.fromRGBO(0, 0, 0, 0.8),
height: 0.0,
minWidth: 0.0,
child: null,
);
} else {
rightText = new Text(
rbText[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(rbTextColor[mainIndex][0],
rbTextColor[mainIndex][1], rbTextColor[mainIndex][2], 1.0),
),
textAlign: TextAlign.center,
);
}
});
}
}
Future<bool> _showDialog() {
print("Show dialog");
return showDialog(
context: context,
builder: (BuildContext contxt) {
return AlertDialog(
title: new Text(
"Exit!!!",
style: TextStyle(color: Colors.black),
),
content: new Text(
"Are You sure you want to exit?",
style: TextStyle(color: Colors.black),
),
actions: <Widget>[
new FlatButton(
child: new Text(
"Yes",
style: TextStyle(color: Colors.blue),
),
onPressed: null,
)
],
);
});
}
@override
Widget build(BuildContext context) {
final mediaQueryData = MediaQuery.of(context);
width = mediaQueryData.size.width;
height = mediaQueryData.size.height;
Animation<int> _characterCount = new StepTween(
begin: 0, end: text[mainIndex].length)
.animate(new CurvedAnimation(parent: controller, curve: Curves.easeIn));
controller.addStatusListener((status) {
if (status == AnimationStatus.completed) {
isEnabled = true;
}
});
charImage = new Image.asset(
avatar[mainIndex],
fit: BoxFit.contain,
height: height * 0.2,
);
charName = new Text(
character[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(mainTextColor[mainIndex][0],
mainTextColor[mainIndex][1], mainTextColor[mainIndex][2], 1.0),
fontSize: 22.0,
),
);
mainText = new Text(
text[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(mainTextColor[mainIndex][0],
mainTextColor[mainIndex][1], mainTextColor[mainIndex][2], 1.0),
fontSize: 20.0,
),
textAlign: TextAlign.start,
);
leftText = new Text(
lbText[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(lbTextColor[mainIndex][0],
lbTextColor[mainIndex][1], lbTextColor[mainIndex][2], 1.0)),
textAlign: TextAlign.center,
softWrap: true,
maxLines: 10,
);
rightText = new Text(
rbText[mainIndex],
style: new TextStyle(
color: Color.fromRGBO(rbTextColor[mainIndex][0],
rbTextColor[mainIndex][1], rbTextColor[mainIndex][2], 1.0),
),
textAlign: TextAlign.center,
);
leftButton = new MaterialButton(
key: null,
onPressed: () {
if (isEnabled) {
leftButtonClick();
}
},
color: Color.fromRGBO(0, 0, 0, 0.8),
height: height * 0.2,
minWidth: width * 0.49,
child: new Container(
child: leftText,
),
);
rightButton = new MaterialButton(
key: null,
onPressed: () {
if (isEnabled) {
rightButtonClick();
}
},
color: Color.fromRGBO(0, 0, 0, 0.8),
height: height * 0.2,
minWidth: width * 0.49,
child: rightText,
);
return new WillPopScope(
child: new Scaffold(
backgroundColor: new Color(0xFF000000),
body: new Container(
height: double.infinity,
width: double.infinity,
decoration: new BoxDecoration(
image: DecorationImage(
image: new AssetImage('assets/images/blury_background.png'),
fit: BoxFit.fill)),
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
charImage,
new Container(
padding: const EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 5.0),
child: new Center(
child: charName,
),
color: Color.fromRGBO(0, 0, 0, 0.8),
),
new AnimatedBuilder(
animation: _characterCount,
builder: (BuildContext context, Widget child) {
String txt =
text[mainIndex].substring(0, _characterCount.value);
return new Container(
color: Color.fromRGBO(0, 0, 0, 0.8),
child: new Text(
txt,
style: new TextStyle(
color: Color.fromRGBO(
mainTextColor[mainIndex][0],
mainTextColor[mainIndex][1],
mainTextColor[mainIndex][2],
1.0),
fontSize: 20.0,
),
textAlign: TextAlign.start,
),
padding: const EdgeInsets.all(20.0),
//fromLTRB(12.0, 0.0, 5.0, 0.0),
height: height * 0.5,
);
}),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Container(
child: leftButton,
),
new Container(
child: rightButton,
),
],
),
]),
),
),
onWillPop: () {
print("on back is pressed");
},
);
}
}
import'程序包:deep6/audio_player/点击_player.dart';
导入“包:deep6/main_menu.dart”;
导入“包:deep6/utility/countDown.dart”;
导入“包:deep6/utility/preference.dart”;
进口“包装:颤振/材料.省道”;
进口
'package:flatter_local_notifications/flatter_local_notifications.dart';
导入“package:shared_preferences/shared_preferences.dart”;
类SceneStorey扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新材料PP(
debugShowCheckedModeBanner:false,
家:场景(
字符:空,
文本:空,
mainTextColor:null,
阿凡达:空,
lbText:null,
lbTextTarget:null,
lbTextColor:null,
rbText:null,
rbTextTarget:null,
rbTextColor:null,
),
);
}
}
类场景扩展StatefulWidget{
列表字符=新列表();
列表文本=新列表();
List mainTextColor=新列表();
列表化身=新列表();
List lbText=新列表();
List lbTextTarget=新列表();
List lbTextColor=新列表();
List rbText=新列表();
List rbTextTarget=新列表();
List rbTextColor=新列表();
场面(
{键,
这个人物,,
这个文本,
这个.mainTextColor,
这个,阿凡达,
这个.lbText,
这个.lbTextTarget,
这个.lbTextColor,
这个.rbText,
这个.rbTextTarget,
这个.rbTextColor})
:super(key:key);
@凌驾
SceneState createState()=>新建SceneState();
}
字符串名称、文本、buttonLeftText、buttonRightText;
字符串avatar='assets/images/profile_cure.png';
int sceneId,leftTarget,rightTarget;
类SceneState使用TickerProviderStateMixin扩展状态{
双宽、双高;
列表内容=新列表();
倒数计时器;
Prefskys prefs;
int main index=0;
bool isEnabled=false;
列表字符=新列表();
列表文本=新列表();
List mainTextColor=新列表();
列表化身=新列表();
List lbText=新列表();
List lbTextTarget=新列表();
List lbTextColor=新列表();
List rbText=新列表();
List rbTextTarget=新列表();
List rbTextColor=新列表();
图像处理;
文本字符名、主文本、左文本、右文本;
物料按钮左按钮、右按钮;
点击播放器cPlay;
动画控制器;
@凌驾
void initState(){
super.initState();
character=widget.character;
text=widget.text;
mainTextColor=widget.mainTextColor;
化身=widget.avatar;
lbText=widget.lbText;
lbTextTarget=widget.lbTextTarget;
lbTextColor=widget.lbTextColor;
rbText=widget.rbText;
rbTextTarget=widget.rbTextTarget;
rbTextColor=widget.rbTextColor;
动画();
cPlay=新的ClickPlayer();
偏好();
}
void preference()异步{
SharedReferences pref=等待SharedReferences.getInstance()。然后((pref)
{
pref.setBool(PrefsKeys.bool_continuation,true);
});
}
void animation()异步{
控制器=新的AnimationController(
持续时间:常量持续时间(毫秒:4000),vsync:this;
等待控制器.forward();
controller.dispose();
}
未来onSelectNotification(字符串负载)异步{
如果(有效负载!=null){
debugPrint('通知有效负载:'+有效负载);
}
导航器。更换(
上下文
材料路线(
生成器:(上下文)=>MenuScreen(),
),
);
打印(“点击通知$payload”);
}
void showNotification()异步{
flatterlocalnotificationsplugin flatterlocalnotificationsplugin;
var初始化设置和ROID;
var初始化设置SIOS;
var初始化设置;
FlatterLocalNotificationsPlugin=新的FlatterLocalNotificationsPlugin();
初始化设置和ROID=
新的AndroidInitializationSettings(“ic_启动器”);
initializationSettingsIOS=新IOSSInitializationSettings();
initializationSettings=新的initializationSettings(
初始化设置和ROID,初始化设置SIOS);
FlatterLocalNotificationsPlugin.initialize(初始化设置,
选择通知:选择通知);
变量scheduledNotificationDateTime=
new DateTime.now().add(新的持续时间(分钟:2));
var androidPlatformChannelSpecifics=新的AndroidNotificationDetails(
“0”、“Anas”、“通知”,
重要性:重要性。最大,优先级:优先级。高);
var iOSPlatformChannelSpecifics=新的IOSNotificationDetails();
var platformChannelSpecifics=新通知详细信息(
androidPlatformChannelSpecifics、iOSPlatformChannelSpecifics);
等待通知日程安排(
0,
"深6",,
“现在你可以继续了。”,
scheduledNotificationDateTime,
平台(具体信息);
打印(“内部通知类”);
}
void leftButtonClick()异步{
cPlay.playClick();
mainIndex=lbTextTarget[mainIndex];
打印($mainIndex mainIndex);
如果(主索引==18){
showNotification();
共享引用优先=
等待SharedReferences.getInstance()。然后((pref){
pref.setInt(
PrefsKeys.str_时间戳,新
DateTime.now().millissecondssinceepoch);
pref.setString(PrefsKeys.str_字符,字符[mainIndex]);
pref.setString(PrefsKeys.str_mainText,text[mainIndex]);
pref.setString(PrefsKeys.str_头像,头像[maindex]);
pref.setString(PrefsKeys.strlbtext,lbText[main index]);
列表颜色=新列表();
添加(mainTextColor[mainIndex][0].toString());
添加(mainTextColor[mainIndex][1].toString());
添加(mainTextColor[mainIndex][2].toString());
pref.setStringList(PrefsKeys.list_color,color);
导航器。更换(
上下文
材料路线(
生成器:(上下文)=>MainMenu(),
),
);
});
}否则{
isEnabled=false;
设置状态()
Navigator.of(context).push(MaterialPageRoute(builder: (context) => SceneStory()));
Navigator.of(context).push(MaterialPageRoute(builder: (context) => Scene()));