Basic use of Flutter event_bus (you can also write when you read it with your feet)

Basic use of Flutter event_bus (you can also write when you read it with your feet)

Original: @As.Kai
Blog address: https://blog.csdn.net/qq_42362997
If the following content is helpful to you, please like it~

First, the most basic necessary conditions import dependencies:

event_bus: ^1.1.1 The
latest pub version click me to check
This article realizes whether subscribers are logged in to notify all publishers.
Implementation scenario: monitor the user's login status

Step into the main topic:

Before using:

event_bus can communicate through the event bus, divided into publishers/subscribers.
Publishers can trigger events and subscribers can listen to events.
This allows objects to communicate and interact with each other without explicitly defining listeners to track them.

All in all, you can understand them as
SharedPreferences that do not require asynchronous processing, and providers
that do not need to be registered in the main function. This is a personal understanding. Everyone understands differently. If there are misleading errors, you can send me a message.

Next, let's talk about how to use the most basic event_bus:

We enter the project Demo:
first we need to create an event_bus tool class for the current project:
event_bus_util.dart:

import 'package:event_bus/event_bus.dart';

EventBus eventBus = new EventBus();
//用户是否注册
class UserIsLogin{
  final bool isLogin;
  UserLoggedInEvent({this.isLogin});
}

On the first page, we put a Text to display the current login status, and
then put a button to jump to a new page. In the
initialization method of two.dart, let's subscribe to whether the user we just wrote in event_bus is logged in

String test = '当前登录状态:';

@override
void initState() {
  // TODO: implement initState
  super.initState();
  eventBus.on<UserLoggedInEvent>().listen((event) {
    if(event.isLogin){
      print('true');
      test = '用户已登录';
    }else{
      print('false');
      test = '用户未登录';
    }
    setState((){});
  });
}
@override
Widget build(BuildContext context) {
  return new Scaffold(
    appBar: new AppBar(
      title: new Text('aaa'),
    ),
//event_bus Demo
body: new Column(
  children: [
    new Text(test),
    new GestureDetector(
      onTap: () {
        Navigator.push(context, new MaterialPageRoute(builder: (context){
          return Two();
        }));
      },
      child: new Text('第二个页面'),
    )
  ],
),
);

two.dart: On the
second page, we mainly do the trigger event of the publisher.
Put two buttons, one for the user to log in and the other for the user not to log in

@override
Widget build(BuildContext context) {
  return new Scaffold(
    appBar: new AppBar(
      title: new Text('two'),
    ),
    body: new Column(
      children: [
        new GestureDetector(
          onTap: (){
            print('onTap()');
            eventBus.fire(UserLoggedInEvent(isLogin: false));
          },
          child: new Text('用户未登录'),
        ),
        new GestureDetector(
          onTap: (){
            print('onTap()');
            eventBus.fire(UserLoggedInEvent(isLogin: true));
          },
          child: new Text('用户登录'),
        ),
      ],
    )
  );
}

eventBus.fire(UserLoggedInEvent(isLogin: false));
eventBus.fire(UserLoggedInEvent(isLogin: true));

Click the publisher to trigger the event

Run flutter run to check the effect!

Finally, give a thumbs up, sirs!

Follow me and grow together!
@As.Kai

Guess you like

Origin blog.csdn.net/qq_42362997/article/details/114023763