zoukankan      html  css  js  c++  java
  • 练习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(
               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(
                          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();
    }
    

      

  • 相关阅读:
    Android兼容性测试CTS
    Tkinter
    初探socket
    性能监控2
    HTTP
    python实现接口自动化1
    pip安装超时问题
    一行 Python
    Python 面向对象进阶
    Python 面向对象编程基础
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10880658.html
Copyright © 2011-2022 走看看