【问题标题】:AppBar with top (vertical) drawer带有顶部(垂直)抽屉的 AppBar
【发布时间】:2020-06-28 11:30:22
【问题描述】:

我正在尝试从 AppBar 底部制作抽屉。这是我到目前为止的地方

  double height = 5;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text('Drawer'),
        actions: <Widget>[
          FlatButton(
            child: Text('press'),
            onPressed: () {
              setState(() {
                height == 5 ? height = 150 : height = 5;
              });
            },
          )
        ],
      ),
      body: Column(
        children: <Widget>[
          AnimatedContainer(
            color: Colors.green,
            duration: Duration(milliseconds: 500),
            height: height,
            child: ListView(
              children: <Widget>[Text('hello')],
            ),
          ),
          Container(
            height: 200,
            color: Colors.red,
          )
        ],
      ),
    );
  }

它以某种方式工作正常,但是这个解决方案移动了底部容器,我需要那个抽屉在它后面,或者换句话说,在红色容器的顶部展开。另一个问题是,当您将Column 和一些孩子放在动画容器中时,内容就会溢出。最好的解决方案是将那个抽屉移到 AppBar 后面而不是改变我猜的大小

这是我尝试修复溢出但没有用的方法

AnimatedContainer(
            color: Colors.green,
            duration: Duration(milliseconds: 500),
            height: height,
            child: height == 5
                ? Container(color: Colors.green)
                : Column(
                    children: <Widget>[Text('hello')],
                  ),
          )

【问题讨论】:

  • 如果使用 Column , Stack 会怎样?
  • @dev-aentgs 我尝试使用堆栈并将列替换为堆栈使动画容器消失。你可以试试..我在我的问题中制作了简单的可重现代码..
  • 如果订单发生变化,Stack 中的最后一个小部件将位于顶部。会尝试

标签: flutter dart


【解决方案1】:

试试这个,只需将列小部件更改为堆栈小部件,并在代码中交换容器和动画容器的位置。

double height = 5;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text('Drawer'),
        actions: <Widget>[
          FlatButton(
            child: Text('press'),
            onPressed: () {
              setState(() {
                height == 5 ? height = MediaQuery.of(context).size.height : height = 5;
              });
            },
          )
        ],
      ),
      body: Stack(
        children: <Widget>[
          Container(
            height: 200,
            color: Colors.red,
          ),
          AnimatedContainer(
            color: Colors.green,
            duration: Duration(milliseconds: 500),
            height: height,
            child: ListView(
              children: <Widget>[Text('hello')],
            ),
          )
        ],
      ),
    );

  }

【讨论】:

  • 很好,修复了一件事,但仍然存在溢出。如果将 ListView 替换为 AnimatedContainer 中的 column 和一些子项,则它会溢出。我把列表视图放在那里只是为了让它工作
  • 它溢出是因为 AnimatedContainer 的最终高度是固定的。你能添加一些图片或更多关于最终结果的细节吗? @delmin
  • 你也可以使用scrollview来避免溢出
  • @dev-aentgs 最终结果将有一些用于过滤器的控制视觉效果,例如按钮单选按钮滑块和复选框。这个解决方案即使在视觉上有效,也不适用于其中的小部件。我有用我尝试过的方法更新了我的问题。
  • @delmin physics: NeverScrollableScrollPhysics(), 可用于防止滚动
【解决方案2】:

以@delmin 和@niteesh 的代码为基础,添加了physics: NeverScrollableScrollPhysics(),height = MediaQuery.of(context).size.height

class AnimatedDrawerDemo extends StatefulWidget {
  @override
  _AnimatedDrawerDemoState createState() => _AnimatedDrawerDemoState();
}

class _AnimatedDrawerDemoState extends State<AnimatedDrawerDemo> {
  double height = 5;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text('Drawer'),
        actions: <Widget>[
          FlatButton(
            child: Text('press'),
            onPressed: () {
              setState(() {
                height == 5
                    ? height = MediaQuery.of(context).size.height
                    : height = 5;
              });
            },
          )
        ],
      ),
      body: Stack(
        children: <Widget>[
          Container(
            height: 200,
            color: Colors.red,
          ),
          AnimatedContainer(
            color: Colors.green,
            duration: Duration(milliseconds: 500),
            height: height,
            child: ListView(
              physics: NeverScrollableScrollPhysics(),
              children: <Widget>[
                DrawerHeader(
                  child: Text('Drawer Header'),
                  decoration: BoxDecoration(
                    color: Colors.blue,
                  ),
                ),
                ListTile(
                  title: Text('Item 1'),
                  onTap: () {
                    // Update the state of the app
                    // ...
                    // Then close the drawer
                    //Navigator.pop(context);
                  },
                ),
                ListTile(
                  title: Text('Item 1'),
                  onTap: () {
                    // Update the state of the app
                    // ...
                    // Then close the drawer
                    //Navigator.pop(context);
                  },
                ),
                ListTile(
                  title: Text('Item 1'),
                  onTap: () {
                    // Update the state of the app
                    // ...
                    // Then close the drawer
                    //Navigator.pop(context);
                  },
                ),
                ListTile(
                  title: Text('Item 1'),
                  onTap: () {
                    // Update the state of the app
                    // ...
                    // Then close the drawer
                    //Navigator.pop(context);
                  },
                ),
                ListTile(
                  title: Text('Item 1'),
                  onTap: () {
                    // Update the state of the app
                    // ...
                    // Then close the drawer
                    //Navigator.pop(context);
                  },
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

【讨论】:

  • 嗯它确实有效。关于它的某些东西感觉不对,但它有效......我将关闭这个问题,然后回答......为了公平起见,我会将第一个答案标记为已回答。很抱歉
  • 不必向@delmin 道歉,这是当之无愧的。如果您想出正确的方法来做到这一点,请在以后用您的方法更新问题。
猜你喜欢
  • 2019-05-19
  • 1970-01-01
  • 2018-12-07
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
相关资源
最近更新 更多