Mig*_*ivo 5 animation dart flutter flutter-layout
我正在尝试创建上面的模型中描述的动态动画。我需要的是:
PageView。动画应如下所示:
开始:以Stack双方为中心。
动画:缩小并滑动必须在头像右侧的文本(具有可变的长度)。
结束:作为模型中的第二个图像。并排滚动,同时下面的内容不断滚动。
思想SliverPersistentHeader与合并CustomMultiChildLayout,但问题是,该文本为中心开始和结束对齐到左边,我可以动态动画这一点。我试图最后删除居中文本的偏移量,但是感觉不正确。
任何帮助或仅此动画的示例将不胜感激。谢谢。
Rém*_*let 10
您将需要一个Sliver来根据滚动偏移量对布局进行动画处理。更具体地说,根据您的情况选择SliverPersistentHeader。
不过,CustomMultiChildLayout并不是必需的,您可以使用补间和align / padding / stuff实现相同的结果。但是,如果您的布局开始变得过于复杂,则可以尝试一下。
诀窍是使用SliverPersistentHeader给定的滚动偏移量来计算当前进度。然后使用该进度将元素定位在其原始位置和最终位置之间。
这是一个原始示例:
class TransitionAppBar extends StatelessWidget {
final Widget avatar;
final Widget title;
const TransitionAppBar({this.avatar, this.title, Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return SliverPersistentHeader(
pinned: true,
delegate: _TransitionAppBarDelegate(
avatar: avatar,
title: title,
),
);
}
}
class _TransitionAppBarDelegate extends SliverPersistentHeaderDelegate {
final _avatarTween =
SizeTween(begin: Size(150.0, 150.0), end: Size(50.0, 50.0));
final _avatarMarginTween =
EdgeInsetsTween(begin: EdgeInsets.zero, end: EdgeInsets.only(left: 10.0));
final _avatarAlignTween =
AlignmentTween(begin: Alignment.topCenter, end: Alignment.centerLeft);
final _titleMarginTween = EdgeInsetsTween(
begin: EdgeInsets.only(top: 150.0 + 5.0),
end: EdgeInsets.only(left: 10.0 + 50.0 + 5.0));
final _titleAlignTween =
AlignmentTween(begin: Alignment.center, end: Alignment.centerLeft);
final Widget avatar;
final Widget title;
_TransitionAppBarDelegate({this.avatar, this.title})
: assert(avatar != null),
assert(title != null);
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
final progress = shrinkOffset / 200.0;
final avatarSize = _avatarTween.lerp(progress);
final avatarMargin = _avatarMarginTween.lerp(progress);
final avatarAlign = _avatarAlignTween.lerp(progress);
final titleMargin = _titleMarginTween.lerp(progress);
final titleAlign = _titleAlignTween.lerp(progress);
return Stack(
fit: StackFit.expand,
children: <Widget>[
Padding(
padding: avatarMargin,
child: Align(
alignment: avatarAlign,
child: SizedBox.fromSize(size: avatarSize, child: avatar),
),
),
Padding(
padding: titleMargin,
child: Align(
alignment: titleAlign,
child: DefaultTextStyle(
style: Theme.of(context).textTheme.title, child: title),
),
)
],
);
}
@override
double get maxExtent => 200.0;
@override
double get minExtent => 100.0;
@override
bool shouldRebuild(_TransitionAppBarDelegate oldDelegate) {
return avatar != oldDelegate.avatar || title != oldDelegate.title;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将其与CustomScrollView一起使用:
Scaffold(
body: CustomScrollView(
slivers: <Widget>[
TransitionAppBar(
avatar: Material(
color: Colors.blue,
elevation: 3.0,
),
title: Text("Hello World"),
),
SliverList(
delegate: SliverChildBuilderDelegate((context, index) {
return ListTile(
title: Text('$index'),
);
}),
)
],
),
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
961 次 |
| 最近记录: |