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

Flutter AppBar反向动画时标题出现跳动异常

Flutter 自定义AppBar动画异常问题

我正在使用Flutter开发一款应用,需求为首页采用大尺寸AppBar,其他页面恢复常规尺寸的AppBar。参考此前的实现方案完成了自定义AppBar,但出现动画问题:切换页面时标题文本并非平滑动画到目标位置,而是直接跳回。

自定义AppBar类代码

import 'package:flutter/material.dart';

class CustomAppBar extends StatefulWidget implements PreferredSizeWidget{
  double height;
  String title;
  Color color;
  Color textColor;
  BuildContext context;

  CustomAppBar(this.height, this.title, this.color, this.textColor, this.context, {super.key});

  @override
  State<CustomAppBar> createState() => _CustomAppBarState();

  @override
  Size get preferredSize => Size.fromHeight(MediaQuery.of(context).size.height * 0.22);
}

class _CustomAppBarState extends State<CustomAppBar> {
  @override
  Widget build(BuildContext context) {
    return PreferredSize(
      preferredSize: Size.fromHeight(widget.height),
      child: AnimatedContainer(
        padding: const EdgeInsets.only(bottom: 10),
        duration: const Duration(milliseconds: 1000),
        curve: Curves.decelerate,
        height: widget.height,
        child: AppBar(
          toolbarHeight: widget.height,
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          elevation: 5,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                bottomRight: Radius.circular(30), bottomLeft: Radius.circular(30)),
          ),
          shadowColor: Colors.black,
          title: SizedBox(
            height: widget.height,
            child: Center(
              child: Text(
                widget.title,
                textAlign: TextAlign.center,
                style: const TextStyle(
                  fontSize: 30,
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
        ),

      ),
    );
  }

}

Scaffold中AppBar的设置代码

appBar: CustomAppBar(height, getTitle(currentIndex), Colors.purple, Colors.white, context),

动画异常演示

动画演示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 18:39:56