為您解碼網(wǎng)站建設(shè)的點(diǎn)點(diǎn)滴滴
發(fā)表日期:2018-12 文章編輯:小燈 瀏覽次數(shù):2225
可以看看這篇 Flutter
頁面跳轉(zhuǎn),攜帶參數(shù)的頁面跳轉(zhuǎn)的使用和說明點(diǎn)擊跳轉(zhuǎn)
在Android中,Intent 用來做Activity 之間的跳轉(zhuǎn),又或者用來發(fā)送廣播,傳遞消息。那么,在Flutter中,它有什么替代方式呢?
如果需要在Flutter中進(jìn)行頁面切換,可以使用路由,具體代碼如下:
import 'package:flutter/material.dart';//void main() => runApp(MaterialApp(home: DemoApp()));void main() { runApp(MaterialApp( home: DemoApp(), // becomes the route named '/' routes: <String, WidgetBuilder>{ '/a': (BuildContext context) => MyPage(title: "page A"), '/b': (BuildContext context) => MyPage(title: 'page B'), '/c': (BuildContext context) => MyPage(title: 'page C'), }, )); }class MyPage extends StatelessWidget { final String title;MyPage({this.title});Widget build(BuildContext context) { return Center(child: Text(title)); } }class DemoApp extends StatelessWidget { //Widget build(BuildContext context) => Scaffold(body: Signature()); Widget build(BuildContext context) { return Scaffold( body: Center( child: Text("DemoApp"), ), floatingActionButton: FloatingActionButton( onPressed: () => _toPageA(context), tooltip: 'Update Text', child: Icon(Icons.update), ), ); }void _toPageA(BuildContext context) { Navigator.of(context).pushNamed("/a"); } }
首先自定義了
routes
然后定義了按鈕點(diǎn)擊事件,使其跳轉(zhuǎn)到pageA
。
在 Flutter中,編寫一個(gè)頁面,必須提供 home
并 return
一個(gè) Widght
來顯示頁面,但是在 MaterialApp
中,通過編寫 routes
是可以忽略掉 home
的,就像下面的例子:
import 'package:flutter/material.dart';void main() { runApp(new FlutterReduxApp()); }class FlutterReduxApp extends StatelessWidget { FlutterReduxApp({Key key}) : super(key: key);@override Widget build(BuildContext context) { return new MaterialApp(routes: { "/": (context) { //store.state.platformLocale = Localizations.localeOf(context); return MyPage(); }, "/b": (context) { ///通過 Localizations.override 包裹一層, return MyPage(); }, "/c": (context) { return MyPage(); }, }); } }class MyPage extends StatelessWidget { final String title;MyPage({this.title});Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar(title: new Text("this is Push Page")), body: new Center( child: new RaisedButton( child: new Text("點(diǎn)擊返回"), onPressed: () => Navigator.of(context).pop("this is result text"), color: Colors.blue, highlightColor: Colors.lightBlue, ), ), ); } }
在routes 中,必須提供
/
這樣的一個(gè)根目錄,否則是會(huì)報(bào)錯(cuò)的,就像這樣
class FlutterReduxApp extends StatelessWidget { FlutterReduxApp({Key key}) : super(key: key);@override Widget build(BuildContext context) { return new MaterialApp(routes: { "/a": (context) { //store.state.platformLocale = Localizations.localeOf(context); return MyPage(); }, "/b": (context) { ///通過 Localizations.override 包裹一層, return MyPage(); }, "/c": (context) { return MyPage(); }, }); } }
錯(cuò)誤:
I/flutter (20735): 'package:flutter/src/widgets/app.dart': Failed assertion: line 178 pos 10: 'builder != null || I/flutter (20735):home != null || I/flutter (20735):routes.containsKey(Navigator.defaultRouteName) || I/flutter (20735):onGenerateRoute != null || I/flutter (20735):onUnknownRoute != null'
某些數(shù)據(jù)需要在java端處理之后,交由界面顯示,這時(shí)候就需要flutter 和java 之間的交互了
首先,在 java 文件Activity
的onCreate
中定義如下方法:
package com.example.fluttertestapp;import android.os.Bundle; import android.os.Handler; import android.widget.Toast;import io.flutter.app.FlutterActivity; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugins.GeneratedPluginRegistrant;public class MainActivity extends FlutterActivity {private final String sharedText = "this is shared text"; Handler handler;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GeneratedPluginRegistrant.registerWith(this); handler = new Handler(); //handler.postDelayed(new Runnable() { //@Override //public void run() { // //Toast.makeText(MainActivity.this, String.format("start main thread:%d", 1600), Toast.LENGTH_SHORT).show(); //} //}, 1600); new MethodChannel(getFlutterView(), "app.channel.shared.data") .setMethodCallHandler(new MethodChannel.MethodCallHandler() { @Override public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { if (methodCall.method.contentEquals("getSharedText")) { result.success(sharedText); } } });} }
然后在 main.dart
中定義
import 'package:flutter/material.dart'; import 'package:flutter/services.dart';void main() { runApp(SampleApp()); }class SampleApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Sample Shared App Handler', theme: ThemeData( primarySwatch: Colors.blue, ), home: SampleAppPage(), ); } }class SampleAppPage extends StatefulWidget { SampleAppPage({Key key}) : super(key: key);@override _SampleAppPageState createState() => _SampleAppPageState(); }class _SampleAppPageState extends State<SampleAppPage> { static const platform = const MethodChannel('app.channel.shared.data'); String dataShared = "No data";@override void initState() { super.initState(); getSharedText(); }@override Widget build(BuildContext context) { return Scaffold(body: Center(child: Text(dataShared))); }getSharedText() async { var sharedData = await platform.invokeMethod("getSharedText"); if (sharedData != null) { setState(() { dataShared = sharedData; }); } } }
運(yùn)行程序就可正確的獲取到 shareText
了
在看官方demo中,發(fā)現(xiàn)了
await
字眼,說明應(yīng)該是延時(shí)調(diào)用,所以,嘗試在activity
中也延時(shí)返回?cái)?shù)據(jù),發(fā)現(xiàn),并不會(huì)導(dǎo)致程序阻塞,但是會(huì)導(dǎo)致數(shù)據(jù)獲取失敗,在實(shí)際測(cè)試中,當(dāng)延遲在1600
的時(shí)候,有時(shí)成功獲取shareText
,有時(shí)則失敗。具體代碼如下:
package com.example.fluttertestapp;import android.os.Bundle; import android.os.Handler; import android.widget.Toast;import io.flutter.app.FlutterActivity; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugins.GeneratedPluginRegistrant;public class MainActivity extends FlutterActivity {private final String sharedText = "this is shared text"; Handler handler;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GeneratedPluginRegistrant.registerWith(this); handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { new MethodChannel(getFlutterView(), "app.channel.shared.data") .setMethodCallHandler(new MethodChannel.MethodCallHandler() { @Override public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { if (methodCall.method.contentEquals("getSharedText")) { result.success(sharedText); } } });Toast.makeText(MainActivity.this, String.format("start main thread:%d", 1600), Toast.LENGTH_SHORT).show(); } }, 1600);} }
為了防止調(diào)用方法的失敗,最好不要在主線程中執(zhí)行延時(shí)線程去返回方法。
在Android原生編程中,可以使用
startActivityForResult
來獲取另一個(gè)頁面的返回值,那么,在Flutter
中呢
import 'package:flutter/material.dart';void main() { runApp(MaterialApp( home: DemoApp(), // becomes the route named '/' routes: <String, WidgetBuilder>{ '/a': (BuildContext context) { debugPrint("select route A"); //Navigator.of(context).pop("this is result text"); return MyPage(title: "page A"); } }, )); }class MyPage extends StatelessWidget { final String title;MyPage({this.title});Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar(title: new Text("this is Push Page")), body: new Center( child: new RaisedButton( child: new Text("點(diǎn)擊返回"), onPressed: () => Navigator.of(context).pop("this is result text"), color: Colors.blue, highlightColor: Colors.lightBlue, ), ), ); } }class DemoApp extends StatelessWidget { //Widget build(BuildContext context) => Scaffold(body: Signature()); Widget build(BuildContext context) { return Scaffold( body: Center( child: Text("DemoApp"), ), floatingActionButton: FloatingActionButton( onPressed: () => _toPageA(context), tooltip: 'Update Text', child: Icon(Icons.update), ), ); }Future _toPageA(BuildContext context) { debugPrint("this is debug info"); //Navigator.of(context).pushNamed("/a"); Future result = Navigator.of(context).pushNamed("/a"); result.then((value) { debugPrint(value); }); } }
首先,通過 Navigator.of(context).pushNamed("/a")
跳轉(zhuǎn)到另一個(gè)頁面,然后使用 Future.then
獲取返回,這個(gè)返回值是在另一個(gè)頁面中,通過Navigator.of(context).pop("this is result text")
傳遞而來。
日期:2018-10 瀏覽次數(shù):7353
日期:2018-12 瀏覽次數(shù):4421
日期:2018-07 瀏覽次數(shù):4954
日期:2018-12 瀏覽次數(shù):4254
日期:2018-09 瀏覽次數(shù):5592
日期:2018-12 瀏覽次數(shù):10011
日期:2018-11 瀏覽次數(shù):4895
日期:2018-07 瀏覽次數(shù):4663
日期:2018-05 瀏覽次數(shù):4945
日期:2018-12 瀏覽次數(shù):4398
日期:2018-10 瀏覽次數(shù):5222
日期:2018-12 瀏覽次數(shù):6297
日期:2018-11 瀏覽次數(shù):4552
日期:2018-08 瀏覽次數(shù):4675
日期:2018-11 瀏覽次數(shù):12736
日期:2018-09 瀏覽次數(shù):5661
日期:2018-12 瀏覽次數(shù):4922
日期:2018-10 瀏覽次數(shù):4264
日期:2018-11 瀏覽次數(shù):4613
日期:2018-12 瀏覽次數(shù):6147
日期:2018-06 瀏覽次數(shù):4088
日期:2018-08 瀏覽次數(shù):5536
日期:2018-10 瀏覽次數(shù):4534
日期:2018-12 瀏覽次數(shù):4619
日期:2018-07 瀏覽次數(shù):4446
日期:2018-12 瀏覽次數(shù):4596
日期:2018-06 瀏覽次數(shù):4478
日期:2018-11 瀏覽次數(shù):4455
日期:2018-12 瀏覽次數(shù):4335
日期:2018-12 瀏覽次數(shù):5358
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.