有点意思,

import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart'; main()=>runApp(MaterialApp(
home: MyApp(),
)); class MyApp extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return MyAppState();
}
} class MyAppState extends State<MyApp> with TickerProviderStateMixin{
GameController gameController;
AnimationController animationController;
List bricks = <Brick>[]; @override
void initState() {
super.initState();
animationController = AnimationController(vsync: this, duration: Duration(seconds: 2));
gameController = GameController(animationController);
}
@override
Widget build(BuildContext context) {
return SafeArea(child:Scaffold(
body:Container(
width: double.infinity,
height: double.infinity,
child: Column(
children: <Widget>[
Container(child: RaisedButton(child: Text('BTN'),onPressed: (){
gameController.addData();
}),),
StreamBuilder(
stream: gameController.dataBloc.dataBloc.stream,
builder: (context, snapshot){
if(snapshot.hasData){
List dataList = snapshot.data;
bricks = <Brick>[];
dataList.forEach((brickModel){
bricks.add(Brick(brickModel: brickModel,));
});
return Container(
width:350, height:400,
child: Stack(
children: bricks,
),); }else{
return Center(child: CircularProgressIndicator(),);
}
},
),
],
),
)));
}
} class Brick extends StatelessWidget {
Brick({this.brickModel});
BrickModel brickModel;
@override
Widget build(BuildContext context) {
print('$hashCode, x: ${brickModel.x}, y: ${brickModel.y}');
return Positioned(
left: brickModel.addressX, top: brickModel.addressY,
// left: 100, top: 100,
child: Container(color: Colors.red,child: Text('${brickModel.x}'),),
);
}
} class GameController {
GameController(animationController){
this.animationController = animationController;
this.animationController.addListener((){
brickModels.forEach((brickAddress){
brickAddress.update();
});
dataBloc.dataBloc.add(brickModels);
});
this.animationController.addStatusListener((status){
print('status: $status');
});
} AnimationController animationController;
Animation xAnimation;
DataBloc dataBloc = DataBloc();
List brickModels = <BrickModel>[]; addData(){
brickModels.add(BrickModel(x: 120, y: 130, oldX: 30, oldY: 30,addressX: 50, addressY: 50, animationController: animationController));
brickModels.add(BrickModel(x: 200, y: 40, oldX: 130, oldY: 60,addressX: 50, addressY: 50, animationController: animationController)); brickModels.forEach((brickAnimation){
brickAnimation..createAnimation();
});
animationController.forward();
}
} class BrickModel {
double x, y, oldX, oldY, addressX, addressY;
Animation xAnimation, yAnimation;
AnimationController animationController;
createAnimation(){
if(x!=oldX || y!=oldY){
xAnimation = Tween(begin: oldX, end: x).animate(animationController);
yAnimation = Tween(begin: oldY, end: y).animate(animationController);
}
} update(){
if(x!=oldX || y!=oldY){
addressX = xAnimation.value;
addressY = yAnimation.value;
print('updating: x: $x, y: $y');
}else{
print('no need to updated x:$x, y:$y, oldX:$oldX, oldY:$oldY');
}
}
BrickModel({this.x, this.y, this.oldX, this.oldY, this.addressX, this.addressY, this.animationController});
} class DataBloc {
ReplaySubject dataBloc = ReplaySubject();
}

  

