【问题标题】:Generate navigator buttons dynamically动态生成导航按钮
【发布时间】:2018-06-03 00:21:13
【问题描述】:

我从颤振开始,我试图导航到另一个页面,其中包含一些动态生成的按钮,但我需要我的构建的构建上下文无处不在......(或者可能不需要,我不太确定了解如何使用它)

  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(),
            ),
          ),
      );
    }
}

提前致谢。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 2011-05-13
      • 1970-01-01
      • 2011-09-17
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-11
      相关资源
      最近更新 更多