简体   繁体   English

Flutter:如何制作自定义动画下拉菜单?

[英]Flutter: How to make a custom animated Drop Down Menu?

I'm trying to make a custom drop-down menu in Flutter that is just a simple button that on tap shows a scrollable row of products.我正在尝试在 Flutter 中创建一个自定义下拉菜单,这只是一个简单的按钮,点击时会显示可滚动的产品行。 I've attached some images that better show what I'm referring to.我附上了一些图像,可以更好地显示我所指的内容。 I tried to use an IconButton and an AnimatedContainer but I can't seem to get it working.我尝试使用 IconButton 和 AnimatedContainer 但我似乎无法让它工作。 I was hoping someone would have a better idea on how to do something like this.我希望有人对如何做这样的事情有更好的想法。 在此处输入图像描述

Here is my code so far by the way:顺便说一下,到目前为止,这是我的代码:

class ModelsDropdown extends StatefulWidget {
  final List<Phone> phones;

  ModelsDropdown({@required this.phones});

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

class _ModelsDropdownState extends State<ModelsDropdown> {
  bool _droppedDown;

  @override
  void initState() {
    _droppedDown = false;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
      duration: Duration(milliseconds: 300),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(100.0),
        border: Border.all(width: 2.0)
      ),
      width: 32.0,
      height: 32.0,
      child: Column(
        children: [
          IconButton(
              padding: const EdgeInsets.only(right: 0.0),
              icon: Icon(
                  _droppedDown ? Icons.expand_more : Icons.expand_less
              ),
              color: Colors.black,
              onPressed: () {
                setState(() {
                  _droppedDown = !_droppedDown;
                });
              }
          ),
          _droppedDown ?
          Container(
            width: MediaQuery.of(context).size.width,
            height: 300.0,
            color: Colors.orangeAccent,
          ) :
          SizedBox.shrink()
        ],
      ),
    );
  }
}

With the container at the bottom, it doesn't even work.容器在底部,它甚至不起作用。 I get an overflow error.我收到溢出错误。 Any help is very much appreciated.很感谢任何形式的帮助。 Thanks a lot, everyone.非常感谢大家。

I Think you should Change The Below Container with an AnimatedCrossFade.我认为您应该使用 AnimatedCrossFade 更改下面的容器。 Animated Cross Fade Has Two Child First and Second... https://api.flutter.dev/flutter/widgets/AnimatedCrossFade-class.html动画交叉淡入淡出有两个孩子,第一和第二...

Dont set Height for Your Child... its Works Perfectly...不要为您的孩子设置高度......它的作品完美......

AnimatedCrossFade(
 duration: const Duration(seconds: 3),
 firstChild: Container(),  // When you don't want to show menu.. 
 secondChild: menu,
 crossFadeState: _first ? CrossFadeState.showFirst : CrossFadeState.showSecond,
)

with this method you don't need Animated Container at top... remove that.(return Column)... with Animated cross Fade You don't need to Know The Height of widget and its for works dynamic Heights使用这种方法,您不需要顶部的动画容器...删除它。(返回列)...使用动画交叉淡入淡出您不需要知道小部件的高度及其作品动态高度

-------------- if You Want to use Your Code and Use Fixed Height ---------------- --------------如果您想使用您的代码并使用固定高度----------------

width: _droppedDown ? YourWidth : 32.0,
height: _droppedDown ? 300 : 32.0,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM