flutter - 如何修复 ' FlatButton hidden behind the keyboard'?

标签 flutter dart flutter-layout

我有一个小部件showModalBottomSheet ..里面有一个类(AddTaskScreen),
但是FlatButton隐藏在键盘后面..我应该怎么做才能使其可见?

这是代码:

 class AddTaskScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    String newTaskTitle;
    return Container(
      padding: EdgeInsets.all(20.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20.0),
          topRight: Radius.circular(20.0),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Text(
            'Add Task',
            textAlign: TextAlign.center,
            style: TextStyle(
                fontSize: 30.0,
                color: Colors.lightBlueAccent,
                fontWeight: FontWeight.w700),
          ),
          TextField(
            autofocus: true,
            textAlign: TextAlign.center,
            onChanged: (newText) {
              newTaskTitle = newText;
            },
          ),
          FlatButton(
            child: Text(
              'Add',
              style: TextStyle(color: Colors.white),
            ),
            color: Colors.lightBlueAccent,
            onPressed: () {
              Provider.of<TaskData>(context).addTask(newTaskTitle);
              Navigator.pop(context);
            },
          )
        ],
      ),
    );
  }
}

我尝试了这种解决方案,但不起作用:
Padding(
  padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
  child: FlatButton()
)

这是图像ScreenShot of my App

最佳答案

您可以使用此代码触发底部工作表

void openBottomSheet(context) {
    showModalBottomSheet<dynamic>(
      context: context,
      builder: (BuildContext context) {
        return Padding(
          padding:
              EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
          child: Wrap(
            children: <Widget>[
              AddTaskScreen(),
            ],
          ),
        );
      },
    );
  }

确保我们用包装 AddTaskScreen 并包装,以便有效地呈现它。

并且还用填充及其值来包装它viewInsets.bottom

这是完整的代码
import 'package:flutter/material.dart';

class AddTaskScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    String newTaskTitle;
    return Container(
      padding: EdgeInsets.all(20.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20.0),
          topRight: Radius.circular(20.0),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Text(
            'Add Task',
            textAlign: TextAlign.center,
            style: TextStyle(
                fontSize: 30.0,
                color: Colors.lightBlueAccent,
                fontWeight: FontWeight.w700),
          ),
          TextField(
            autofocus: false,
            textAlign: TextAlign.center,
            onChanged: (newText) {
              newTaskTitle = newText;
            },
          ),
          FlatButton(
            child: Text(
              'Add',
              style: TextStyle(color: Colors.white),
            ),
            color: Colors.lightBlueAccent,
            onPressed: () {
              Navigator.pop(context);
            },
          )
        ],
      ),
    );
  }
}

class ButtomSheetScreen extends StatelessWidget {
  void openBottomSheet(context) {
    showModalBottomSheet<dynamic>(
      context: context,
      builder: (BuildContext context) {
        return Padding(
          padding:
              EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
          child: Wrap(
            children: <Widget>[
              AddTaskScreen(),
            ],
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Bottom Sheet",
        ),
        leading: Builder(
          builder: (BuildContext context) {
            return IconButton(
              icon: const Icon(Icons.chat_bubble_outline),
              onPressed: () {
                openBottomSheet(context);
              },
            );
          },
        ),
      ),
      body: Container(),
    );
  }
}

这是示例work-app的repo

Demo

关于flutter - 如何修复 ' FlatButton hidden behind the keyboard'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57516615/

相关文章:

android - Flutter Audioplayer APK版本不起作用

http - 使用 JSON 数组的 Flutter POST

dart - readByteSync-此行为正确吗?

flutter - 将不同的类对象传递给函数

flutter - Visual Studio Code 在 Flutter 中添加 'prefix0'

flutter - 关闭小部件时如何将 : Remove separator from ListView. 分开?

dart - Flutter:在多个列表或列之间排序、拖放

flutter - 如何将 `min-height` 添加到 Flutter `showTimePicker` 对话框?

button - 使用 slider 按钮创建分段控件小部件-Flutter

gridview - Flutter - 如何调整 GridView 单元格的大小