├── LICENSE ├── README.md ├── lib ├── call │ ├── call.dart │ └── notify.dart └── main.dart └── pubspec.yaml /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 亢少军 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #dart-call 2 | Flutter Dart消息派发及监听使用的库 简单实用 可替代EventBus 3 | -------------------------------------------------------------------------------- /lib/call/call.dart: -------------------------------------------------------------------------------- 1 | //事件派发及监听使用 2 | class Call { 3 | 4 | static Map> _callMap = Map>(); 5 | 6 | static Future addCallBack(String type, Function callback) async { 7 | if(_callMap[type] == null ) { 8 | _callMap[type] = []; 9 | } 10 | if(await hasCallBack(type, callback) == false) { 11 | _callMap[type].add(callback); 12 | } 13 | } 14 | 15 | static Future hasCallBack(type,Function callBack) async { 16 | if(_callMap[type] == null ) { 17 | return false; 18 | } 19 | return _callMap[type].contains(callBack); 20 | } 21 | 22 | static Future removeCallBack(type,Function callBack) async { 23 | if(_callMap[type] == null ) { 24 | return; 25 | } 26 | _callMap[type].removeWhere((element) => element == callBack); 27 | } 28 | 29 | static Future dispatch(String type, {dynamic data = null }) async { 30 | if(_callMap[type] == null) { 31 | throw Exception('回调事件 $type 没有监听,发送失败'); 32 | } 33 | _callMap[type].forEach((element) { 34 | element(data); 35 | }); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /lib/call/notify.dart: -------------------------------------------------------------------------------- 1 | //消息类型 2 | class Notify { 3 | 4 | //测试消息类型 5 | static const TEST_NOTIFY = 'test_notify'; 6 | 7 | 8 | 9 | } -------------------------------------------------------------------------------- /lib/main.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import './call/call.dart'; 3 | import './call/notify.dart'; 4 | 5 | void main() { 6 | runApp(MyApp()); 7 | } 8 | 9 | class MyApp extends StatelessWidget { 10 | 11 | @override 12 | Widget build(BuildContext context) { 13 | return MaterialApp( 14 | title: 'Call示例', 15 | theme: ThemeData( 16 | 17 | primarySwatch: Colors.blue, 18 | ), 19 | home: MyHomePage(title: 'Call示例'), 20 | ); 21 | } 22 | } 23 | 24 | class MyHomePage extends StatefulWidget { 25 | MyHomePage({Key key, this.title}) : super(key: key); 26 | 27 | 28 | final String title; 29 | 30 | @override 31 | _MyHomePageState createState() => _MyHomePageState(); 32 | } 33 | 34 | class _MyHomePageState extends State { 35 | int _counter = 0; 36 | 37 | void _incrementCounter() { 38 | 39 | var data = { 40 | 'userId':111, 41 | 'userName':'玄微子' 42 | }; 43 | 44 | Call.dispatch(Notify.TEST_NOTIFY,data:data); 45 | } 46 | 47 | @override 48 | Widget build(BuildContext context) { 49 | 50 | return Scaffold( 51 | appBar: AppBar( 52 | title: Text(widget.title), 53 | ), 54 | body: Center( 55 | child: TestWidget(), 56 | ), 57 | floatingActionButton: FloatingActionButton( 58 | onPressed: _incrementCounter, 59 | tooltip: '点击派发消息', 60 | child: Icon(Icons.add), 61 | ), 62 | ); 63 | } 64 | } 65 | 66 | 67 | class TestWidget extends StatefulWidget{ 68 | 69 | TestWidget(); 70 | 71 | @override 72 | TestWidgetState createState() => TestWidgetState(); 73 | 74 | } 75 | 76 | class TestWidgetState extends State{ 77 | 78 | String userName = ''; 79 | 80 | @override 81 | void initState() { 82 | super.initState(); 83 | 84 | Call.addCallBack(Notify.TEST_NOTIFY,this._callBack); 85 | 86 | } 87 | 88 | _callBack(data) { 89 | this.setState(() { 90 | userName = data['userName']; 91 | }); 92 | } 93 | 94 | @override 95 | Widget build(BuildContext context) { 96 | return Container( 97 | child: Text("监听到的数据为:" + userName), 98 | ); 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: dart_call 2 | description: A new Flutter application. 3 | 4 | # The following line prevents the package from being accidentally published to 5 | # pub.dev using `pub publish`. This is preferred for private packages. 6 | publish_to: 'none' # Remove this line if you wish to publish to pub.dev 7 | 8 | # The following defines the version and build number for your application. 9 | # A version number is three numbers separated by dots, like 1.2.43 10 | # followed by an optional build number separated by a +. 11 | # Both the version and the builder number may be overridden in flutter 12 | # build by specifying --build-name and --build-number, respectively. 13 | # In Android, build-name is used as versionName while build-number used as versionCode. 14 | # Read more about Android versioning at https://developer.android.com/studio/publish/versioning 15 | # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. 16 | # Read more about iOS versioning at 17 | # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html 18 | version: 1.0.0+1 19 | 20 | environment: 21 | sdk: ">=2.7.0 <3.0.0" 22 | 23 | dependencies: 24 | flutter: 25 | sdk: flutter 26 | 27 | 28 | # The following adds the Cupertino Icons font to your application. 29 | # Use with the CupertinoIcons class for iOS style icons. 30 | cupertino_icons: ^0.1.3 31 | 32 | dev_dependencies: 33 | flutter_test: 34 | sdk: flutter 35 | 36 | # For information on the generic Dart part of this file, see the 37 | # following page: https://dart.dev/tools/pub/pubspec 38 | 39 | # The following section is specific to Flutter. 40 | flutter: 41 | 42 | # The following line ensures that the Material Icons font is 43 | # included with your application, so that you can use the icons in 44 | # the material Icons class. 45 | uses-material-design: true 46 | 47 | # To add assets to your application, add an assets section, like this: 48 | # assets: 49 | # - images/a_dot_burr.jpeg 50 | # - images/a_dot_ham.jpeg 51 | 52 | # An image asset can refer to one or more resolution-specific "variants", see 53 | # https://flutter.dev/assets-and-images/#resolution-aware. 54 | 55 | # For details regarding adding assets from package dependencies, see 56 | # https://flutter.dev/assets-and-images/#from-packages 57 | 58 | # To add custom fonts to your application, add a fonts section here, 59 | # in this "flutter" section. Each entry in this list should have a 60 | # "family" key with the font family name, and a "fonts" key with a 61 | # list giving the asset and other descriptors for the font. For 62 | # example: 63 | # fonts: 64 | # - family: Schyler 65 | # fonts: 66 | # - asset: fonts/Schyler-Regular.ttf 67 | # - asset: fonts/Schyler-Italic.ttf 68 | # style: italic 69 | # - family: Trajan Pro 70 | # fonts: 71 | # - asset: fonts/TrajanPro.ttf 72 | # - asset: fonts/TrajanPro_Bold.ttf 73 | # weight: 700 74 | # 75 | # For details regarding fonts from package dependencies, 76 | # see https://flutter.dev/custom-fonts/#from-packages 77 | --------------------------------------------------------------------------------