flutter_broadcast_receiver 1.0.0+2 copy "flutter_broadcast_receiver: ^1.0.0+2" to clipboard
flutter_broadcast_receiver: ^1.0.0+2 copied to clipboard

Enables you to broadcast global events in your app. Helpful in cases where your modules/components need to know about a change happened somewhere in your app.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_broadcast_receiver/flutter_broadcast_receiver.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: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    //Subscribe broadcast
    BroadcastReceiver().subscribe<String>("BROADCAST_RECEIVER_DEMO",
        (String message) {
      print("BroadcastReceiver() data => $message");
      final snackBar = SnackBar(
          content:
              Text('Yay! A Broadcast is received\n_counter value is $message'));
      ScaffoldMessenger.of(context).showSnackBar(snackBar);
    });
  }

  @override
  void dispose() {
    //Dispose broadcast
    BroadcastReceiver().unsubscribe("BROADCAST_RECEIVER_DEMO");
    super.dispose();
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
    //Publish broadcast
    BroadcastReceiver().publish<String>("BROADCAST_RECEIVER_DEMO",
        arguments: _counter.toString());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title!),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
11
likes
140
pub points
84%
popularity

Publisher

verified publisherdwiky.my.id

Enables you to broadcast global events in your app. Helpful in cases where your modules/components need to know about a change happened somewhere in your app.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

More

Packages that depend on flutter_broadcast_receiver