当我在 showDialog 函数中输入文本并将其传递给文本小部件时,它没有显示

问题描述 投票:0回答:0

我制作了一个 showMyDialog.dart 文件,其中包含带有 AlertDialog 的 showDialog 小部件,并且在内部我使用了一个文本字段来获取用户的输入,以便在 onPress 之后将其显示在同一屏幕上。

输入后它应该显示在屏幕上但是当状态改变并且值在调试控制台中可见时我没有尝试打印

这里是 showMyDialog.dart 的代码:

      import 'package:flutter/material.dart';

      Future<String?> showMyDialog(
        BuildContext context,
        String title,
      ) async {
        TextEditingController _textController = TextEditingController();
        return showDialog<String>(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text(title),
                content: TextField(
                  controller: _textController,
                  textInputAction: TextInputAction.done,
                ),
                actions: <Widget>[
                  TextButton(
                      onPressed: () {
                        Navigator.of(context).pop(_textController.text);
                      },
                      child: const Text("Ok")),
                      TextButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: const Text("Cancel"))
                ],
                
              );
            });
      }

这是我使用它的地方的代码:

      String _tempval = "";
      GestureDetector(
          onTap: () async {
            final enteredtext = await showMyDialog(
              context,
              "Add Temperature",
            );
            setState(() {
              _tempval = enteredtext ?? "";
              print(_tempval);
            });
            },
      child: Container(
        width: 120,
        height: 40,
        alignment: Alignment.center,
        padding: const EdgeInsets.symmetric(horizontal: 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,children: [
            Text(_tempval),
            const SizedBox(
            width: 6,
            ),
          ],
        ),
      decoration: BoxDecoration(
          border: Border.all(width: 0.4),
          borderRadius:
          const BorderRadius.all(Radius.circular(8))),
        ),
      ),
flutter showdialog text-widget flutter-onpressed
© www.soinside.com 2019 - 2024. All rights reserved.