You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter:如何根据_selectAll值设置AnimatedContainer的动画方向

解决AnimatedContainer动画方向不符的问题

问题出在两个地方:一是你当前的height赋值逻辑和期望的展开/收起状态相反,二是默认AnimatedContainer的高度动画以顶部为锚点变化,导致方向不对。要实现**_selectAll为true时从底部向上展开,false时从顶部向下收起**的效果,按以下方式修改:

  1. 反转height的条件判断,让选中状态对应展开的高度,未选中对应收起的高度
  2. 用Align组件包裹AnimatedContainer,设置对齐方式为底部居中,让容器底部固定,高度变化时从顶部方向扩展/收缩

修改后的完整代码:

Align(
  alignment: Alignment.bottomCenter,
  child: AnimatedContainer(
    width: ScreenSize.getScreenWidth(context),
    height: _selectAll ? bottomBarHeight : 10,
    color: _selectAll ? Colors.red : Colors.green,
    duration: const Duration(seconds: 2),
    curve: Curves.fastLinearToSlowEaseIn,
    child: const FlutterLogo(size: 75),
  ),
)

效果说明:

  • 当_selectAll变为true时,容器从高度10向上扩展至bottomBarHeight(底部固定,顶部上移,实现从底部向上展开)
  • 当_selectAll变为false时,容器从bottomBarHeight向下收缩至10(顶部下移,底部固定,实现从顶部向下收起)

内容的提问来源于stack exchange,提问作者Orl13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 02:52:16