美文网首页Flutter
flutter eventbus 传值

flutter eventbus 传值

作者: 冰点雨 | 来源:发表于2020-01-22 09:10 被阅读0次

1.首先引入插件

event_bus: ^1.1.0

2.然后创建个event_util.dart

import 'package:event_bus/event_bus.dart';
export 'dart:async';
class EventBusUtil {
  static EventBus _eventBus;

  static EventBus getInstance() {
    if (_eventBus == null) {
      _eventBus = new EventBus();
    }
    return _eventBus;
  }
}

class PageEvent {
  String test;
  PageEvent(this.test);
}

3.使用

第二个页面发送通知

  //发送订阅消息传值
       EventBusUtil.getInstance().fire(PageEvent('login'));

第一个页面添加监听

  @override
  void initState() {
    super.initState();
    //监听登录事件
    _event = EventBusUtil.getInstance().on<PageEvent>().listen((data) {
      if (data.test == 'login') {
        setState(() {
          _lodinState = '通知我登录成功了';
        });
      }else{
        setState(() {
          _logoutState = '通知我退出登录成功了';
        });
      }

    });

  }

移除监听

  @override
  void dispose() {
    super.dispose();

    //移除监听机制
  _event.cancel();

  }

第一个页面也可以这样接收值

sss = EventBusUtil.getInstance().on<PageEvent>().listen((data) {
                print('${data.test}');
                sss.cancel();
              });

4.创建第一个页面

import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_eventbus/login_logout.dart';
import 'package:flutter_eventbus/utils.dart';

import 'event_util.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '广播demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  String _lodinState;
  String _logoutState;
  StreamSubscription<PageEvent> _event;



  @override
  void initState() {
    super.initState();
    //监听登录事件
    _event = EventBusUtil.getInstance().on<PageEvent>().listen((data) {
      if (data.test == 'login') {
        setState(() {
          _lodinState = '通知我登录成功了';
        });
      }else{
        setState(() {
          _logoutState = '通知我退出登录成功了';
        });
      }

    });

  }

  @override
  void dispose() {
    super.dispose();

    //移除监听机制
  _event.cancel();

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('广播demo'),
        ),
        body: Container(
          child: ListView(
            children: <Widget>[
              Container(
                  alignment: Alignment.topCenter,
                  child: FlatButton(
                      onPressed: () {

                        Utils.turn(context, BroadcastPage());

                      },
                      child: Text('跳转'))
              ),
              Container(
                margin: EdgeInsets.only(top: 20),
                alignment: Alignment.topCenter,
                child: Text(
                  _lodinState != null ? _lodinState : '',
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 20),
                alignment: Alignment.topCenter,
                child: Text(
                  _logoutState != null ? _logoutState : '',
                ),
              ),

            ],
          ),
        )
    );
  }
}

5.创建第二个页面

import 'package:flutter/material.dart';
import 'package:flutter_eventbus/event_util.dart';
import 'package:flutter_eventbus/utils.dart';


class BroadcastPage extends StatefulWidget {
  @override
  BroadcastPageState createState() => new BroadcastPageState();
}

class BroadcastPageState extends State<BroadcastPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('登录 注销'),
      ),
      body: Container(
        child: ListView(
          children: <Widget>[
            Container(
                alignment: Alignment.topCenter,
                child: FlatButton(
                    onPressed: () {

                      //发送订阅消息去修改颜色
                      EventBusUtil.getInstance().fire(PageEvent('login'));
                      Utils.pop(context);

                    },
                    child: Text('登录'))
            ),
            Container(
                alignment: Alignment.topCenter,
                child: FlatButton(
                    onPressed: () {

                      EventBusUtil.getInstance().fire(PageEvent('logout'));
                      Utils.pop(context);

                    },
                    child: Text('退出登录'))
            ),
          ],
        ),
      ),
    );
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  void didUpdateWidget(BroadcastPage oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }
}

相关文章

网友评论

    本文标题:flutter eventbus 传值

    本文链接:https://www.haomeiwen.com/subject/qcyuzctx.html