练习bloc , 动画的更多相关文章

  1. Flutter 状态管理之BLoC

    在正式介绍 BLoC之前, 为什么我们需要状态管理.如果你已经对此十分清楚,那么建议直接跳过这一节.如果我们的应用足够简单,Flutter 作为一个声明式框架,你或许只需要将 数据 映射成 视图 就可 ...

  2. 动画requestAnimationFrame

    前言 在研究canvas的2D pixi.js库的时候,其动画的刷新都用requestAnimationFrame替代了setTimeout 或 setInterval 但是jQuery中还是采用了s ...

  3. 梅须逊雪三分白,雪却输梅一段香——CSS动画与JavaScript动画

    CSS动画并不是绝对比JavaScript动画性能更优越,开源动画库Velocity.js等就展现了强劲的性能. 一.两者的主要区别 先开门见山的说说两者之间的区别. 1)CSS动画: 基于CSS的动 ...

  4. CSS 3学习——animation动画

    以下内容根据官方文档翻译以及自己的理解整理. 1.  介绍 本方案介绍动画(animations).通过动画,开发者可以将CSS属性值的变化指定为一个随时间变化的关键帧(keyframes)的集合.在 ...

  5. javascript动画系列第三篇——碰撞检测

    前面的话 前面分别介绍了拖拽模拟和磁性吸附,当可视区域内存在多个可拖拽元素,就出现碰撞检测的问题,这也是javascript动画的一个经典问题.本篇将详细介绍碰撞检测 原理介绍 碰撞检测的方法有很多, ...

  6. 虾扯蛋:Android View动画 Animation不完全解析

    本文结合一些周知的概念和源码片段,对View动画的工作原理进行挖掘和分析.以下不是对源码一丝不苟的分析过程,只是以搞清楚Animation的执行过程.如何被周期性调用为目标粗略分析下相关方法的执行细节 ...

  7. Visaul Studio 常用快捷键的动画演示

    从本篇文章开始,我将会陆续介绍提高 VS 开发效率的文章,欢迎大家补充~ 在进行代码开发的时候,我们往往会频繁的使用键盘.鼠标进行协作,但是切换使用两种工具会影响到我们的开发速度,如果所有的操作都可以 ...

  8. transtion:过渡动画

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 17.0px Monaco; color: #4f5d66 } p.p2 { margin: 0.0px 0 ...

  9. 再谈CAAnimation动画

    CAAnimaton动画分为CABasicAnimation & CAKeyframeAnimation CABasicAnimation动画, 顾名思义就是最基本的动画, 老规矩先上代码: ...

随机推荐

  1. Unity资源商店 Asset store下载文件夹的位置

    Win10 C:\Users\用户名\AppData\Roaming\Unity\Asset Store-5.x\ Mac OS X ~/Library/Unity/Asset Store

  2. android studio 创建项目的一些配置

    build.gradle文件 apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' // 使用 ...

  3. 011 webpack中使用vue

    一:在webpack中使用vue 1.安装vue的包 2.index.html <!DOCTYPE html> <html lang="en"> <h ...

  4. git 常用命令的总结

    1. git 查看分支 git branch (星号代表当前的分支) 2. 创建一个本地分支 git checkout -b 分支名称 3. 将本地新建分支提交到远程 git push origin ...

  5. 【GMT43智能液晶模块】例程十八:LAN_HTTP实验——网页服务器

    源代码下载链接: 链接:https://pan.baidu.com/s/1sr4a7TBPyvs18jTfCfVj8Q 提取码:jwfv 复制这段内容后打开百度网盘手机App,操作更方便哦 GMT43 ...

  6. nginx安装和命令

    1. nginx安装 1.1 mac上安装 brew search nginx brew install nginx 1.2 windows上安装 下载nginx.zip,解压到D盘,发送快捷方式到桌 ...

  7. jw python 培训帮助 手册

    #########sample 1  (如果python 遇到有问题,就求助于 help 命令,python 是 个人开发的胶水语言,因此不具备 java,c++的类的继承关系) Python hel ...

  8. PMP 第7章错题总结

    这一章全是公式,是计算量最大的一章 1.进度安排的灵活性是由总时差决定的2.三点估算通过考虑估算中的不确定性和风险,可以提高活动持续时间估算的准确性3.快速跟进是一种进度压缩技术,将正常情况下按顺序进 ...

  9. Linux 提示更新密码

    You are required to change your password immediately (password aged)Last login: Thu Aug 22 17:04:01 ...

  10. djang-celery使用带密码的redis

    前言: 网上很多django-celery使用redis(使用不带密码的redis)的用法都是千篇一律,那带密码的redis该怎么使用了呢,没有看到一篇有帮助的,在官网搜了下,发现以下用法,请看下面 ...