首先要引用event_bus.dart文件
初始化EventBus全局对象
生成一个event类,也可以用父类event来传递
在需要监听总线的页面添加监视器,类型是StreamSubscription,可以指定监听的event类型,不符合的event不会被监听到
发送事件可以用EventBus.fire(event)方法,其中的event可以是普通event,也可以是自定event
一个例子:
import 'dart:async'; import 'package:flutter/cupertino.dart'; import "package:flutter/material.dart"; import 'package:flutter/rendering.dart'; import "package:flutter/src/material/dialog.dart"; import 'package:flutter/gestures.dart'; import 'package:event_bus/event_bus.dart'; import 'package:flutter/services.dart'; void main() => runApp(MyApp()); EventBus _bus = EventBus(); int i =0; class changeThemeColorCEvent { Color color; changeThemeColorCEvent(this.color); } class MyApp extends StatelessWidget{ @override Widget build(BuildContext context) { //_bus.on<changeThemeColorCEvent>().listen((event){print(event); print(i++);}); return MaterialApp( title:'fuck', home: TestPage(), ); } } class TestPage extends StatefulWidget { TestPage ({Key key}): super( key :key); @override State<StatefulWidget> createState() { // TODO: implement createState return TestPageState(); } } class TestPageState extends State<TestPage> { StreamSubscription sub; Future<String> wait5Second() async { return Future.delayed(Duration(seconds: 5),() => "我是数据"); } @override void initState() { // TODO: implement initState super.initState(); sub= _bus.on<changeThemeColorCEvent>().listen((event){ print(event); }); } @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar(title: Text('futureBuilder测试'),), body: Center( child: FlatButton( child: Text('点我'), onPressed: () => Navigator.push(context, CupertinoPageRoute(builder: (context) => anotherPage())), ) )); } @override void dispose() { // TODO: implement dispose super.dispose(); sub.cancel(); } } class anotherPage extends StatefulWidget { @override State<StatefulWidget> createState() { // TODO: implement createState return anotherPageState(); } } class anotherPageState extends State<anotherPage> { @override Widget build(BuildContext context) { // TODO: implement build return Container(color: Colors.white, child: RaisedButton( child: Text('点我'), onPressed: () { _bus.fire(changeThemeColorCEvent(Colors.red)); }, ), ); } }
不生成监听器(streamsubscription)也可以监听,但每次调用bus.on<xx>.listen()都会生成一个新的streamsubscription,所以将上面代码改成下面这样的,热重载一下,会发现每次fire event,会print两次。因为热重载(ressemble)是调用didUpdateWidget和build,build里会重新生成一个streamsubscription。
import 'dart:async'; import 'package:flutter/cupertino.dart'; import "package:flutter/material.dart"; import 'package:flutter/rendering.dart'; import "package:flutter/src/material/dialog.dart"; import 'package:flutter/gestures.dart'; import 'package:event_bus/event_bus.dart';void main() => runApp(MyApp()); EventBus _bus = EventBus(); int i =0; class changeThemeColorCEvent { Color color; changeThemeColorCEvent(this.color); } class MyApp extends StatelessWidget{ @override Widget build(BuildContext context) { _bus.on<changeThemeColorCEvent>().listen((event){print(event); print(i++);}); return MaterialApp( title:'fuck', home: TestPage(), ); } } class TestPage extends StatefulWidget { TestPage ({Key key}): super( key :key); @override State<StatefulWidget> createState() { // TODO: implement createState return TestPageState(); } } class TestPageState extends State<TestPage> { // StreamSubscription sub; Future<String> wait5Second() async { return Future.delayed(Duration(seconds: 5),() => "我是数据"); } @override void initState() { // TODO: implement initState super.initState(); // sub= _bus.on<changeThemeColorCEvent>().listen((event){ // print(event); // }); } @override void didUpdateWidget(TestPage oldWidget) { // TODO: implement didUpdateWidget print('didUpdateWidget'); super.didUpdateWidget(oldWidget); } @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar(title: Text('futureBuilder测试'),), body: Center( child: FlatButton( child: Text('点我'), onPressed: () => Navigator.push(context, CupertinoPageRoute(builder: (context) => anotherPage())), ) )); } @override void dispose() { // TODO: implement dispose super.dispose(); // sub.cancel(); } } class anotherPage extends StatefulWidget { @override State<StatefulWidget> createState() { // TODO: implement createState return anotherPageState(); } } class anotherPageState extends State<anotherPage> { @override void didUpdateWidget(anotherPage oldWidget) { // TODO: implement didUpdateWidget print('didupdate'); super.didUpdateWidget(oldWidget); } @override Widget build(BuildContext context) { // TODO: implement build return Container(color: Colors.white, child: RaisedButton( child: Text('点我'), onPressed: () { _bus.fire(changeThemeColorCEvent(Colors.red)); }, ), ); } @override void dispose() { // TODO: implement dispose super.dispose(); } }