继承AnimationWidget可以自动更新控件,不用监听了!

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
AnimationController animationController;
CurvedAnimation curve;
Animation animationSize;
Animation animationColor;
Animation animationIcon;
@override
void initState() {
animationController = AnimationController(
vsync: this,
duration: Duration(
milliseconds: 2000,
));
curve =
CurvedAnimation(parent: animationController, curve: Curves.easeInOut);
animationSize = Tween(
begin: 78.0,
end: 368.0,
).animate(curve);
animationColor =
ColorTween(begin: Colors.redAccent, end: Colors.green).animate(curve);
animationIcon = Tween(begin: 0.0,end: 360.0).animate(curve);

animationController.addStatusListener((AnimationStatus status) {
print(status);
});

// TODO: implement initState
super.initState();
}

@override
void dispose() {
// TODO: implement dispose
animationController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(''),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
AnimationArrow(
animations: [animationSize, animationColor,animationIcon],
controller: animationController,
),
],
),
);
}
}

class AnimationArrow extends AnimatedWidget {
final List animations;
final AnimationController controller;
AnimationArrow({this.animations, this.controller})
: super(listenable: controller);

@override
Widget build(BuildContext context) {
List<Widget> iconWidget = [Icon(Icons.map),Icon(Icons.security)];
int index=1;
// TODO: implement build
return IconButton(
icon: iconWidget[index],
iconSize: animations[0].value,
color: animations[1].value,
onPressed: () {
switch (controller.status) {
case AnimationStatus.completed:
controller.reverse();
break;
default:
controller.forward();
break;
}
});
}
}
posted @ 2019-06-07 16:03  braveheart007  阅读(268)  评论(0编辑  收藏  举报