Flutter抽屉底部添加ListTile失败问题求助
解决Flutter Drawer底部固定ListTile的问题
你的问题出在用ListView作为Drawer的直接子组件,ListView的children里使用Expanded不会生效——ListView本身是滚动容器,它会根据子项的大小来布局,不会为Expanded分配剩余空间。要把ListTile固定在Drawer底部,需要调整整体结构:
把Drawer的child换成Column,然后将可滚动的内容放在Expanded包裹的ListView里,底部直接放置需要固定的ListTile。这样Column会把剩余空间分配给中间的ListView,底部的ListTile就会固定在Drawer最下方。
修改后的完整代码:
drawer: Drawer( child: Column( children: [ SizedBox( height: 127, child: DrawerHeader( decoration: BoxDecoration(color: Colors.amber[400]), child: const Text( 'Train Me', style: TextStyle(fontSize: 28), ), ), ), const ListTile( title: Text( 'Witaj, Kuba', style: TextStyle(fontSize: 20), ), ), Divider( color: Colors.grey, thickness: 1, ), // 中间可滚动部分用Expanded包裹ListView Expanded( child: ListView( padding: EdgeInsets.zero, children: [ ListTile( title: const Text.rich(TextSpan(children: [ WidgetSpan( child: Icon(Icons.settings, size: 22), ), TextSpan(text: ' Ustawienia', style: TextStyle(fontSize: 20)) ])), onTap: () {}, ), Divider( color: Colors.grey, thickness: 1, ), // 这里可以添加更多需要滚动的ListTile ], ), ), // 底部固定的About ListTile Divider( color: Colors.grey, thickness: 1, ), ListTile( title: const Text.rich(TextSpan(children: [ WidgetSpan( child: Icon(Icons.info_outline, size: 22), ), TextSpan(text: ' About', style: TextStyle(fontSize: 20)) ])), onTap: () {}, ), ], ), )
关键说明:
- Column作为Drawer的直接子组件,能灵活分配空间:顶部是固定高度的头部,中间是占满剩余空间的可滚动区域,底部是固定的ListTile。
- Expanded包裹ListView,确保ListView能占用Column的剩余空间,当内容过多时可以正常滚动。
- 底部的ListTile直接放在Column的最后,自然会固定在Drawer底部。
内容的提问来源于stack exchange,提问作者lil_buck3t
相关产品推荐
相关产品推荐

