ir2*_*pid 7 flutter flutter-animation
我使用下面的代码摇动一个小部件,但效果是一次,
我如何让它以一定的时间间隔循环运行。我相信可以通过更改密钥来完成,但这是最终的,无法更改。
import 'package:flutter/material.dart';
@immutable
class ShakeWidget extends StatelessWidget {
final Duration duration;
final double deltaX;
final Widget child;
final Curve curve;
const ShakeWidget({
Key key,
this.duration = const Duration(milliseconds: 500),
this.deltaX = 20,
this.curve = Curves.bounceOut,
this.child,
}) : super(key: key);
/// convert 0-1 to 0-1-0
double shake(double animation) =>
2 * (0.5 - (0.5 - curve.transform(animation)).abs());
@override
Widget build(BuildContext context) {
return TweenAnimationBuilder<double>(
key: key,
tween: Tween(begin: 0.0, end: 1.0),
duration: duration,
builder: (context, animation, child) => Transform.translate(
offset: Offset(deltaX * shake(animation), 0),
child: child,
),
child: child,
);
}
}
Run Code Online (Sandbox Code Playgroud)
Khe*_*rel 11
您需要使用 AnimationController 并在控制器完成时调用重复
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: SafeArea(
child: ShakeWidget(
child: Text('Hello world'),
),
),
);
}
}
class ShakeWidget extends StatefulWidget {
const ShakeWidget({
super.key,
this.duration = const Duration(milliseconds: 500),
this.deltaX = 20,
this.curve = Curves.bounceOut,
required this.child,
});
final Duration duration;
final double deltaX;
final Widget child;
final Curve curve;
@override
State<ShakeWidget> createState() => _ShakeWidgetState();
}
class _ShakeWidgetState extends State<ShakeWidget>
with SingleTickerProviderStateMixin {
late AnimationController controller;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: widget.duration,
vsync: this,
)
..forward()
..addListener(() {
if (controller.isCompleted) {
controller.repeat();
}
});
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
/// convert 0-1 to 0-1-0
double shake(double value) =>
2 * (0.5 - (0.5 - widget.curve.transform(value)).abs());
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: controller,
builder: (context, child) => Transform.translate(
offset: Offset(widget.deltaX * shake(controller.value), 0),
child: child,
),
child: widget.child,
);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
使用 AnimationController,如下所示:
AnimationController(
duration: const Duration(milliseconds: 300),
vsync: this,
)..repeat();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17490 次 |
| 最近记录: |