如何将数据从 showmodalbottomsheet 传递到 Flutter 中的上一页

How to Pass data from showmodalbottomsheet to Previous Page in Flutter

我如何将数据从 showmodalbottomsheet 传递到上一页。下面是示例代码,我试过的是当我点击它时有一个按钮显示 modalbottomsheet 当我点击完成按钮时它应该将 1 值传递到上一页并且我还添加了 setStateonTap 上按下,但在上一页上它仍然没有改变,除非我点击 Flutter Hot Reload。如何解决这个问题?

class TestPage extends StatefulWidget {
  const TestPage({Key? key}) : super(key: key);

  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  String textResult = "";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(textResult),
                TextButton(
                    onPressed: () {
                      showModalBottomSheet(
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.only(
                                topRight: Radius.circular(24.0),
                                topLeft: Radius.circular(24.0))),
                        context: context,
                        builder: (_) => StatefulBuilder(
                            builder: (BuildContext context, setState) {
                          return Container(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      GestureDetector(
                                        onTap: () {
                                          setState(() {
                                            textResult = "1";
                                          });
                                          Navigator.pop(context);
                                        },
                                        child: Text(
                                          'Done',
                                          style: TextStyle(
                                              color: Colors.red,
                                              fontWeight: FontWeight.bold),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          );
                        }),
                      );
                    },
                    child: Text('Press'))
              ],
            ),
          ),
        ),
      ),
    );
  }
}

尝试更改此行:

builder: (BuildContext context, setState) {

用这条线

builder: (BuildContext context, StateSetter setStateModal) {

解释: build 中的外部小部件(Scaffold 及其内容)和 showModalBottomSheet 中的小部件具有不同的 setState( )。当您需要更新该小部件的某些部分时,您必须确保调用外部小部件。这个想法是 setState( )showModalBottomSheet 之外的小部件。为此,您可以给这两个 setState() 不同的名称,这样您就知道您在呼叫哪个:StateSetter setStateModal

@Canada2000 的回答还可以。我只是在扩展它。
showDialog()showModalBottomSheet()+... 中使用 StatefulBuilder 是这些小部件与当前上下文树分开。

当我们想在对话框中显示更改时,我们使用 StatefulBuilderStatefulBuilder 有自己的 statebuilder: (BuildContext context, setState) 并且调用 setState 使用 StatefulBuilder 的设置状态。

现在假设您想同时更改 UI,为此您只需将 StatefulBuilder 的状态重命名为类似 @Canada2000 所说的生成器内部的 SBsetState

  • 更新 _TestPageState 使用 setState((){})
  • 更新对话框 UI,使用 StatefulBuilder 的状态,如 SBsetState((){})

如果您不想在对话框中显示任何更改,您可以简单地忽略 StatefulBuilder。

您的小部件

import 'package:flutter/material.dart';

class TestPage extends StatefulWidget {
  const TestPage({Key? key}) : super(key: key);

  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  String textResult = "";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("text Result ${textResult}"),
                TextButton(
                    onPressed: () {
                      showModalBottomSheet(
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.only(
                                topRight: Radius.circular(24.0),
                                topLeft: Radius.circular(24.0))),
                        context: context,
                        builder: (_) => StatefulBuilder(
                            builder: (BuildContext context, setStateBTS) {
                          return Container(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      GestureDetector(
                                        onTap: () {
                                          setState(() {
                                            textResult = "1";
                                          });
                                          Navigator.pop(context);
                                        },
                                        child: Text(
                                          'Done',
                                          style: TextStyle(
                                              color: Colors.red,
                                              fontWeight: FontWeight.bold),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          );
                        }),
                      );
                    },
                    child: Text('Press'))
              ],
            ),
          ),
        ),
      ),
    );
  }
}