flutter - 添加可弃用后,我的堆栈布局小部件变得死了

标签 flutter dart

我在listview flutter应用程序中添加了可忽略的小部件。添加它后,我的堆栈布局小部件变为无效代码,并且该列表未显示在我的应用中。
它显示为“任务实例”。但在不容忽视的情况下,该列表可以正常工作。请兄弟帮我解决这个问题。我的代码如下-

@override
Widget build(BuildContext context) {
  return new Scaffold(
    resizeToAvoidBottomInset: false,
    body: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        _myAppBar(context),
        Container(
          width: MediaQuery
              .of(context)
              .size
              .width,
          height: MediaQuery
              .of(context)
              .size
              .height - 80,
          child: ListView.builder(
              itemCount: items.length,
              itemBuilder: (context, index) {
                final item = items[index];

                return Dismissible(
                  // Each Dismissible must contain a Key. Keys allow Flutter to
                  // uniquely identify widgets.
                  key: new ObjectKey(items[index]),
                  // Provide a function that tells the app
                  // what to do after an item has been swiped away.
                  onDismissed: (direction) {
                    // Remove the item from the data source.
                    setState(() {
                      items.removeAt(index);
                    });

                    // Then show a snackbar.
                    Scaffold.of(context)
                        .showSnackBar(SnackBar(content: Text("$item dismissed")));
                  },
                  // Show a red background as the item is swiped away.
                  background: Container(color: Colors.red),
                  child: ListTile(title: Text('$item')),
                );


                return Stack(children: <Widget>[
                  // The containers in the background
                  Column(children: <Widget>[
                    Padding(
                      padding: EdgeInsets.only(left: 8.0, right: 8.0),
                      child: Container(
                        width: MediaQuery
                            .of(context)
                            .size
                            .width,
                        height: 80.0,
                        child: Padding(
                          padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
                          child: Material(
                            color: Colors.white,
                            elevation: 14.0,
                            shadowColor: Color(0x802196F3),
                            child: Center(
                              child: Padding(
                                padding: EdgeInsets.all(8.0),
                                child: Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    todoType('${items[index].tasktype}'),
                                    Text(
                                      '${items[index].taskcustomername}',
                                      style: TextStyle(
                                          color: Colors.black,
                                          fontSize: 20.0),
                                    ),
                                    Column(
                                      mainAxisAlignment:
                                      MainAxisAlignment.center,
                                      children: <Widget>[
                                        Text(
                                          '${items[index].taskcustomeruserid}',
                                          style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 18.0,
                                              fontWeight: FontWeight.bold),
                                        ),
                                        Text(
                                          '${items[index].taskcustomermobileno}',
                                          style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 16.0),
                                        ),
                                        // ..........................dismiss


                                        //.................end dismiss


                                      ],
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ]),


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

我希望我为消防局做的 list 。
但这只是显示“任务”的实例

在不容忽视的情况下,该列表正在运行。

最佳答案

您先返回Dismissible,然后返回Stack,但是返回Dismissible之后的代码将无法执行。您应该将Stack放在Dismissible子级中,代替ListTile

像这样:

@override
Widget build(BuildContext context) {
  return new Scaffold(
    resizeToAvoidBottomInset: false,
    body: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        _myAppBar(context),
        Container(
          width: MediaQuery
              .of(context)
              .size
              .width,
          height: MediaQuery
              .of(context)
              .size
              .height - 80,
          child: ListView.builder(
              itemCount: items.length,
              itemBuilder: (context, index) {
                final item = items[index];

                return Dismissible(
                  // Each Dismissible must contain a Key. Keys allow Flutter to
                  // uniquely identify widgets.
                  key: new ObjectKey(items[index]),
                  // Provide a function that tells the app
                  // what to do after an item has been swiped away.
                  onDismissed: (direction) {
                    // Remove the item from the data source.
                    setState(() {
                      items.removeAt(index);
                    });

                    // Then show a snackbar.
                    Scaffold.of(context)
                        .showSnackBar(SnackBar(content: Text("$item dismissed")));
                  },
                  // Show a red background as the item is swiped away.
                  background: Container(color: Colors.red),
                  child: Stack(children: <Widget>[
                    // The containers in the background
                    Column(children: <Widget>[
                      Padding(
                        padding: EdgeInsets.only(left: 8.0, right: 8.0),
                        child: Container(
                          width: MediaQuery
                              .of(context)
                              .size
                              .width,
                          height: 80.0,
                          child: Padding(
                            padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
                            child: Material(
                              color: Colors.white,
                              elevation: 14.0,
                              shadowColor: Color(0x802196F3),
                              child: Center(
                                child: Padding(
                                  padding: EdgeInsets.all(8.0),
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      todoType('${items[index].tasktype}'),
                                      Text(
                                        '${items[index].taskcustomername}',
                                        style: TextStyle(
                                            color: Colors.black,
                                            fontSize: 20.0),
                                      ),
                                      Column(
                                        mainAxisAlignment:
                                        MainAxisAlignment.center,
                                        children: <Widget>[
                                          Text(
                                            '${items[index].taskcustomeruserid}',
                                            style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 18.0,
                                                fontWeight: FontWeight.bold),
                                          ),
                                          Text(
                                            '${items[index].taskcustomermobileno}',
                                            style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 16.0),
                                          ),
                                          // ..........................dismiss


                                          //.................end dismiss


                                        ],
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ]),


                  ]),
                );

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

关于flutter - 添加可弃用后,我的堆栈布局小部件变得死了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58470691/

相关文章:

Flutter:删除生成的文件

dart - 在Dart的构造函数中通过网络调用初始化变量

flutter - 如何在Dart中检查字符串为null?

google-maps - Flutter - 谷歌地图在调整大小时崩溃

dart - 通过 Dart 中的循环写入文件

dart - 安装google api日历时出现flutter dart错误

android - 如何设置 Flutter build android aar 版本?

android - Flutter:Firestore 组件不存在

flutter - 如何关闭屏幕键盘?

Flutter OverflowBox 在下一列的 widget 后面