Flutter 设置状态不更新 bottomsheet 函数中的变量值

标签 flutter setstate bottom-sheet

我有一个按钮,点击它我会调用下面的函数,该函数在下面创建一个 Bottom Sheet 。发生的事情是它根据列表数组值构建卡片列表。然后我想根据 onTap 手势更新其中一张卡片的值。我注意到它检测到手势但没有更新它的值。最重要的是,我有一个全局变量定义为

String _localPNumberSelected="";

在 onTap 中我调用了 setState

setState(() {                                                   
 _localPNumberSelected=vList[index]["pNumber"].toString();
});

但这不会更新

children: [
  new Text('$_localPNumberSelected'),                                                
],

这里是完整的代码。

void _callQuickListModalBottomSheet(context){
    Future<void> future = showModalBottomSheet<void>(
      context: context,
      builder: (BuildContext bc){
          return Container(
            height: 280,
            margin: new EdgeInsets.fromLTRB(200, 0, 10, 0),
            child : new Container(
              decoration: new BoxDecoration(
                 color: Color.fromRGBO(36, 46, 66, 1),
                 borderRadius: new BorderRadius.only(
                   topLeft: const Radius.circular(20),
                   topRight: const Radius.circular(20)
                 ),                 
              ),
              child: new Column(                    
                      children: <Widget>[
                        new Container(
                        margin: new EdgeInsets.fromLTRB(10, 10, 10, 0),
                        height:45,
                        child: new Column(
                        children: <Widget>[
                          new Card(
                                    color: Colors.white,
                                    child: Row (
                                    children: <Widget>[
                                              new Column(
                                                 mainAxisAlignment: MainAxisAlignment.center,
                                                 crossAxisAlignment: CrossAxisAlignment.center,
                                                 children: <Widget>[
                                                   Container(
                                                      decoration: new BoxDecoration(
                                                      color: Colors.green,
                                                      borderRadius: new BorderRadius.only(
                                                        topLeft: const Radius.circular(5),
                                                        bottomLeft: const Radius.circular(5)
                                                      ),
                                                      ),
                                                  child: Icon(Icons.check, size: 20.0),
                                                  height: 27,
                                                  width: 30,
                                                ),
                                              ],
                                              ),
                                              new Column(
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                crossAxisAlignment: CrossAxisAlignment.center,
                                                mainAxisSize: MainAxisSize.max,
                                                children: [
                                                        new Text('$_localPNumberSelected'),

                                               ],
                                             ), 
                                      ],
                                    )
                          )
                        ]
                        )
                        ),
                        new Container(
                        height:190,                        
                        child:
                        new ListView.builder(
                          shrinkWrap: true,
                          itemCount: vehicleListdata.length,                          
                          itemBuilder: (BuildContext ctxt, int index) {
                            return Container(
                              margin: new EdgeInsets.fromLTRB(10, 0, 10, 0),
                              width: 30.0,
                              height: 35.0,                         
                                  child: Card(
                                    color: Colors.white,
                                    child: Row (
                                    children: <Widget>[
                                              new Column(
                                                 mainAxisAlignment: MainAxisAlignment.center,
                                                 crossAxisAlignment: CrossAxisAlignment.center,
                                                 children: <Widget>[
                                                 Container(
                                                      decoration: new BoxDecoration(
                                                      color: Colors.amber,
                                                      borderRadius: new BorderRadius.only(
                                                        topLeft: const Radius.circular(5),
                                                        bottomLeft: const Radius.circular(5)
                                                      ),

                                                    ),
                                                  height: 27,
                                                  width: 10,

                                                  ),
                                              ],
                                              ),
                                              new Column(
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                crossAxisAlignment: CrossAxisAlignment.center,
                                                mainAxisSize: MainAxisSize.max,
                                                children: [
                                                     new GestureDetector(
                                                          onTap: () {
                                                            setState(() {
                                                              _localPNumberSelected=vList[index]["pNumber"].toString();
                                                            });
                                                          doSomething(vList[index]["pNumber"].toString());
                                                        }, 
                                                        child:new Text(vList[index]["pNumber"].toString()),    
                                                       ),

                                            ],
                                          ), 
                                      ],
                                    ),


                                  )
                            );

                          }
                        )
                        )
                      ],
                    ),
            )
          );
      }
    );
    future.then((void value) => _closeModal(value));
}

最佳答案

查看示例

 String update = 'Oya Update Jhoor';

  void locationSheet() {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return StatefulBuilder(builder: (context,state){
            return Center(
              child: FlatButton(
                  onPressed: () {
                    updated(state);
                  },
                  child: new Text('$update')),
            );
          });
        });
  }

  Future<Null> updated(StateSetter updateState) async {
    updateState(() {
      update = 'Update Leekan si';
    });
  }

关于Flutter 设置状态不更新 bottomsheet 函数中的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54903675/

相关文章:

Flutter - InkWell 为什么会影响容器的边距区域

flutter :库比蒂诺 ListTile ? (或如何创建类似 iOS 的设置菜单)

android - 应用程序 :compileFlutterBuildRelease error while building app flutter

javascript - react : Setting State for Deeply Nested Objects w/Hooks

android - bottomSheetDialogFragment 全屏

FLUTTER:如何显示 Open Weather Map 中的天气图标?

flutter - Flutter中的setState()方法直到for循环中的最后一次调用才更新 View

javascript - React 中setState 的这两种updater 函数是否相等?

android - 无法将编译 'com.android.support:design:27.0.2' 添加到 The Build Gradle

java - 如何通过在Android持久 Bottom Sheet 中向上滑动从隐藏状态进入折叠状态?