如何将数据从警报对话框传递到颤振中的同一页面

2024-01-03

我想从警报对话框传递数据。警报对话框包含文本字段,因此无论用户在文本字段上输入什么内容,文本都应该传递到主页(屏幕)。下面是警报对话框的代码

    Padding(
                          padding: const EdgeInsets.only(left: 42.0),
                          child: Align(
                            alignment: Alignment.topCenter,
                            child: RaisedButton(onPressed: (){
                                _showDialog();
                            },
                          ),
                        ),

Padding(
                padding: const EdgeInsets.only(top: 50.0),
                  child: new Text('// Displays text'););

    void _showDialog() {
        showDialog(
          context: context,
          builder: (BuildContext context) {
            // return object of type Dialog
            return AlertDialog(
              title: new Text("Alert Dialog title"),
              content: TextField(
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                    hintText: 'Enter the number'
                ),
              )
              ,
              actions: <Widget>[
                // usually buttons at the bottom of the dialog
                Row(
                  children: <Widget>[
                   new FlatButton(
                   child: new Text("Cancel"),
                    onPressed: () {
                    Navigator.of(context).pop();
                    },
                  ),
                    new FlatButton(onPressed: (){

                    }, child: new Text("OK"))
                  ],

                ),
              ],
            );
          },
        );
      }

编辑新解决方案:

// write this in your main page
String onMainPageText;

你可以像这样显示在您的主页上!单击您的确定后显示对话框 method Text(onMainPageText)

改变你的_showDialog方法与以下代码。

  void _showDialog() {
    String dialogText;
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Alert Dialog title"),
          content: TextField(
            onChanged: (String textTyped) {
              setState(() {
                dialogText = textTyped;
              });
            },
            keyboardType: TextInputType.number,
            decoration: InputDecoration(hintText: 'Enter the number'),
          ),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            Row(
              children: <Widget>[
                new FlatButton(
                  child: new Text("Cancel"),
                  onPressed: () {
                    setState(() {
                      onMainPageText = '';
                    });
                    Navigator.of(context).pop();
                  },
                ),
                new FlatButton(
                    onPressed: () {
                      setState(() {
                        onMainPageText = dialogText;
                      });
                      Navigator.of(context).pop();
                    },
                    child: new Text("OK"))
              ],
            ),
          ],
        );
      },
    );
  }

旧答案:

创建一个全局 TextEditingController 将处理您的问题,您可以使用以下命令访问文本字段文本textEditingConroller.text

不要忘记定义 textEditingController在你的班级里

class YourMainPageState extends State<YourMainPage>{
  TextEditingController textEditingController = new TextEditingController();

}
  void _showDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Alert Dialog title"),
          content: TextField(
            controller: textEditingController,
            keyboardType: TextInputType.number,
            decoration: InputDecoration(hintText: 'Enter the number'),
          ),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            Row(
              children: <Widget>[
                new FlatButton(
                  child: new Text("Cancel"),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                new FlatButton(onPressed: () {print(textEditingController.text);}, child: new Text("OK"))
              ],
            ),
          ],
        );
      },
    );
  }

您可以使用该代码显示键入的文本:

Padding(
                padding: const EdgeInsets.only(top: 50.0),
                  child: new Text(texEditingController.text););
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将数据从警报对话框传递到颤振中的同一页面 的相关文章

