首页 > 解决方案 > 如何使用 ontap 动态添加小部件

问题描述

我创建了一个嵌套的 for 循环,用于动态创建像小部件这样的按钮。就布局而言,一切都很顺利。但是,当我点击按钮时,点击功能将使用错误的按钮 id 执行(使用当前按钮 id 而不是我用来创建它的那个)(我用来创建按钮的索引)有没有人知道我应该如何解决这个问题?谢谢。

class ButtonItemsModel {
final String image;
final Color color;
final String title;
final String description;
final String ontap;
ButtonItemsModel({
  @required this.color,
  @required this.description,
  @required this.image,
  @required this.title,
  @required this.ontap,
});
}
for (var row_count = 1; row_count <= _no_of_row; row_count++) {
      buttoncells = [];
      for (var button_cell_count = 1; button_cell_count <=
          _rowwidgetno; button_cell_count++) {
        if (buttonid >= buttonItems.length) {
          break;
        }
        buttoncells.add(Padding(
          padding: EdgeInsets.fromLTRB(
              hpadding, vpadding, hpadding, vpadding),
          child: Column(
              children: [
                Container(
                    width: button_width + 30,
                    child: Column(
                      children: [
                        Card(
                          // button color
                          elevation: (10),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(15),
                          ),
                          color: buttonItems[buttonid].color,
                          child: InkWell(
                            customBorder: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(15),
                            ),
                            splashColor: Colors.red,
                            onTap: () async {
                               //here is the part that i have problems with
                              if (await canLaunch(
                                  buttonItems[buttonid].ontap)) {
                                await launch(buttonItems[buttonid].ontap);
                              } else {
                                throw 'Could not launch' +
                                    buttonItems[buttonid].ontap;
                              }
                            },
                            child:
                            Padding(
                              padding: EdgeInsets.fromLTRB(5, 5, 5, 5),

                              child: SizedBox(
                                  width: button_width, height: button_width,
                                  child: Image(image: AssetImage(
                                      buttonItems[buttonid].image),
                                  )),
                            ),
                          ),

                        ),
                        Text(buttonItems[buttonid].title),
                      ],
                    )
                )
              ]
          ),
        ),
        );
        buttonid = ((row_count - 1) * _rowwidgetno + button_cell_count);
      }
        buttonrow.add(Row(
        children: buttoncells,
      ));
    }
    main_column = Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: buttonrow,
    );
  }

标签: flutterdart

解决方案


推荐阅读