android Flutter:删除alertDialog中键盘TextFormField的溢出

lg40wkob  于 2023-03-16  发布在  Android
关注(0)|答案(2)|浏览(102)

使用alertDialog时,我希望用户输入他们正在创建的新锻炼的名称。但是键盘导致整个小部件试图滚动并导致覆盖。我该如何解决这个问题?
警报对话框中的文本表单字段

showDialog(
    context: context,
    builder: (context) {
      TextEditingController titleController = TextEditingController();
      return AlertDialog(
        title: const Text("Workout Title"),
        content: SizedBox(
          height: 110,
          child: Column(
            children: [
              TextFormField(
                  focusNode: _focusNode,
                  controller: titleController,
                  inputFormatters: [
                    LengthLimitingTextInputFormatter(14),
                  ]),
              Padding(
                padding: const EdgeInsets.only(top: 10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    TextButton(
                        onPressed: () => Navigator.pop(context),
                        child: const Text("CANCEL")),
                    TextButton(
                        onPressed: () async {
                          setState(() {
                            if (titleController.text.isNotEmpty) {
                              dayTiles.add(
                                  DayTile(title: titleController.text));
                            }
                          });
                          FocusScope.of(context).requestFocus(FocusNode());
                          saveData();
                          Navigator.pop(context);
                        },
                        child: const Text("DONE")),
                  ],
                ),
              ),
            ],
          ),
        ),
      );
    });
pexxcrt2

pexxcrt21#

溢出应该来自于支架体。请尝试使其可滚动,如

Scaffold(
  body: SingleChildScrollView(
    child: Column(
irlmq6kh

irlmq6kh2#

您不需要在alertView中使用Scaffold,您需要的是将SingleChildScrollView作为父Scaffold的主体。

class _DemoState extends State<Demo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Demo'),
      ),
      // right here
      body: SingleChildScrollView(
        child: Column(children: [
          //child,
          //child,
          TextButton(
              onPressed: () {
                showDialog(context: context, builder: ...);
              },
              child: const Text('Add workout'))
        ]),
      ),
    );
  }
}

相关问题