zoukankan      html  css  js  c++  java
  • 2个监听器+ dialog + replysubject + extends

    import 'package:flutter/material.dart';
    import 'package:rxdart/rxdart.dart';
    import 'package:flutter/widgets.dart';
    //import 'package:flutter_mvvm/core/viewmodel_provider.dart';
    //import 'package:flutter_mvvm/page/home/viewmodel_home.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter MVVM Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: HomePage(),
        );
      }
    }
    
    class HomePage extends StatefulWidget{
      @override
      State<StatefulWidget> createState() {
        // TODO: implement createState
        return HomePageState();
      }
    }
    
    class HomePageState extends State<HomePage> {
      ExtremeBloc extremeBloc = ExtremeBloc();
    
    //  _showDialog(){
    //
    //    showDialog(context: context,
    //    builder: (_){
    //      return SimpleDialog(
    //        title: Text('dialog'),
    //        children: <Widget>[
    //          StreamBuilder(
    //            stream: dataBloc.dataBloc.stream,
    //            builder: (context, snapshot){
    //              if(snapshot.hasData){
    //                var data = snapshot.data;
    //                return Container(
    //                  child: Text('$data'),
    //                );
    //
    //              }else{
    //                return Text('pending..');
    //              }
    //            },
    //          ),
    //        ],
    //      );
    //    });
    //  }
    
      @override
      Widget build(BuildContext context) {
        extremeBloc.get1String();
        extremeBloc.get2String();
        return Scaffold(
          appBar: AppBar(title: Text('test'),),
          body: Container(
            child: Column(
              children: <Widget>[
                StreamBuilder(
                  stream: extremeBloc.data1Bloc.stream,
                  builder: (context, snapshot){
                    if(snapshot.hasData){
                      return Text('from 1: ${snapshot.data}');
                    }else{
                      return Text('no data from 1');
                    }
                  },
                ),
    
                StreamBuilder(
                  stream: extremeBloc.data2Bloc.stream,
                  builder: (context, snapshot){
                    if(snapshot.hasData){
                      return Text('from 2: ${snapshot.data}');
                    }else{
                      return Text('no data from 2');
                    }
                  },
                ),
    
    
    
              ],
            ),
          ),
        );
      }
    }
    
    
    class DataBloc {
      ReplaySubject data1Bloc = ReplaySubject();
    
      get1String()async{
        for(int i=0; i<10; i++){
          data1Bloc.add(i);
          await Future.delayed(Duration(seconds: 1));
        }
      }
    }
    
    
    
    class ExtremeBloc extends DataBloc {
      ReplaySubject data2Bloc = ReplaySubject();
    
    
      get2String()async{
        for(int i=0; i<10; i++){
          data2Bloc.add(i*100);
          await Future.delayed(Duration(seconds: 1));
        }
      }
    }
    

      

  • 相关阅读:
    第七组(69)团队展示
    结对编程作业
    同步异步和阻塞非阻塞
    TCP和UDP和IP和HTTP和socket
    http协议
    数据库基础知识
    准确的笑话
    Java实现多线程的方式
    HashMap
    HTTPS与HTTP
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10792324.html
Copyright © 2011-2022 走看看