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

  

posted @ 2019-04-29 19:08  CrossPython  阅读(168)  评论(0编辑  收藏  举报