我正在编写一个Flutter应用程序,我想在达到条件时在没有任何用户交互的情况下更改页面
将导航设置为a =2;
int a=0;
HeadsetEvent headsetPlugin = new HeadsetEvent();
HeadsetState headsetEvent;
@override
void initState() {
super.initState();
/// if headset is plugged
headsetPlugin.getCurrentState.then((_val){
setState(() {
headsetEvent = _val;
a=1;
});
});
/// Detect the moment headset is plugged or unplugged
headsetPlugin.setListener((_val) {
setState(() {
headsetEvent = _val;
a=2;
});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("auto navigation"),
),
body: Center(
child: Text(a.toString()),
),
),
);
}
}
这是一个带有变量的代码,每次获取一个值,当a = 2时,我要导航
答案 0 :(得分:0)
编辑:
headsetPlugin.setListener((_val) {
setState(() {
headsetEvent = _val;
});
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
});