112、Flutter实现图片放大缩小的动画小

Flutter是通过Animation组件来实现动画效果的

main.dart文件中的内容与上一篇显示的代码相同

这一篇博客主要关注放大缩小的动画效果

1、WelcomeScreen  widget继承了StatefulWidget组件,

在initState方法中设置动画效果

 

2、动画效果主要通过

Animation组件来实现

AnimationController设置动画执行持续的时间长短
controller = AnimationController(
        duration: Duration(seconds: 1), vsync: this); //AnimationController

 

CurvedAnimation设置动画的效果
animation = CurvedAnimation(parent: controller, curve: Curves.decelerate);

 

   controller.forward()设置动画的执行,并且添加监听器

   controller.forward()之后的状态是完成的即

    AnimationStatus.completed

   controller.reverse(from: 1.0);

   之后的状态是丢失的即

    AnimationStatus.dismissed

    controller.forward();

    animation.addStatusListener((status){
      if (status == AnimationStatus.completed){
        controller.reverse(from: 1.0);
      }else if (status == AnimationStatus.dismissed){
        controller.forward();
      }
    });

    controller.addListener(() {
      setState(() {});
      print(animation.value);
    });

 

 

 

   完整的代码如下所示:

import 'package:flutter/material.dart';
import 'login_screen.dart';
import 'registration_screen.dart';

class WelcomeScreen extends StatefulWidget {
  static String id = 'welcome_screen';

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

class _WelcomeScreenState extends State
    with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation animation;

  @override
  void initState() {
    super.initState();

    controller = AnimationController(
        duration: Duration(seconds: 1), vsync: this); //AnimationController

//    animation = ColorTween(begin: Colors.blueGrey, end: Colors.white)
//        .animate(controller);

    animation = CurvedAnimation(parent: controller, curve: Curves.decelerate);

    controller.forward();

    animation.addStatusListener((status){
      if (status == AnimationStatus.completed){
        controller.reverse(from: 1.0);
      }else if (status == AnimationStatus.dismissed){
        controller.forward();
      }
    });

    controller.addListener(() {
      setState(() {});
      print(animation.value);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 24.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Row(
              children: [
                Hero(
                  tag: 'logo',
                  child: Container(
                    child: Image.asset('images/logo.png'),
                    height: animation.value * 100,
                  ),
                ),
                Text(
                  'Falsh Chat',
                  style: TextStyle(
                    fontSize: 45.0,
                    fontWeight: FontWeight.w900,
                  ),
                ),
              ],
            ),
            SizedBox(
              height: 48.0,
            ),
            Padding(
              padding: EdgeInsets.symmetric(vertical: 16.0),
              child: Material(
                elevation: 5.0,
                color: Colors.lightBlueAccent,
                borderRadius: BorderRadius.circular(30.0),
                child: MaterialButton(
                  onPressed: () {
                    Navigator.pushNamed(context, LoginScreen.id);
                  },
                  minWidth: 200.0,
                  height: 42.0,
                  child: Text(
                    'Log In',
                  ),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(vertical: 16.0),
              child: Material(
                color: Colors.blueAccent,
                borderRadius: BorderRadius.circular(30.0),
                elevation: 5.0,
                child: MaterialButton(
                  onPressed: () {
                    Navigator.pushNamed(context, RegistrationScreen.id);
                  },
                  minWidth: 200.0,
                  height: 42.0,
                  child: Text(
                    'Register',
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

整个效果如下图所示:

112、Flutter实现图片放大缩小的动画小_第1张图片112、Flutter实现图片放大缩小的动画小_第2张图片112、Flutter实现图片放大缩小的动画小_第3张图片

你可能感兴趣的:(Flutter)