练习bloc , 动画
有点意思,
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 , 动画的更多相关文章
- Flutter 状态管理之BLoC
在正式介绍 BLoC之前, 为什么我们需要状态管理.如果你已经对此十分清楚,那么建议直接跳过这一节.如果我们的应用足够简单,Flutter 作为一个声明式框架,你或许只需要将 数据 映射成 视图 就可 ...
- 动画requestAnimationFrame
前言 在研究canvas的2D pixi.js库的时候,其动画的刷新都用requestAnimationFrame替代了setTimeout 或 setInterval 但是jQuery中还是采用了s ...
- 梅须逊雪三分白,雪却输梅一段香——CSS动画与JavaScript动画
CSS动画并不是绝对比JavaScript动画性能更优越,开源动画库Velocity.js等就展现了强劲的性能. 一.两者的主要区别 先开门见山的说说两者之间的区别. 1)CSS动画: 基于CSS的动 ...
- CSS 3学习——animation动画
以下内容根据官方文档翻译以及自己的理解整理. 1. 介绍 本方案介绍动画(animations).通过动画,开发者可以将CSS属性值的变化指定为一个随时间变化的关键帧(keyframes)的集合.在 ...
- javascript动画系列第三篇——碰撞检测
前面的话 前面分别介绍了拖拽模拟和磁性吸附,当可视区域内存在多个可拖拽元素,就出现碰撞检测的问题,这也是javascript动画的一个经典问题.本篇将详细介绍碰撞检测 原理介绍 碰撞检测的方法有很多, ...
- 虾扯蛋:Android View动画 Animation不完全解析
本文结合一些周知的概念和源码片段,对View动画的工作原理进行挖掘和分析.以下不是对源码一丝不苟的分析过程,只是以搞清楚Animation的执行过程.如何被周期性调用为目标粗略分析下相关方法的执行细节 ...
- Visaul Studio 常用快捷键的动画演示
从本篇文章开始,我将会陆续介绍提高 VS 开发效率的文章,欢迎大家补充~ 在进行代码开发的时候,我们往往会频繁的使用键盘.鼠标进行协作,但是切换使用两种工具会影响到我们的开发速度,如果所有的操作都可以 ...
- transtion:过渡动画
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 17.0px Monaco; color: #4f5d66 } p.p2 { margin: 0.0px 0 ...
- 再谈CAAnimation动画
CAAnimaton动画分为CABasicAnimation & CAKeyframeAnimation CABasicAnimation动画, 顾名思义就是最基本的动画, 老规矩先上代码: ...
随机推荐
- [Web] HTML5新特性history pushState/replaceState解决浏览器刷新缓存
转载: https://www.jianshu.com/p/cf63a1fabc86 现实开发中,例如‘商品列表页’跳转‘商品详情页’,返回时,不重新加载刷新页面,并且滚动到原来的位置. 1.首先,先 ...
- Shell命令行提示定制
/******************************************************************************* * Shell命令行提示定制 * 说明 ...
- win10教育版永久激活密钥 win10教育版激活码 win10教育版产品密钥2018(第三种方法亲测可用!)
有没有最新win10教育版激活密钥?win10教育版功能比较全面,增加了一些学术上需要的功能.有用户装了win10教育版,提示需要产品密钥,网上找到的win10教育版永久激活码大多失效,这边小编为大家 ...
- Tensorflows安装(cpu版安装方法)
一.说明 首先声明,本人系统是Windows10 64位,Win7未试. 本文旨在帮助园友以更简单的方式安装Tensorflow,下面介绍的是如何安装Python的Tensorflow cpu版本. ...
- ubuntu连接多个realsense d435
ubuntu连接多个realsense d435 import pyrealsense2 as rs import numpy as np import cv2 import time import ...
- Springboot配置连接两个数据库
背景: 项目中需要从两个不同的数据库查询数据,之前实现方法是:springboot配置连接一个数据源,另一个使用jdbc代码连接. 为了改进,现在使用SpringBoot配置连接两个数据源 实现效果: ...
- matlab学习笔记13_2匿名函数
一起来学matlab-matlab学习笔记13函数 13_2 匿名函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 https://ww2.mathworks.cn/help/m ...
- [Metricbeat] Metricbeat监控golang服务器
0x0 前言 最近这几天研究了一下ElasticSearch相关的技术栈.前面一篇转发了别人些的非常详细的ElasticSearch和Kibana搭建的过程.发现Elastic家族还有Metricbe ...
- istio1.0安装
1. istio1.0安装 创建 istio 目录 [root@centos-110 ~]# mkdir istio [root@centos-110 ~]# cd istio 1.1 获取安装包 链 ...
- SpringBoot读取Linux服务器某路径下文件\读取项目Resource下文件
// SpringBoot读取Linux服务器某路径下文件 public String messageToRouted() { File file = null; try { file = Resou ...