第1节:用于测试与原生项目交互的Flutter项目

第1节:用于测试与原生项目交互的Flutter项目

本节学习内容:通过Flutter的平台通道在Flutter工程中建立提供给原生项目的方法及接收从原生项目返回的值。

本节学习用时:30分钟

本节学习方式:动手实践


本节目录:

一、本节内容介绍

二、本节代码解释


一、本节内容介绍

1、主要介绍的知识点有:

  • 在iOS界面中点击一个按钮跳转到Flutter页面,该Flutter页面有需要传参Main页面

2、要实现的效果如下图所示:

OC跳到Flutter有需要传参的Main页面

3、实现本节效果的代码

本节所有代码文件为:main.dartStatelessMainPage.dartStatefulMainPage.dartNewRouteNoneParam.dartNewRouteWithParam

您可自己通过flutter create materialappproject创建materialappproject项目后,将其lib文件夹下的代码文件替换为如下即可。

3.1、其中完整的main.dart代码如下:
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
import 'package:flutter/material.dart';
import 'StatelessMainPage.dart';
import 'StatefulMainPage.dart';
import 'NewRouteNoneParam.dart';
import 'NewRouteWithParam.dart';



void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Platform Channel Demo',
home: StatelessMainPage(), //不需传参数的Flutter页面
//home: StatefulMainPage(), //需传参数的Flutter页面
routes: {
'StatelessMainPage':(context) => StatelessMainPage(),
'StatefulMainPage':(context) => StatefulMainPage(),
'NewRouteNoneParam':(context) => NewRouteNoneParam(),
'NewRouteWithParam':(context) => NewRouteWithParam()
}
);
}
}
3.2、其中完整的StatelessMainPage.dart代码如下:
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
135
136
137
138
139
140
import 'package:flutter/material.dart';
import 'StatefulMainPage.dart';
import 'NewRouteNoneParam.dart';
import 'NewRouteWithParam.dart';

import 'package:flutter/services.dart';
import 'dart:async';

// 已写在main.dart中
//void main() => runApp(MyApp());
//
//class MyApp extends StatelessWidget {
// @override
// Widget build(BuildContext context) {
// return MaterialApp(
// title: 'Platform Channel Demo',
// home: StatelessMainPage(),
// routes: {
// 'newRoute':(context) => NewRoute(),
// 'newRouteWithParam':(context) => NewRouteWithParam(),
// 'StatefulMainPage':(context) => StatefulMainPage()
// }
// );
// }
//}

class StatelessMainPage extends StatelessWidget {
// 创建一个给native的channel (类似iOS的通知)
static const methodChannel = const MethodChannel('com.dvlproad.ciyouzen/platform_channel');

// 给客户端发送一些东东, 这里先不用去拿回什么
Future<Null> _showToast() async {
try {
await methodChannel.invokeMethod('showToast','这是我在Flutter中写的文字');
} on PlatformException {

}
}

Future<Null> _goBack() async {
try {
await methodChannel.invokeMethod('goBack');
} on PlatformException {

}
}

Future<Null> _goiosPage() async {
try {
Map<String, dynamic> map = { "content": "flutter传给iOS的内容","data":[1,2,3,4,5]};
await methodChannel.invokeMethod('goiosPage', map);
} on PlatformException {

}
}

@override
Widget build(BuildContext context) {
//Scaffold是Material中主要的布局组件.
return new Scaffold(
appBar: new AppBar(
leading: new IconButton(
icon: new Icon(Icons.menu),
tooltip: 'Navigation menu',
onPressed: null,
),
title: new Text('platform channel Example title'),
actions: <Widget>[
// new IconButton(
// icon: new Icon(Icons.search),
// tooltip: 'Search',
// //onPressed: null,
// onPressed: () {
// goStatefulMainPage(context);
// },
// ),
new FlatButton(
child: Image.asset('lib/Resources/nav_right_gray_normal.png'),
onPressed: (){
goStatefulMainPage(context);
}),
],
),
//body占屏幕的大部分
body: new Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text('Hello, platform channel!'),
new RaisedButton(
child: Text('showToast'),
onPressed: (){
_showToast();
}
),
new RaisedButton(
child: Text('goBack'),
onPressed: (){
_goBack();
}
),
new RaisedButton(
child: Text('goiosPage'),
onPressed: (){
_goiosPage();
}
),

],
),
),
floatingActionButton: new FloatingActionButton(
tooltip: 'Add', // used by assistive technologies
child: new Icon(Icons.add),
onPressed: () {
goNextPageNoneParam(context);
},
)
);
}
}

void goNextPageNoneParam(context) {
Navigator.push(context, new MaterialPageRoute(builder: (context) {
return new NewRouteNoneParam();
}));
}


void goNextPageWithParams(context) {
Navigator.push(context, new MaterialPageRoute(builder: (context) {
return new NewRouteWithParam();
}));
}

