【问题标题】:flutter opening a drawer using a button inside a scaffold body使用脚手架主体内的按钮颤动打开抽屉
【发布时间】:2021-08-26 02:45:42
【问题描述】:

我有什么 一个带有 onpress 的自定义按钮来打开一个抽屉,我的 build sn-p : (inside MyClassState)

  Widget build(BuildContext context) => Scaffold(
      key: _key,
      body: Container(
        child: Column(
                  children: [
                    Row(children: [

ElevatedButton(
  child: Padding(
    padding: const EdgeInsets.all(5.0),
    child: Icon(
      Icons.settings,
      size: 38,
      color: Colors.white,
    ),
  ),
  onPressed: () => _key.currentState?.openEndDrawer(),
), 

]),]),))

使用方法globalkey _key(在阅读了一些解决方案here之后)

Class MyClassState extends State<MyClass> {
    GlobalKey<ScaffoldState> _key = GlobalKey();
  ...
}

我的预期

抽屉在按下/点击时打开

当前行为结果

点击时没有任何反应,但我可以使用滑动手势打开抽屉。

在这种情况下我做错了什么?

【问题讨论】:

    标签: flutter mobile navigation-drawer


    【解决方案1】:

    你没有在脚手架中声明endDrawer,这是你更新的代码

     Widget build(BuildContext context) => Scaffold(
          key: _key,
          endDrawer: Drawer(      /// this is missing in your code
            child: Container(
              width: 200,
              color: Colors.red,
            ),
          ),
          body: Container(
            child: Column(
                      children: [
                        Row(children: [
    
    ElevatedButton(
      child: Padding(
        padding: const EdgeInsets.all(5.0),
        child: Icon(
          Icons.settings,
          size: 38,
          color: Colors.white,
        ),
      ),
      onPressed: () => _key.currentState?.openEndDrawer(),
    ), 
    
    ]),]),))
    

    【讨论】:

    • OHHH 我确实声明了它,但我声明为“抽屉:抽屉(...”请注意我可以使用从左到右滑动的手势打开抽屉。这就是为什么它让我想知道为什么它不点击按钮。endDrawer 和抽屉之间有区别吗?
    • @vontdeux 一个从左侧打开,第二个从右侧打开。这就是DrawerEndDrawer 之间的区别
    猜你喜欢
    • 2021-02-08
    • 2021-02-17
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2015-05-17
    • 1970-01-01
    相关资源
    最近更新 更多