modalBottomSheet被键盘重叠

时间:2019-09-29 15:11:56

标签: flutter

它写成here

/// The scaffold will expand to fill the available space. That usually
/// means that it will occupy its entire window or device screen. When
/// the device's keyboard appears the Scaffold's ancestor [MediaQuery]
/// widget's [MediaQueryData.viewInsets] changes and the Scaffold will
/// be rebuilt. By default the scaffold's [body] is resized to make
/// room for the keyboard. 

因此,如果底部有一个TextField,则Scaffold会自动调整大小,并且确实会发生。但是,当我将TextField放在modalBottomSheet内时,它不会被键盘推上去。键盘与modalBottomSheet(与TextField重叠)。如果Scaffold本身的大小得到了调整,modalBottomSheet将如何保持原样? resizeToAvoidBottomInsetmodalBottomSheet没有影响。 这是示例代码。

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          showModalBottomSheet(
              context: context, builder: (context) => ShowSheet());
        },
      ),
    );
  }
}

class ShowSheet extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child: TextField(
        autofocus: true,
      ),
    );
  }
}

如果这个问题愚蠢,我深表歉意,但我不明白。

1 个答案:

答案 0 :(得分:0)

我仍然不知道原因可能是因为modalBottomSheet正在使用PopupRoute,所以不确定这是一条不同的路线。无论如何,在这里我找到了我只需要放一些底部viewInsets填充的解决方案。

Widget build(BuildContext context) {
    return Padding(
      padding:
          EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
      child: Container(
        color: Colors.blue,
        height: 200,
        child: TextField(
          autofocus: true,
        ),
      ),
    );
  }

我还需要将isScrollControlled: true,中的showModalBottomSheet()设置为