void goStatefulMainPage(context) {
Navigator.push(context, new MaterialPageRoute(builder: (context) {
return new StatefulMainPage();
}));
}
3.3、其中完整的StatefulMainPage.dart代码如下:
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import 'package:flutter/material.dart';
import 'NewRouteNoneParam.dart';

import 'package:flutter/services.dart';
import 'dart:async';


// 已写在main.dart中
//void main() => runApp(MyApp());
//
//class MyApp extends StatelessWidget {
// @override
// Widget build(BuildContext context) {
// return MaterialApp(
// title: 'Platform Channel Demo',
//
// home: StatefulMainPage(),
// );
// }
//}

class StatefulMainPage extends StatefulWidget {

@override
State<StatefulWidget> createState() {
// TODO: implement createState
return StatefulMainPageState();
}
}

class StatefulMainPageState extends State<StatefulMainPage> {
// 创建一个给native的channel (类似iOS的通知)
static const methodChannel = const MethodChannel(
'com.dvlproad.ciyouzen/platform_channel');

// 给客户端发送一些东东, 这里先不用去拿回什么
Future<Null> _showToast() async {
try {
await methodChannel.invokeMethod('showToast','这是我在Flutter中写的文字');
} on PlatformException {

}
}

Future<Null> _goBack() async {
try {
await methodChannel.invokeMethod('goBack');
} on PlatformException {

}
}

Future<Null> _goiosPage() async {
try {
Map<String, dynamic> map = { "content": "flutter传给iOS的内容","data":[1,2,3,4,5]};
await methodChannel.invokeMethod('goiosPage', map);
} on PlatformException {

}
}

String imageName = "lib/Resources/message_arrow.png";
Future<Null> _changeLeftBarButtonAction() async {
try {
Map<String, dynamic> map = { "content": "flutter传给iOS的内容","data":[1,2,3,4,5]};
final Map dict = await methodChannel.invokeMethod('changeLeftBarButtonAction', map);

setState(() {
imageName = dict["imageName"];
print(imageName);
});
} on PlatformException {

}
}

@override
void initState() {
// TODO: implement initState
super.initState();
_changeLeftBarButtonAction();
}

@override
Widget build(BuildContext context) {
//Scaffold是Material中主要的布局组件.
return new Scaffold(
appBar: new AppBar(
leading: new FlatButton(
// child: Image.asset('lib/Resources/nav_back_white_normal.png'),
child: Image.asset(imageName),
onPressed: (){
_goBack();
}),

// new IconButton(
// icon: new Icon(Icons.menu),
//// icon: Image.asset('lib/Resources/message_arrow.png'),
// tooltip: 'Navigation menu',
// onPressed: null,
// ),
title: new Text('StatefulMainPage'),
actions: <Widget>[
// new IconButton(
// icon: new Icon(Icons.search),
// tooltip: 'Search',
// onPressed: null,
// ),
new FlatButton(
child: Image.asset('lib/Resources/nav_right_gray_normal.png'),
onPressed: null
),
],
),
//body占屏幕的大部分
body: new Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('lib/Resources/message_arrow.png'),
new Text('Hello, StatefulMainPage!'),
new RaisedButton(
child: Text('showToast'),
onPressed: (){
_showToast();
}
),
new RaisedButton(
child: Text('goBack'),
onPressed: (){
_goBack();
}
),
new RaisedButton(
child: Text('goiosPage'),
onPressed: (){
_goiosPage();
}
),

],
),
),
floatingActionButton: new FloatingActionButton(
tooltip: 'Add', // used by assistive technologies
child: new Icon(Icons.add),
onPressed: () {
goNextPageNoneParam(context);
},
)
);
}
}

void goNextPageNoneParam(context) {
Navigator.push(context, new MaterialPageRoute(builder: (context) {
return new NewRouteNoneParam();
}));
}
3.4、其中完整的NewRouteNoneParam.dart代码如下:
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
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';

class NewRouteNoneParam extends StatelessWidget {
static const methodChannel = const MethodChannel('com.dvlproad.ciyouzen/platform_channel');
Future<Null> _goBack() async {
try {
await methodChannel.invokeMethod('goBack');
} on PlatformException {

}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("New route none Param"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("This is new route"),
RaisedButton(
child: Text('goback'),
onPressed: _goBack
)
],
)

),
);
}
}
3.5、其中完整的NewRouteWithParam.dart代码如下:
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
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';

class NewRouteWithParam extends StatelessWidget {
static const methodChannel = const MethodChannel('com.dvlproad.ciyouzen/platform_channel');
Future<Null> _goBack() async {
try {
await methodChannel.invokeMethod('goBack');
} on PlatformException {

}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("New route with params"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("This is new route with params"),
RaisedButton(
child: Text('goback'),
onPressed: _goBack
)
],
)

),
);
}
}