ksnotification 0.0.4 copy "ksnotification: ^0.0.4" to clipboard
ksnotification: ^0.0.4 copied to clipboard

outdated

简单好用的通知广播管理.

ksnotification #

简单5步实现通知的管理,借鉴iOS NotificationCenter管理模式。

/*1、接口实现*/
class _MyHomePageState extends State<MyHomePage> implements KSObserver {
  int _counter = 0;
  static const String increment_counter = 'increment_counter';

  @override
  void initState() {
    super.initState();
    /*2、添加监听*/
    KSNotificationCenter.shard().addObserver(this, increment_counter);
  }

  @override
  void dispose() {
    /*5、移除监听*/
    KSNotificationCenter.shard().removeObserver(this, increment_counter);
    super.dispose();
  }

  /*4、接收监听消息*/
  @override
  receiveNotify(Map message, String name) {
    if (name == increment_counter){
      int value = message['num'] as int;
      setState(() {
        _counter += value;
      });
    }
  }

  void _incrementCounter() {
    /*3、发送通知广播*/
    KSNotificationCenter.shard().post({'num':100}, increment_counter);
  }

  @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.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
1
likes
0
pub points
0%
popularity

Publisher

unverified uploader

简单好用的通知广播管理.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on ksnotification