颤抖如何自动从页面导航到另一个

时间:2020-05-17 12:11:43

标签: java android ios flutter dart

我正在编写一个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时,我要导航

1 个答案:

答案 0 :(得分:0)

编辑:

headsetPlugin.setListener((_val) {
  setState(() {
    headsetEvent = _val;
  });
Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => SecondRoute()),
      );
});