第1节:用于测试交互的Flutter项目
Last updated
Last updated
本节学习内容:通过Flutter的平台通道在Flutter工程中建立提供给原生项目的方法及接收从原生项目返回的值。
本节学习用时:30分钟
本节学习方式:动手实践
本节目录:
一、本节内容介绍
二、本节代码解释
在iOS界面中点击一个按钮跳转到Flutter页面,该Flutter页面有需要传参Main页面
本节所有代码文件为:main.dart
、StatelessMainPage.dart
、StatefulMainPage.dart
、NewRouteNoneParam.dart
、NewRouteWithParam
。
您可自己通过
flutter create materialappproject
创建materialappproject项目后,将其lib文件夹下的代码文件替换为如下即可。
main.dart
代码如下: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()
}
);
}
}
StatelessMainPage.dart
代码如下: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();
}));
}
StatefulMainPage.dart
代码如下: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();
}));
}
NewRouteNoneParam.dart
代码如下: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
)
],
)
),
);
}
}
NewRouteWithParam.dart
代码如下: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
)
],
)
),
);
}
}