动态生成导航按钮

时间:2018-06-03 00:21:14

标签: dart flutter

我从颤动开始,我试图导航到另一个页面,其中包含一些动态生成的按钮,但我需要构建的构建内容到处...(或者不是,我&#39 ;我真的不明白如何使用它)

  class GamePage extends StatelessWidget {
        final Player p1;
        final Player p2;
        final List<Hexagon> arena;
    GamePage({this.p1, this.p2, this.arena});

    @override

    Widget getHexagon(Hexagon hcase) {
      Widget child;

    //if
    //else if

    //else =>
            child = RaisedButton(
            onPressed: nextTurn(),
            );

      Container container = new Container(
          child: child,
          );
      return container;
    }

    void nextTurn()
    {
      Navigator.push(/*here is the context needed*/, new MaterialPageRoute(
                      builder: (BuildContext context) =>
                         new GamePage(p1: p2, p2: p1, arena: arena,)));
    }

    List<Widget> getArena()
    {
      List<Widget> children = new List<Widget>();

        for (var hexagon in arena)
        {
          children.add(getHexagon(hexagon));
        }
      return children;
    }

    Widget build(BuildContext context) {

      return new Scaffold(
        body: 
        new Container(
            child: new GridView.count(
              children: getArena(),
            ),
          ),
      );
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以将该函数包含在另一个函数中,一旦调用它,将上下文传递给_nextTurn

        child = RaisedButton(
          onPressed: (){
            _nextTurn(context)
          },
        );