Flutter,Fluro“匿名关闭”

时间:2019-10-04 18:30:13

标签: flutter dart

我刚刚开始学习Dart / Flutter,并被建议使用Fluro软件包进行导航。

是否可以使用Fluro路由器在2个StatefulWidget“页面”之间切换? 在无状态小部件之间进行切换时,我没有任何问题,但是当我试图使它们成为有状态时,出现了以下错误:“返回类型'Page2'不是由匿名闭包定义的'Widget'。”我不知道应该在代码中进行哪些更改。

class FluroRouter {
  static Router router = Router();
  static void setupRouter() {
    router.define("Page2", handler: page2Handler);
    router.define("Page1", handler: page1Handler);
  }

  static Handler page2Handler = Handler(
      handlerFunc: (BuildContext context, Map<String, dynamic> params) => Page2());

  static Handler page1Handler = Handler(
      handlerFunc: (BuildContext context, Map<String, dynamic> params) => Page1());
}


class BasePage2 extends StatefulWidget {
  @override
  State createState() {
    return Page2();
  }
}
class Page2 extends State<BasePage2> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome',
      home: Scaffold(
          floatingActionButton: FloatingActionButton(
            backgroundColor: Colors.pink,
            child: Icon(Icons.router),
            onPressed: () {
              Navigator.pushNamed(context, 'Page1');
            },
          ),
          appBar: AppBar(
            title: Text("Page 2"),
          ),
          body: Center(
            child: Text("Page 2"),
          )),
    );
  }
}

//Page 1 looks the same, only the text says "Page 1"

预先感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

您需要传递StatefulWidget而不是State

... => BasePage2()

... => BasePage1()