Middle
Pastāstiet man par animāciju Flutter.
sobes.tech AI
Atbilde no AI
Flutter animācijas iedala galvenajos divos veidos: implicitās un explicitās.
-
Implicitās animācijas ir vienkāršas animācijas, kas automātiski pārvalda widget stāvokļa izmaiņas. Piemēram, AnimatedContainer gludi maina savus parametrus (krāsu, izmēru), nemainot nepieciešamību rakstīt animācijas kontrolierus.
-
Explicitās animācijas nodrošina pilnīgu kontroli pār animācijas procesu. Tām izmanto AnimationController, kas pārvalda laiku un progresu. Ar Tween un Animation palīdzību var noteikt, kā tieši mainās vērtības laikā.
Implicitās animācijas piemērs:
AnimatedContainer(
duration: Duration(seconds: 1),
width: _selected ? 200 : 100,
height: _selected ? 200 : 100,
color: _selected ? Colors.blue : Colors.red,
child: ...,
)
Explicitās animācijas piemērs:
class MyAnimatedWidget extends StatefulWidget {
@override
_MyAnimatedWidgetState createState() => _MyAnimatedWidgetState();
}
class _MyAnimatedWidgetState extends State<MyAnimatedWidget> with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this, duration: Duration(seconds: 2));
_animation = Tween<double>(begin: 0, end: 300).animate(_controller);
_controller.forward();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Container(
width: _animation.value,
height: _animation.value,
color: Colors.blue,
);
},
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
Kopumā Flutter nodrošina jaudīgus līdzekļus gludām un atsaucīgām animācijām, kas uzlabo lietotāja pieredzi.