简体   繁体   English

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

[英]Flutter how to automatic navigation from page to another

I'm writing a flutter application and i want to change the page without any users interaction when a condition is reached iw ant the navigation to be when a =2;我正在编写一个 flutter 应用程序,我想在达到某个条件时在没有任何用户交互的情况下更改页面 iw ant 当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()),
        ),
      ),
    );
  }
}

that's a code with a variable aa eaach time take a value i want the navigation when a=2 thank you这是一个带有变量 aa 的代码,每次取一个值我想要在 a=2 时导航 谢谢

Edit:编辑:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM