所以,我正在用Ffltter构建一个应用程序,在主页上,我想有一个更大的AppBar,而在其他应用程序中,AppBar恢复到常规大小,我设法实现了前者良好而流畅的动画,但我无法让后者工作.
下面是我正在做的一段代码片段:
CustomAppBar类: 导入‘包装:Flutter 翼/material .dart’;
class CustomAppBar extends StatefulWidget implements PreferredSizeWidget{
double height;
String title;
Color color;
Color textColor;
CustomAppBar(this.height, this.title, this.color, this.textColor, {super.key});
@override
State<CustomAppBar> createState() => _CustomAppBarState();
@override
// TODO: implement preferredSize
Size get preferredSize => Size.fromHeight(height);
}
class _CustomAppBarState extends State<CustomAppBar> {
@override
Widget build(BuildContext context) {
return PreferredSize(
preferredSize: Size.fromHeight(widget.height),
child: AnimatedContainer(
duration: const Duration(milliseconds: 500),
curve: Curves.decelerate,
height: widget.height,
child: Container(
color: Colors.green,
),
),
);
}
}
然后在Scaffold中,我按如下方式分配AppBar:
appBar: CustomAppBar(height, getTitle(currentIndex), Colors.purple, Colors.white),
但我最终得到了这样一个动画:
请注意,当单击第二个导航项时,它是如何从高度260跳到大约120,然后从120到90(数字是近似值)进行动画显示的
更新:我try 将CustomAppBar添加到‘Body’中,而不是‘appbar’中,它工作起来很奇怪,但我很想知道是什么导致了这个问题.