https://www.jianshu.com/p/6c05e2e8c864

 

引入库
dependencies:
event_bus: ^1.1.1

工具类

 

import 'dart:async';


import 'package:event_bus/event_bus.dart';

class EventBusUtil{

   static EventBus _eventBus;

   //获取单例
   static EventBus getInstance(){
     if(_eventBus==null){
       _eventBus=EventBus();
     }
     return _eventBus;
   }

   //返回某事件的订阅者
   static StreamSubscription<T> listen<T extends Event>(Function(T event) onData) {
     if(_eventBus==null){
       _eventBus=EventBus();
     }
     //内部流属于广播模式,可以有多个订阅者
     return _eventBus.on<T>().listen(onData);
   }

   //发送事件
   static void fire<T extends Event>(T e) {
     if(_eventBus==null){
       _eventBus=EventBus();
     }
     _eventBus.fire(e);
   }
}

abstract class Event {}

class LoginEvent extends Event{
   static int eventIn=1;
   static int eventOut=0;
   final int status;
   LoginEvent(this.status);
}

class OrderStatusEvent extends Event{

}

如何使用

 

import 'dart:async';


import 'package:LinDemo/event_bus_util.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'EventBus研究',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  StreamSubscription<LoginEvent> _loginSubscription;
  StreamSubscription<OrderStatusEvent> _orderStateSubscription;

  String loginStatus="未登录";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _loginSubscription=EventBusUtil.listen((event){
      loginStatus=loginStatus+"登录了,";
      setState(() {

      });
    });

    _orderStateSubscription=EventBusUtil.listen((event){
      loginStatus=loginStatus+"出现了订单状态,";
      setState(() {

      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _loginSubscription?.cancel();
    _orderStateSubscription?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("我是接受页面"),
      ),
      body: Center(
        child: Column(
          children: [
            Text(loginStatus,style: TextStyle(
              fontSize: 20,
            ),),
            GestureDetector(
              onTap: (){
                Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context){
                  return SecondPage();
                }));
              },
              child: Container(
                child: Text("去发送页面"),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("我是发送页面"),
      ),
      body: Center(
        child: GestureDetector(
          onTap: (){
            //模拟发送登录事件
            EventBusUtil.fire(LoginEvent(LoginEvent.eventIn));
            Navigator.of(context).pop();
          },
          child: Text("发送事件",style: TextStyle(
            fontSize: 20,
          ),),
        ),
      ),
    );
  }
}


作者:TankBaby
链接:https://www.jianshu.com/p/6c05e2e8c864
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。