gpt4 book ai didi

flutter - 加载数据时制作骨架屏幕的最简单方法

转载 作者:IT老高 更新时间:2023-10-28 12:40:49 25 4
gpt4 key购买 nike

我是 Flutter 的新手,我正在尝试制作与片段中包含的示例类似的东西。用飞镖和 flutter 最简单的方法是什么。

基本上,当来自服务器的数据仍在下载时,我想将闪耀动画应用于容器。

谢谢

div {
margin: auto;
width: 500px;
height: 600px; /* change height to see repeat-y behavior */

background-image:
radial-gradient( circle 50px at 50px 50px, lightgray 99%, transparent 0 ),
linear-gradient( 100deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.5) 50%, rgba(255, 255, 255, 0) 80% ),
linear-gradient( lightgray 20px, transparent 0 ),
linear-gradient( lightgray 20px, transparent 0 ),
linear-gradient( lightgray 20px, transparent 0 ),
linear-gradient( lightgray 20px, transparent 0 );

background-repeat: repeat-y;

background-size:
100px 200px, /* circle */
50px 200px, /* highlight */
150px 200px,
350px 200px,
300px 200px,
250px 200px;

background-position:
0 0, /* circle */
0 0, /* highlight */
120px 0,
120px 40px,
120px 80px,
120px 120px;

animation: shine 1s infinite;
}

@keyframes shine {
to {
background-position:
0 0,
100% 0, /* move highlight to right */
120px 0,
120px 40px,
120px 80px,
120px 120px;
}
}
<div></div>

最佳答案

您可以将 StackPositioned.fillFractionallySizedBox 结合起来,以将此类渐变定位在另一个小部件的顶部。

然后你可以将它组合到一个 AnimationController、一个 AnimatedBuilder 和一个 AlignFractionallySizedBox 来制作动画梯度随时间变化的位置。

你会有这个:

class LoadAnimation extends StatefulWidget {
final Widget child;

LoadAnimation({@required this.child, Key key}) : super(key: key);

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

class _LoadAnimationState extends State<LoadAnimation>
with SingleTickerProviderStateMixin {
AnimationController controller;

@override
void initState() {
controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
)..repeat();
super.initState();
}

@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
widget.child,
Positioned.fill(
child: ClipRect(
child: AnimatedBuilder(
animation: controller,
builder: (context, child) {
return FractionallySizedBox(
widthFactor: .2,
alignment: AlignmentGeometryTween(
begin: Alignment(-1.0 - .2 * 3, .0),
end: Alignment(1.0 + .2 * 3, .0),
).chain(CurveTween(curve: Curves.easeOut)).evaluate(controller),
child: child,
);
},
child: const DecoratedBox(
decoration: const BoxDecoration(
gradient: const LinearGradient(
colors: const [
Color.fromARGB(0, 255, 255, 255),
Colors.white,
],
),
),
),
)),
),
],
);
}
}

然后您可以通过包装任何给定的小部件来使用它:

LoadAnimation(
child: Container(
height: 100.0,
width: 200.0,
color: Colors.lime,
),
),

enter image description here

关于flutter - 加载数据时制作骨架屏幕的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51677301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com