【问题标题】:Flutter - Is there a way to use just IconButton (without creating an app bar) to open the drawer?Flutter - 有没有办法只使用 IconButton(不创建应用栏)来打开抽屉?
【发布时间】:2019-09-05 22:51:37
【问题描述】:

我正在尝试使用可以在我的应用页面上打开抽屉的 IconButton,所以当我点击图标按钮时,我希望看到抽屉。我一直在网上寻找一种方法,但似乎只有两种解决方案:我可以使用 appbar 将 IconButton 放入,或者我可以尝试浮动操作按钮。但它们不是我要找的东西,我只想用 IconButton 打开抽屉。有可能吗?

【问题讨论】:

  • 看看这个:stackoverflow.com/questions/47435231/…。我已经有同样的问题,这个答案对我有帮助
  • @RubensMelo 问题是 IconButton 仍在 appBar 中。我想使用 IconButton 打开抽屉,但我真的不希望我的页面中有应用栏。
  • 解决方案是一样的,使用_scaffoldKey.currentState.openDrawer()使用带有图标按钮的全局“活动”抽屉
  • 想通了。谢谢!

标签: dart flutter


【解决方案1】:

是的,您可以通过IconButton轻松打开抽屉,而无需使用appBar。 您需要像我使用 _scaffoldKey 一样使用 Key 并使用 _scaffoldKey.currentState.openDrawer() 方法在 IconButton 小部件中打开抽屉。

class HomeState extends StatelessWidget {

final GlobalKey<ScaffoldState> _scaffoldKey =  GlobalKey<ScaffoldState>();

 @override
Widget build(BuildContext context) {

return Scaffold(
  key: _scaffoldKey,
         drawer: Drawer(
      child: ListView(
        children: <Widget>[
          ListTile(
            title: Text("Ttem 1"),
            trailing: Icon(Icons.arrow_forward),
          ),
          ListTile(
            title: Text("Item 2"),
            trailing: Icon(Icons.arrow_forward),
          ),
        ],
      ),
    ),
        body: ListView(
         children:[

    Container(
              margin: EdgeInsets.only(left: 15.0,top:100.0),
              child: IconButton(
                icon: Icon(Icons.menu),
                onPressed: () {
                  _scaffoldKey.currentState.openDrawer();
                },

              ),
            ),
            ]
        ),
            );}


   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-12
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2021-09-24
    相关资源
    最近更新 更多