ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

练习bloc , 动画

2019-05-17 12:47:52  阅读:229  来源: 互联网

标签:addressX 动画 child oldY oldX 练习 brickModel bloc animationController


有点意思,

 

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();
}

  

 

标签:addressX,动画,child,oldY,oldX,练习,brickModel,bloc,animationController
来源: https://www.cnblogs.com/pythonClub/p/10880658.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有