如何防止键盘覆盖 TextField

时间:2021-04-21 15:49:57

标签: flutter

我在屏幕底部附近有一个 TextField。当我聚焦 TextField 时,键盘与它重叠。但是 TextField 下方的按钮会向上推超过 TextField。

保持此 TextField 焦点的常用方法是什么?

return Scaffold(
  backgroundColor: Theme.of(context).colorScheme.background,
  body: Padding(
    padding: MyTheme.scaffoldPadding,
    child: Column(
      children: [
        Expanded(
          child: Column(
            children: [
              Align(
                alignment: Alignment.centerRight,
                child: Padding(
                  padding: EdgeInsets.all(12),
                  child: GestureDetector(
                    child: MyText(
                      "Skip",
                      MyTextType.title,
                    ),
                  ),
                ),
              ),
              Align(
                alignment: Alignment.centerLeft,
                child: SizedBox(
                  width: 64,
                  height: 64,
                  child: Image.asset("assets/images/main_icon.png"),
                ),
              ),
              Align(
                alignment: Alignment.centerLeft,
                child: MyText(
                  "This is some DescriptionText",
                  MyTextType.header,
                  textAlign: TextAlign.left,
                ),
              ),
              Align(
                alignment: Alignment.centerLeft,
                child: MyText(
                  "Here is some more description text",
                  MyTextType.description,
                  textAlign: TextAlign.left,
                ),
              ),
              SizedBox(height: 24),
              Column(
                children: [
                  FeedbackRadioWidgetItem(
                    isSelected: _selectedItem == 0,
                    onSelected: () => setState(
                      () {
                        _selectedItem = 0;
                      },
                    ),
                    text: "Selection Text 1",
                  ),
                  SizedBox(height: radioItemSpacing),
                  FeedbackRadioWidgetItem(
                    isSelected: _selectedItem == 1,
                    onSelected: () => setState(
                      () {
                        _selectedItem = 1;
                      },
                    ),
                    text: "Selection Text 2",
                  ),
                  SizedBox(height: radioItemSpacing),
                  FeedbackRadioWidgetItem(
                    isSelected: _selectedItem == 2,
                    onSelected: () => setState(
                      () {
                        _selectedItem = 2;
                      },
                    ),
                    text: "Selection Text 3",
                  ),
                  SizedBox(height: radioItemSpacing),
                  FeedbackRadioWidgetItem(
                    isSelected: _selectedItem == 3,
                    onSelected: () => setState(
                      () {
                        _selectedItem = 3;
                      },
                    ),
                    text: "Selection Text 4",
                  ),
                  SizedBox(height: radioItemSpacing * 2),
                  TextField(
                    style: TextStyle(
                      fontFamily: MyTheme.fontFamily,
                      fontSize: MyTheme.descriptionFontSize,
                      fontWeight: MyTheme.descriptionFontWeight,
                      color: Theme.of(context).colorScheme.onBackground,
                    ),
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: '(Optional) Write your feedback.'
                    ),
                    controller: _controller,
                    keyboardType: TextInputType.multiline,
                    maxLength: 400,
                    maxLines: 2,
                    minLines: 2,
                  ),
                ],
              ),
            ],
          ),
        ),
        MyButton(
          expandMode: HorizontalExpandMode.isNotInRow,
          text: "SEND FEEDBACK",
          onPressed: () => Navigator.of(context).pushNamedAndRemoveUntil("/home-screen", (route) => false),
        ),
      ],
    ),
  ),
);

1 个答案:

答案 0 :(得分:1)

请试试

Scaffold(
      body: SingleChildScrollView(
        child: Scaffold(
          backgroundColor: Theme.of(context).colorScheme.background,
          body: Padding(
            padding: MyTheme.scaffoldPadding,
            child: Column(
              children: [
                Expanded(
                  child: Column(
                    children: [
                      Align(
                        alignment: Alignment.centerRight,
                        child: Padding(
                          padding: EdgeInsets.all(12),
                          child: GestureDetector(
                            child: MyText(
                              "Skip",
                              MyTextType.title,
                            ),
                          ),
                        ),
                      ),
                      Align(
                        alignment: Alignment.centerLeft,
                        child: SizedBox(
                          width: 64,
                          height: 64,
                          child: Image.asset("assets/images/main_icon.png"),
                        ),
                      ),
                      Align(
                        alignment: Alignment.centerLeft,
                        child: MyText(
                          "This is some DescriptionText",
                          MyTextType.header,
                          textAlign: TextAlign.left,
                        ),
                      ),
                      Align(
                        alignment: Alignment.centerLeft,
                        child: MyText(
                          "Here is some more description text",
                          MyTextType.description,
                          textAlign: TextAlign.left,
                        ),
                      ),
                      SizedBox(height: 24),
                      Column(
                        children: [
                          FeedbackRadioWidgetItem(
                            isSelected: _selectedItem == 0,
                            onSelected: () => setState(
                              () {
                                _selectedItem = 0;
                              },
                            ),
                            text: "Selection Text 1",
                          ),
                          SizedBox(height: radioItemSpacing),
                          FeedbackRadioWidgetItem(
                            isSelected: _selectedItem == 1,
                            onSelected: () => setState(
                              () {
                                _selectedItem = 1;
                              },
                            ),
                            text: "Selection Text 2",
                          ),
                          SizedBox(height: radioItemSpacing),
                          FeedbackRadioWidgetItem(
                            isSelected: _selectedItem == 2,
                            onSelected: () => setState(
                              () {
                                _selectedItem = 2;
                              },
                            ),
                            text: "Selection Text 3",
                          ),
                          SizedBox(height: radioItemSpacing),
                          FeedbackRadioWidgetItem(
                            isSelected: _selectedItem == 3,
                            onSelected: () => setState(
                              () {
                                _selectedItem = 3;
                              },
                            ),
                            text: "Selection Text 4",
                          ),
                          SizedBox(height: radioItemSpacing * 2),
                          TextField(
                            style: TextStyle(
                              fontFamily: MyTheme.fontFamily,
                              fontSize: MyTheme.descriptionFontSize,
                              fontWeight: MyTheme.descriptionFontWeight,
                              color: Theme.of(context).colorScheme.onBackground,
                            ),
                            decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: '(Optional) Write your feedback.'),
                            controller: _controller,
                            keyboardType: TextInputType.multiline,
                            maxLength: 400,
                            maxLines: 2,
                            minLines: 2,
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                MyButton(
                  expandMode: HorizontalExpandMode.isNotInRow,
                  text: "SEND FEEDBACK",
                  onPressed: () => Navigator.of(context)
                      .pushNamedAndRemoveUntil(
                          "/home-screen", (route) => false),
                ),
              ],
            ),
          ),
        ),
      ),
    )