首页 > 解决方案 > 即使在显示键盘后如何使容器修复定位

问题描述

我已经玩了一段时间了,但无法正常工作。基本上我有底部导航栏,我想在底部导航栏的顶部固定定位的容器,即使显示键盘也会留在那里。到目前为止,无论我尝试什么,当调用键盘时,容器最终都会出现在键盘的顶部。到目前为止,这是我的代码。基本上我最后的希望是将它包装在 Stack 小部件中,但这没有用。

class HomeScreen extends StatefulWidget {   static const String id = '/app';   final FirebaseUser user;   HomeScreen({this.user});

  @override   _HomeScreenState createState() => _HomeScreenState(); }

class _HomeScreenState extends State<HomeScreen> {   int _currentIndex
= 0;   final List<Widget> _children = [Screen1(), Screen2()];

  void bottomTabHandler(int index) {
    setState(() {
      _currentIndex = index;
    });   }

  @override   Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        onTap: bottomTabHandler,
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.receipt),
            title: Text('Option1'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.receipt),
            title: Text('Option2'),
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.add), title: Text('Option3')),
        ],
      ),
      appBar: AppBar(
        centerTitle: true,
        title: Text('XXX'),
        automaticallyImplyLeading: false,
      ),
      endDrawer: AppbarDrawer(),
      body: Stack(
        children: <Widget>[
          _children[_currentIndex],
          Positioned(
            bottom: 0,
            child: Container(
              width: 250,
              height: 100,
              color: Colors.blue,
            ),
          ),
        ],
      ),
     );
    }    
}

我在 screen1 有简单的 TextField 这就是我得到的

在此处输入图像描述在此处输入图像描述

标签: flutter

解决方案


如果您想确保在键盘出现时不调整小部件,您可以使用以下resizeToAvoidBottomInset属性Scaffold

return Scaffold(
  resizeToAvoidBottomInset: false,
  body: Stack(
  ...

推荐阅读