【问题标题】:Refresh ListView.builder On Button Click in Flutter在 Flutter 中单击按钮刷新 ListView.builder
【发布时间】:2020-06-08 12:27:13
【问题描述】:

我正在使用下面的代码,其中有一个带有开关的 ListView。 我想实现类似当我单击RaisedButton 时的东西 - 它会重新加载 ListView 并且switch.value 的所有值都应该更改为 true 或 false。 用户可以从 ListView 中的项目或单击按钮更改开关的值。 我不知道应该如何更改 ListView 中的值或所有开关。

 return Column(
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width / 2,
          height: 100,
          padding: EdgeInsets.all(20),
          child: RaisedButton(
              onPressed: () {
              },
              child: Text(
                BTN_START_TRIP,
                style: new TextStyle(
                  fontSize: 20.0,
                ),
              ),
              textColor: buttonFontColor,
              color: buttonColor,
              shape: new RoundedRectangleBorder(
                  borderRadius: new BorderRadius.circular(15.0))
          ),
        ),
        Expanded(
          child: ListView.builder(

            padding: EdgeInsets.all(3.0),
            // Let the ListView know how many items it needs to build.
            itemCount: snapshot.data.results.length,
            // Provide a builder function. This is where the magic happens.
            // Convert each item into a widget based on the type of item it is.
            itemBuilder: (context, index){

              return Container(
                height: 120,
                child: Card(
                  elevation: 10,
                  child: InkWell(
                    splashColor: Colors.blue.withAlpha(30),
                    onTap: () {
                      print(snapshot.data.results[index].original_title);
                    },
                    child: Container(
                      height: 120,
                      child: Row(
                          children: <Widget>[
                            Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Container(
                                  padding: EdgeInsets.fromLTRB(20, 0, 5, 0),
                                  width: MediaQuery.of(context).size.width -90,
                                  child: Align(
                                    alignment: Alignment.topLeft,
                                    child: Text(snapshot.data.results[index].original_title,
                                        textAlign: TextAlign.left,
                                        style: TextStyle(fontSize: defaultTitleFontsize, color: defaultFontColor),
                                        maxLines: 5),
                                  ),
                                ),
                                Container(
                                  padding: EdgeInsets.fromLTRB(20, 0, 5, 0),
                                  child:  Align(
                                    alignment: Alignment.topLeft,
                                    child: Text(snapshot.data.results[index].original_language,textAlign: TextAlign.left,style: TextStyle(fontSize: defaultsubTitleFontsize, color: defaultFontColor)),
                                  ),
                                ),
                              ],
                            ),
                            Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                crossAxisAlignment: CrossAxisAlignment.end,
                              children: <Widget>[
                                Container(
                                  child: Switch(
                                      value: false,
                                      onChanged: (value){
                                        setState(() {
                                          print(value);
                                        });
                                      }
                                  ),
                                ),
                              ],
                            ),
                          ]
                      ),

                    ),
                  ),
                ),
              );
            },
          ),
        )
      ],
    );

【问题讨论】:

    标签: flutter flutter-listview


    【解决方案1】:

    您需要有一个变量来决定开关是打开还是关闭。并且在某个事件期间(例如单击按钮)将变量设置为适当的值并通过调用 setState 重新触发构建(重新绘制 UI)。您需要有状态小部件的上述逻辑部分才能完成此操作。

    【讨论】:

      猜你喜欢
      • 2021-04-16
      • 2021-05-12
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多