Flutter Animation AnimatedBuilder

摘要:
runApp(MyApp());(上下文)=>@overridevoinitState(){super.initState();animation=Tween(begin:200).animate(animationController);堆栈(childs:Container(child:RaisedButton(child:
Flutter AnimatedBuilder

创建动画的widget

Key key,

@required Listenable animation,

@required this.builder,

this.child,

animation:Animationcontroller //动画

child 动画作用的view

builder:每次controller值改变都会回到builder 重新生成view


import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/buildAnimation',
      routes: {
        '/':(context)=>AnimationDemo(),
        '/test':(context)=>TestAnimation(),
        '/buildAnimation':(context)=>BuildAnimation(),
      },
    );
  }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class BuildAnimation extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return BuildAnimationState();
  }
}

class BuildAnimationState extends State<BuildAnimation> with TickerProviderStateMixin{
  AnimationController animationController;
  var animation;

  @override
  void initState() {
    super.initState();
    animationController = AnimationController(
        duration: Duration(seconds: 2),
        vsync: this);
    animation = Tween(begin: 20, end: 200).animate(animationController);
    animationController.forward();   //放到某个其他地方去启动, 会唤起Builder
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('builder'),),
      body: Container(
         double.infinity,
        height: double.infinity,
        child: Stack(
          children: <Widget>[
            AnimatedBuilder(
              animation: animation,
              builder: (context, Widget child){  //child 可以定义要动的东西....这里先暂时省略了.
                return Positioned(
                  left: animation.value/1.0,
                  top: animation.value/1.0,
                  child: Container(child: RaisedButton(
                  child: Text('abc'),onPressed: (){
                    print(animation.value);
                  }),),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TestAnimation extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return TestAnimationState();
  }
}

class TestAnimationState extends State<TestAnimation> with TickerProviderStateMixin{
  AnimationController animationController;
  Tween positionTween = Tween(begin: 20, end: 200);
  var animation;

  @override
  void initState() {
    super.initState();
    animationController = AnimationController(
        vsync: this,
        duration: Duration(seconds: 1),
    );
     animation = positionTween.animate(animationController);

     animationController.addListener((){
       print(animationController.value);
     });
     animationController.addStatusListener((value){
       print('status: $value');
     });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('test'),),
      body: Container(
         double.infinity,
        height: double.infinity,
        color: Colors.black12,
        child: TestAnimationDemo(animationController: animationController,animation:animation),
      ),
    );
  }
}

class TestAnimationDemo extends AnimatedWidget {
  AnimationController animationController;
  Animation animation;
  TestAnimationDemo({this.animationController, this.animation}):super(listenable:animationController);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Positioned(
          left: animation.value/1.0,
          top: animation.value/1.0,
          child: Container(
            child: IconButton(icon: Icon(Icons.forward), onPressed: (){
              animationController.forward();
            }),
          ),
        ),
      ],
    );
  }
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////
class AnimationDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('AnimationDemo'),
          elevation: 0.0,
        ),
        body: AnimationDemoHome());
  }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
class AnimationDemoHome extends StatefulWidget {
  @override
  _AnimationDemoHomeState createState() => _AnimationDemoHomeState();
}

class _AnimationDemoHomeState extends State<AnimationDemoHome>
    with TickerProviderStateMixin {
  AnimationController animationDemoController;
  Animation animation;
  Animation animationColor;
  CurvedAnimation curve;

  @override
  void initState() {
    super.initState();
    animationDemoController = AnimationController(
      // value: 32.0,
      // lowerBound: 32.0,
      // upperBound: 100.0,
      duration: Duration(milliseconds: 1000),
      vsync: this,
    );

    curve = CurvedAnimation(
        parent: animationDemoController, curve: Curves.bounceOut);

    animation = Tween(begin: 32.0, end: 100.0).animate(curve);
    animationColor =
        ColorTween(begin: Colors.red, end: Colors.red[900]).animate(curve);
    // animationDemoController.addListener(() {
    //   // print('${animationDemoController.value}');
    //   setState(() {});
    // });
    animationDemoController.addStatusListener((AnimationStatus status) {
      print(status);
    });
    // animationDemoController.forward();
  }

  @override
  void dispose() {
    super.dispose();
    animationDemoController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: AnimatedHeart(
        animations: [
          animation,
          animationColor,
        ],
        controller: animationDemoController,
      ),
    );
  }
}


class AnimatedHeart extends AnimatedWidget {
  final List animations;
  final AnimationController controller;

  AnimatedHeart({
    this.animations,
    this.controller,
  }) : super(listenable: controller);

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: Icon(Icons.favorite),
      iconSize: animations[0].value,
      color: animations[1].value,
      onPressed: () {
        switch (controller.status) {
          case AnimationStatus.completed:
            controller.reverse();
            break;
          default:
            controller.forward();
        }
      },
    );
  }
}

  

 

免责声明:文章转载自《Flutter Animation AnimatedBuilder》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇科大讯飞语音识别LSTM 详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[django]l利用xlrd实现xls文件导入数据

代码: #coding:utf-8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www.settings") ''' Django 版本大于等于1.7的时候,需要加上下面两句 import django django.setup() 否则会抛出错误 dja...

pymysql 线程安全pymysqlpool

# -*-coding: utf-8-*- # Author : Christopher Lee # License: Apache License # File : test_example.py # Date : 2017-06-18 01-23 # Version: 0.0.1 # Description: simple test...

在Delphi中动态地使用SQL查询语句 Adoquery sql 参数 冒号

  http://blog.sina.com.cn/s/blog_732d14b50100qnt8.html   在Delphi中动态地使用SQL查询语句 在一般的数据库管理系统中,通常都需要应用SQL查询语句来提高程序的动态特性。下面介绍如何在Delphi中实现这种功能。在Delphi中,使用SQL查询语句的途径是:在窗体中置入TQuery构件,设置其...

echarts markLine 辅助线非直线设置

效果图: 用例option: option = { title: { text: '未来一周气温变化', subtext: '纯属虚构' }, tooltip: { trigger: 'axis' }, legend: { data:...

videojs做直播、弹幕

从上一年开始,我们开始接触直播,现在直播成本真的很低,很多CDN供应商都有提供,本文只是大概讲述播放器这个话题。 开始调研 播放格式,我挑了三种。分别是HLS,RTMP,HTTP-FLV。 下面简单说说区别,如果不做移动端,HTTP-FLV是最优选择,也是当前主流直播网站所用的格式。HLS在videojs中也有js的实现,即使是不支持hls,也能让它支持,...

MYSQL,触发器,实现两个表共用ID不重复

前后台没有分开,为了区分前后台用户,所以分表,但是ID不能重复,因为关联了权限表. 这里实现后台用户表使用奇数ID 前台用户表使用偶数ID MYSQL 没有sequence  SET @@auto_increment_offsetSET @@auto_increment_increment 也只能在当前查询有效.所以这也是权宜之计  SET FOREIGN...