随机推荐

  • UIViewController调用对方的delegate

    我有两个 UIViewController 每个都有它的委托 并且正在调用其中一个 一个类称为 TopicViewController 另一个类称为 MentionViewController 代码如下所示 import
  • JavaScript 中比较 null 与 undefined 的速度

    我刚刚运行了一个非常简单的 JavaScript性能测试 http jsperf com js coerce null 不要问为什么 该测试声明了一个变量 但没有为其分配任何内容 var x 然后它比较比较值变量的速度null 并undef
  • Rails 3 / Devise:不再创建密码盐?

    我已经在一个项目上工作了一段时间 该项目使用 Devise 进行用户身份验证 每当创建用户时 它都会为他们生成密码盐及其加密密码 当我进入这个项目的尾声时 我正在测试用户注册 并注意到我的新角色没有在数据库中为这些新用户创建密码盐 而我的旧
  • 如何在Java中更改HTML标签内容?

    如何更改Java中标签的HTML内容 例如 before div text div text div text div after div text div new text div text div 我尝试过 JTidy 但不支持getT
  • 将数组格式的字符串转换为javascript数组[重复]

    这个问题在这里已经有答案了 我有一个这种格式的字符串 A B C 我想将它转换为数组 A B C 我尝试使用JSON parse 但它不起作用 任何帮助 将不胜感激 const strArray A B C const parsedStri
  • 使用 LINQ 读取 Csv

    我有一个像这样的 csv 文件 A 22 23 12 B 32 4 33 C 34 3 33 我想打印每行的总和和平均值并跳过第一列 如何在 LINQ 中使用 Lambda 进行操作 var stuff from l in File Rea
  • 在AndroidTestCase中使用@Ignore

    我正在使用 AndroidTestCase 进行一些单元测试 并且想知道是否可以使用我在 junit4 中读过的忽略注释 仅使用注释就会出现错误 也许有什么特别需要注意的地方 提前感谢 马库斯 您可以使用 Suppress在测试用例类或单个
  • DECLARE_DYNAMIC 和 DECLARE_DYNCREATE 之间的区别?

    你能让我知道两者之间有什么区别吗DECLARE DYNAMIC and DECLARE DYNCREATE 我们到底可以在哪里使用它们 第一个声明类具有运行时类型信息 第二个声明可以在运行时动态创建实例 MSDN 文档对此进行了详细描述 请
  • 如何用C#实现Lua容器(虚拟文件系统)模块加载器

    听起来有点可怕不是吗 一些背景信息 我想使用 LuaInterface 将包含一些 lua 模块的 tar 存档加载到我的 C 应用程序中 最简单的方法是将这些文件提取到临时文件夹 修改 lua 模块搜索路径并像往常一样使用 require
  • Woocommerce 产品库图像显示为全尺寸图像而不是缩略图

    该网站当前的 Woocommerce 设置正常提供主产品图像和多个图库图像 主要产品图像已正确使用 以便它显示在列表中 单击后 将打开产品页面 但是 在此产品页面中 Woocommerce 上提供的图库图像在屏幕右侧以全尺寸图像的形式显示在
  • adb 无法识别 Nexus 7 平板电脑

    我的 android sdk 和我的平板电脑出现一些奇怪的行为 我正在尝试通过以下方式识别我的平板电脑 adb devices l command 每次我插入平板电脑时 计算机上都会弹出一个窗口 询问我将平板电脑识别为相机 并询问我想用什么
  • 由于无法处理“${project.artifactId}”,无法构建 docker 映像:替换中缺少 ':'

    我正在尝试使用以下命令为 Maven 项目构建 docker 映像 mvn clean install DskipTests Pdocker 我有以下内容Dockerfile FROM openjdk 8 jre ARG serviceus
  • 使用 cypher 将节点插入 Neo4j 数据库的最有效方法是什么

    我试图通过使用 py2neo python 模块 py2neo cypher execute 执行 cypher 命令 将大量节点 500 000 插入到 非嵌入式 neo4j 数据库中 最终我需要消除对 py2neo 的依赖 但我目前正在
  • while ((c = getchar()) != EOF) 不终止

    我一直在阅读 C 编程语言 并且了解了输入和输出的这一部分 I ve read other threads saying that the console doesn t recognize enter as EOF So that I s
  • 将示例 XML 代码放入 Javadoc 中

    如何将示例 XML 代码放入 Javadoc 中 XML 代码包含我不想处理的尖括号 我尝试了以下组合PRE and code但没有成功 您可以使用 codejavadoc 标签 看看这个帖子 http www rgagnon com ja
  • EclipseLink 动态 MOXy 访问枚举值

    我正在使用下面列出的 XSD 和相应的 XML 一切都运转良好动态MOXy http wiki eclipse org EclipseLink Examples MOXy Dynamic但我不知道如何访问java中的枚举类型 有什么建议么
  • Javascript:确定字符串中的所有字符是否唯一,如果不唯一,则删除重复字符

    设置一个数组a letter occurences 但努力循环遍历这个数组 以检查occurences gt 1并删除那些存在的 function charFreq s var i j var a new Array for j 0 j l
  • iOS 11 PDFKit 不更新注释位置

    我正在构建一个在 iPad 上编辑 PDF 的应用程序 我正在尝试使用添加到 PDFView 超级视图的 panGesture 识别器来实现注释的拖动 问题是注释的新矩形边界已分配 但更改并未反映在屏幕上 这是我的代码 objc func
  • C# 中的通用列表和静态变量行为

    我有一个简单的 C 应用程序 当我运行代码时 我没有得到预期的结果 我得到 2 2 1 但我期待 1 2 3 using System using System Collections Generic using System Linq u
  • 如何将数据从警报对话框传递到颤振中的同一页面

    我想从警报对话框传递数据 警报对话框包含文本字段 因此无论用户在文本字段上输入什么内容 文本都应该传递到主页 屏幕 下面是警报对话框的代码 Padding padding const EdgeInsets only left 42 0 ch