Flutter GridView.builder如何更新 [英] Flutter GridView.builder how to update

查看:43
本文介绍了Flutter GridView.builder如何更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 GriView 卡片构建Flutter应用程序,我想在每次按下 floatingActionButton 时添加新卡片.但是如何更新我的看法?我知道这些项目已添加到我的列表中,但是 GridView 不会显示更改.

I am trying to build an Flutter App with a GriView of cards and I want to add a new Card every time I hit the floatingActionButton. But how to update my view? I know the items are added to my list, but the GridView doesn't show the change.

  List<Widget> cardsList = [];

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text('Team Kitty'),
      ),
      body: GridView.builder(
          itemCount: cardsList.length,
          gridDelegate:
              SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
          itemBuilder: (BuildContext context, int index) {
            return Container(child: cardsList[index]);
          }),
      floatingActionButton: new FloatingActionButton(
        onPressed: addCard(),
        tooltip: 'Add Card',
        child: new Icon(Icons.add),
      ),
    );
  }
  addCard() {
    setState(() {
      cardsList.add(_RobotCard());
    });
  }
}

我发现很难找到好的Grid文档,也许有人暗示了?

I find it hard to find good Grid documentation, maybe someone has a hint?

Thx Guys

推荐答案

在构建窗口小部件时调用 addCard 方法,而不是将方法的引用作为 onPressed 参数传递:

You invoke addCard method while your widget is built instead of passing method's reference as onPressed parameter:

floatingActionButton: new FloatingActionButton(
  onPressed: addCard, // pass method reference here
  tooltip: 'Add Card',
  child: new Icon(Icons.add),
)

这篇关于Flutter GridView.builder如何更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