【问题标题】:Refresh on ListView.builder while click on row view of item builder Flutter在单击项目构建器 Flutter 的行视图时刷新 ListView.builder
【发布时间】:2021-05-12 08:17:31
【问题描述】:

我正在开发 Flutter 应用程序,我在 BottomSheet 弹出窗口中使用 ListView.builder 类。我需要开发一些东西,用户可以从 ListView 中选择多个数据。

为此,我使用Map mapProfCredsT = new Map<dynamic, bool>(); 来存储列表视图中每个单元格的点击事件。 另外,我在每个点击事件上都使用了setState() {},以便我的数据应该更新。但即使在此之后,我也无法在点击事件后更新每个单元格的 UI。

请考虑以下代码sn-p:

Map mapProfCredsT = new Map<dynamic, bool>();

在 API 调用的响应中,我将所有标志添加为每个 ID 为 false,

for (int i = 0; i < arrProfCredsCount; i++) {
int id_key = respProfCreds['payload']['user_type'][i]['id'];
mapProfCredsT[id_key] = false; }

现在容器和图标的颜色取决于容器的点击事件如下:

Widget build(BuildContext context) {
return Material(
  type: MaterialType.transparency,
  child: Scaffold(
    key: scaffoldState,
    body: SafeArea(
      child: Container(
        height: double.infinity,
        width: double.infinity,
        color: Colors.teal,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(10.0),
              width: double.infinity,
              child: Text(
                'BottomSheet Sample',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 30.0,
                  color: Colors.black,
                ),
              ),
            ),
            Container(
              padding: EdgeInsets.all(20.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  GestureDetector(
                    onTap: () {
                      setState(() {
                        if (isChanged) {
                          isChanged = false;
                        } else {
                          isChanged = true;
                        }
                      });
                    },
                    child: Container(
                      color: Colors.white,
                      padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
                      child: Text('Change'),
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      // scaffoldState.currentState.showBottomSheet(
                      // scaffoldState.currentState.showBottomSheet(
                      showModalBottomSheet(
                          context: context,
                          builder: (builder) {
                            return new Container(
                              color: Colors.white,
                              height: 70.0.h,
                              child: Column(
                                children: <Widget>[
                                  Container(
                                    height: 10.0.w,
                                    color: Colors.white,
                                    child: Center(
                                      child: Text(
                                        'Professional Creds',
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    child: Container(
                                      child: ListView.builder(
                                          itemCount: arrProfCredsCount,
                                          itemBuilder: (context, index) {
                                            return ConstrainedBox(
                                              constraints: BoxConstraints(
                                                minHeight: 15.0.w,
                                              ),
                                              child: GestureDetector(
                                                onTap: () {
                                                  var profID = respProfCreds['payload']['user_type'][index]['id'];
                                                  print('Position on bottomsheet : $profID');
                                                  setState(() {
                                                    // Update text data..
                                                    respProfCreds['payload']['user_type'][index]['title'] =
                                                        'Test ${respProfCreds['payload']['user_type'][index]['id']}';
                                                  });
                                                },
                                                child: Container(
                                                  margin: EdgeInsets.all(2.0.w),
                                                  // color: Colors.tealAccent,
                                                  // color: mapProfCredsT[respProfCreds['payload']['user_type'][index]['id']]? themeYellow: Colors.blueGrey,
                                                  color: isChanged ? themeYellow : Colors.blueGrey,
                                                  child: Center(
                                                    child: Text(
                                                      '${respProfCreds['payload']['user_type'][index]['title']}',
                                                      textAlign: TextAlign.start,
                                                      style: kDataSingleSelectionBottomNav,
                                                    ),
                                                  ),
                                                ),
                                              ),
                                            );
                                          }),
                                    ),
                                  ),
                                ],
                              ),
                            );
                          });
                    },
                    child: Container(
                      color: Colors.white,
                      padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
                      child: Text('Open'),
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    ),
  ),
);}

请让我知道我在这里做错了什么,不允许我更新我的记录。

【问题讨论】:

  • 发布你的完整代码,特别是GestureDetector的父代码
  • @ikerfah 我已经更新了 GestureDetector 的父母,请你帮帮我,这里有什么问题或遗漏。
  • mapProfCredsT[profId] 有什么用?
  • 用StatefulBuilder(builder: (BuildContext context,void Function(void Function()) setState) {return Container(..);)}包裹你的Container(showModalBottomSheet的孩子),确保你使用的是StatefulBuilder中的setState(你可以使用不同的名字以避免混淆),检查并告诉我
  • 我将分享一些细节作为答案

标签: flutter flutter-layout flutter-listview


【解决方案1】:

这里的问题是showModalBottomSheet不是你的StatefulWidget的一部分,这意味着调用setState只刷新你的StatefulWidget而不是BottomSheet,因为它不是它的一部分。

所以为了解决这个问题。你这样做是通过

  • 将StatefulWidget 传递给您的showModalBottomSheet 构建器
  • 将StatefulBuilder 用作showModalBottomSheet 构建器的子级,然后从此StatefulBuilder 中使用setState,使用不同的名称以避免混淆。

【讨论】:

    【解决方案2】:

    setState 永远不会更新bottomSheet,因为您需要使用有状态构建器。 在这张表中,无论您想使用 setState 的任何地方,都使用 statefulBuilder 中声明的 updateState。

        showModalBottomSheet(
              context: context,
              builder: (builder) {
                return StatefulBuilder(builder: (context,updateState){
    //stateful builder added over here
                  return Container(
                    color: Colors.white,
                    height: 70.0.h,
                    child: Column(
                      children: <Widget>[
                        Container(
                          height: 10.0.w,
                          color: Colors.white,
                          child: Center(
                            child: Text(
                              'Professional Creds',
                            ),
                          ),
                        ),
                        Expanded(
                          child: Container(
                            child: ListView.builder(
                                itemCount: arrProfCredsCount,
                                itemBuilder: (context, index) {
                                  return ConstrainedBox(
                                    constraints: BoxConstraints(
                                      minHeight: 15.0.w,
                                    ),
                                    child: GestureDetector(
                                      onTap: () {
                                        var profID = respProfCreds['payload']['user_type'][index]['id'];
                                        print('Position on bottomsheet : $profID');
                                        updateState(() {
                                          // Update text data..
                                          respProfCreds['payload']['user_type'][index]['title'] =
                                          'Test ${respProfCreds['payload']['user_type'][index]['id']}';
                                        });
                                      },
                                      child: Container(
                                        margin: EdgeInsets.all(2.0.w),
                                        // color: Colors.tealAccent,
                                        // color: mapProfCredsT[respProfCreds['payload']['user_type'][index]['id']]? themeYellow: Colors.blueGrey,
                                        color: isChanged ? themeYellow : Colors.blueGrey,
                                        child: Center(
                                          child: Text(
                                            '${respProfCreds['payload']['user_type'][index]['title']}',
                                            textAlign: TextAlign.start,
                                            style: kDataSingleSelectionBottomNav,
                                          ),
                                        ),
                                      ),
                                    ),
                                  );
                                }),
                          ),
                        ),
                      ],
                    ),
                  );
                });
              });
    

    【讨论】:

      猜你喜欢
      • 2020-06-08
      • 2022-01-20
      • 2021-04-16
      • 2011-12-02
      • 2012-12-06
      • 1970-01-01
      • 2021-06-24
      • 2021-09-07
      相关资源
      最近更新 更多