flutter 2.0:嵌套 MaterialApps -> showModalBottomSheet 中的 textInput 问题

Mar*_*ein 6 flutter flutter2.0

在我的应用程序中,我使用 2 个材质应用程序来处理底部导航栏的导航。由于我的应用程序非常复杂,这是执行此操作的最佳方法。

在一个屏幕上,当用户未登录时,将打开一个底部表单,用户可以在其中输入他的登录凭据。

bottomSheet 应出现在导航栏上方。

在下面的代码片段中,这就是我解决它的方法。

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

final _navigationKey = GlobalKey<NavigatorState>();

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      resizeToAvoidBottomInset: true,
      body: MaterialApp(
        navigatorKey: _navigationKey,
        routes: {
          '0': (context) => Home(),
          '1': (context) => Scaffold(backgroundColor: Colors.yellow),
        },
        initialRoute: '0',
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.account_box_rounded),
            label: 'account',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.baby_changing_station),
            label: 'stuff',
          )
        ],
        onTap: (int index) {
          setState(() {
            currentIndex = index;
          });
          Navigator.of(context).popUntil((route) => !(route is PopupRoute));
          _navigationKey.currentState.pushReplacementNamed(index.toString());
        },
      ),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      showModalBottomSheet(
        context: context,
        useRootNavigator: false,
        isScrollControlled: true,
        builder: (_) => Container(
          child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: TextField(),
          ),
        ),
      );
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true,
      backgroundColor: Colors.red,
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

现在问题来了:

在 flutter 1.22 中:
当用户点击 TextInput 时,键盘会打开,而 bottomSheet 会将其位置移动到键盘上方。
颤振 1 中的行为

在 flutter 2.0 中:
当用户点击 TextInput 时,键盘会打开并且底部表格将其位置移出屏幕
颤振 2 中的行为

有没有人有好的解决方法?

到目前为止我尝试过的 我给了bottomSheet一个底部填充:

底部:MediaQuery.of(context).viewInsets.bottom),

但是并没有解决问题

Gui*_*lli 0

我遇到了同样的问题,我使用SingleChildScrollView底板内页修复了。

showModalBottomSheet(
  isScrollControlled: true,
  context: context,
  builder: (context) {
    return SingleChildScrollView(
    child: Container(
      padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
      child: ...,
    ),
  );
});
Run Code Online (Sandbox Code Playgroud)

来源:https ://github.com/flutter/flutter/issues/18564#issuecomment-586205403