Tiktok playing animation flutter
A popular topic!
To create a TikTok-like playing animation in Flutter, you can use the following approaches:
- AnimatedBuilder: This widget rebuilds its child when the animation changes. You can use it to create a playing animation by updating the animation's value and rebuilding the child widget.
import 'package:flutter/material.dart';
class PlayingAnimation extends StatefulWidget { @override _PlayingAnimationState createState() => _PlayingAnimationState(); }
class _PlayingAnimationState extends State
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 3),
);
_animation = Tween
@override Widget build(BuildContext context) { return AnimatedBuilder( animation: _animation, child: YourWidget(), builder: (context, child) { return Transform.translate( offset: Offset(0, _animation.value * 100), child: child, ); }, ); } }
In this example, the `AnimatedBuilder` widget rebuilds its child when the animation changes. The `Transform.translate` widget is used to move the child widget up and down based on the animation's value.
2. **ValueListenableBuilder**: This widget rebuilds its child when the value of the `ValueListenable` changes. You can use it to create a playing animation by updating the animation's value and rebuilding the child widget.
```dart
import 'package:flutter/material.dart';
class PlayingAnimation extends StatefulWidget {
@override
_PlayingAnimationState createState() => _PlayingAnimationState();
}
class _PlayingAnimationState extends State<PlayingAnimation> with TickerProviderStateMixin {
AnimationController _controller;
Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 3),
);
_animation = Tween<double>(begin: 0, end: 1).animate(_controller);
}
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: _animation,
builder: (context, value, child) {
return Transform.translate(
offset: Offset(0, value * 100),
child: child,
);
},
child: YourWidget(),
);
}
}
In this example, the ValueListenableBuilder
widget rebuilds its child when the animation's value changes. The Transform.translate
widget is used to move the child widget up and down based on the animation's value.
- StaggeredGrid: This widget can be used to create a playing animation by staggering the grid items.
import 'package:flutter/material.dart';
class PlayingAnimation extends StatefulWidget { @override _PlayingAnimationState createState() => _PlayingAnimationState(); }
class _PlayingAnimationState extends State
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 3),
);
_animation = Tween
@override Widget build(BuildContext context) { return StaggeredGrid.count( crossAxisCount: 3, children: List.generate(9, (index) { return AnimatedBuilder( animation: _animation, child: YourWidget(), builder: (context, child) { return Transform.translate( offset: Offset(0, _animation.value * 100), child: child, ); }, ); }), ); } }
In this example, the `StaggeredGrid` widget is used to create a 3x3 grid. Each grid item is an `AnimatedBuilder` widget that rebuilds its child when the animation changes. The `Transform.translate` widget is used to move each grid item up and down based on the animation's value.
These are just a few examples of how you can create a playing animation in Flutter. You can customize the animation to fit your specific needs by adjusting the animation's duration, curve, and other properties.