1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'dart:async';
void main() => runApp(new MyApp());
Widget _widgetForRoute(String route) { switch (route) { case 'route1': return MyHomePage(title: 'Flutter Demo Home Page1'); case 'route2': return MyHomePage(title: 'Flutter Demo Home Page2'); default: return MyHomePage(title: 'Flutter Demo Home Page2'); } }
class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: _widgetForRoute(window.defaultRouteName), ); } }
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; //获取到插件与原生的交互通道 需要和android的key一致 static const toAndroidPlugin = const MethodChannel('com.guoshikeji.toandroid/project_details'); static const fromAndroiPlugin = const EventChannel('com.guoshikeji.toflutter/project_details'); StreamSubscription _fromAndroiSub; var _nativeParams; @override void initState() { // TODO: implement initState super.initState(); _startfromAndroiPlugin();//在initState中初始化 } //注册从android获取消息的监听 void _startfromAndroiPlugin(){ if(_fromAndroiSub == null){ _fromAndroiSub = fromAndroiPlugin.receiveBroadcastStream() .listen(_onfromAndroiEvent,onError: _onfromAndroiError); } } //获取成功的方法 void _onfromAndroiEvent(Object event) { setState(() { _nativeParams = event; }); } //获取失败的方法 void _onfromAndroiError(Object error) { setState(() { _nativeParams = "error"; print(error); }); } //发送消息 Future<Null> _jumpToNative() async { String result = await toAndroidPlugin.invokeMethod('withoutParams');
print(result); } //发送带参数的消息 Future<Null> _jumpToNativeWithParams() async { Map<String, String> map = { "flutter": "这是一条来自flutter的参数" }; String result = await toAndroidPlugin.invokeMethod('withParams', map); print(result); }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( // Here we take the value from the MyHomePage object that was created by // the App.build method, and use it to set our appbar title. title: Text(widget.title), ), body: Center( child: new ListView( children: <Widget>[ new Padding( padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0), child: new RaisedButton( textColor: Colors.black, child: new Text('跳转到原生界面'), onPressed: () { _jumpToNative(); }), ), new Padding( padding: const EdgeInsets.only( left: 10.0, top: 10.0, right: 10.0), child: new RaisedButton( textColor: Colors.black, child: new Text('跳转到原生界面(带参数)'), onPressed: () { _jumpToNativeWithParams(); }), ),
new Padding( padding: const EdgeInsets.only( left: 10.0, top: 10.0, right: 10.0), child: new Text('这是一个从原生获取的参数:$_nativeParams'), ) , new Padding( padding: const EdgeInsets.only( left: 10.0, top: 10.0, right: 10.0), child: new Text('Flutter floatingActionButton 点击次数$_counter'), )
], ) ), ); } }
|