flutter_bloc 是一个bloc第三方库,这个库很方便的让你集成bloc模式,这个库结合了RXDart,先了解一下bloc 的模式吧

Flutter之BLOC_数据

1,widget 触发event 事件 

2,bloc 接收event 事件并作出逻辑处理

3 ,并把逻辑处理结果给返回出来 

4,UI展示数据

其实它有点像mvvm   ,Event只是出发事件,并不能传值,bloc 接收这个event,根据event去找到具体的方法去处理逻辑,之后把结果返回,如果再不明白,我举个例子,我去饭店吃饭去告诉老板点一个大盘鸡(这个是event),老板根据菜名找到具体的厨师(sink),厨师做好大盘鸡(这是逻辑处理)之后告诉老板做好(state)老板把菜端上来(UI跟数据改变)

flutter_bloc 提供几个api  根据这几个API 就可以快速搭建bloc

   BlocBuilder    

   BlocProvider   

   BlocProviderTree  

   BlocListener  

   BlocListenerTree 

BlocBuilder

有三个属性 bloc  condition  builder

BlocBuilder(
bloc: ,这个添加bloc dart
condition: (previousState, currentState){ return true;},//可选默认返回true
builder: (BuildContext context, List state) {}state 返回数据
)。

BlocProvider

这个可以管理全局变量

    BlocProvider(

    bloc:,这个添加bloc dart 把这个bloc 传递其它字界面使用

    child:LogIn(),子类

    )

子widget 通过BlocProvider.of<LogBloc>(context) 获取这个bloc

 如果涉及到push 可以通过这种模式传递

    Navigator.push(context, new MaterialPageRoute(

    builder: (Context)=>BlocProvider(

    bloc:LogBloc(),

    child:HomePage1(),

    )));

BlocProviderTree

可以管理多个状态

一个widget 涉及多个state 可以用它管理

BlocProviderTree(
  blocProviders: [
    BlocProvider<BlocA>(bloc: BlocA()),
    BlocProvider<BlocB>(bloc: BlocB()),
    BlocProvider<BlocC>(bloc: BlocC()),
  ],
  child: ChildA(),
)

demo

import 'package:flutter/material.dart';
import 'package:flutter_app/bloc/counter_bloc_demo.dart';


class BlocDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CounterProvider(
      bloc: CounterBloc(),
      child: Scaffold(
        appBar: AppBar(
          title: Text('BlocDemo'),
          elevation: 0.0,
        ),
        body: CounterHome(),
        floatingActionButton: CounterActionButton(),
      ),
    );
  }
}
import 'dart:async';

import 'package:flutter/material.dart';

class CounterHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    CounterBloc _counterBloc = CounterProvider.of(context).bloc;
    
    return Center(
      child: StreamBuilder(
        initialData: 0,
        stream: _counterBloc.count,
        builder: (context, snapshot) {
          return ActionChip(
            label: Text('${snapshot.data}'),
            onPressed: () {
              // _counterBloc.log();
              _counterBloc.counter.add(1);
            },
          );
        },
      ),
    );
  }
}

class CounterActionButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    CounterBloc _counterBloc = CounterProvider.of(context).bloc;
    
    return FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: () {
        // _counterBloc.log();
        _counterBloc.counter.add(1);
      },
    );
  }
}

class CounterProvider extends InheritedWidget {
  final Widget child;
  final CounterBloc bloc; 

  CounterProvider({
    this.child,
    this.bloc,
  }) : super(child: child);

  static CounterProvider of(BuildContext context) =>
      context.inheritFromWidgetOfExactType(CounterProvider);

  @override
  bool updateShouldNotify(CounterProvider oldWidget) {
    return true;
  }
}

class CounterBloc {
  int _count = 0;
  
  final _counterActionController = StreamController<int>();
  StreamSink<int> get counter => _counterActionController.sink;

  final _counterController = StreamController<int>();
  Stream<int> get count => _counterController.stream;

  CounterBloc() {
    _counterActionController.stream.listen(onData);
  }

  void onData(int data) {
    print('$data');
    _count = data + _count;
    _counterController.add(_count);
  }

  void disponse() {
    _counterActionController.close();
    _counterController.close();
  }
  
  void log() {
    print('BLoC'); 
  }
}

Flutter之BLOC_ico_02