為您解碼網(wǎng)站建設(shè)的點點滴滴
發(fā)表日期:2018-12 文章編輯:小燈 瀏覽次數(shù):2201
可以看看這篇 Flutter
頁面跳轉(zhuǎn),攜帶參數(shù)的頁面跳轉(zhuǎn)的使用和說明點擊跳轉(zhuǎn)
在Android中,Intent 用來做Activity 之間的跳轉(zhuǎn),又或者用來發(fā)送廣播,傳遞消息。那么,在Flutter中,它有什么替代方式呢?
如果需要在Flutter中進行頁面切換,可以使用路由,具體代碼如下:
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
然后定義了按鈕點擊事件,使其跳轉(zhuǎn)到pageA
。
在 Flutter中,編寫一個頁面,必須提供 home
并 return
一個 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("點擊返回"), onPressed: () => Navigator.of(context).pop("this is result text"), color: Colors.blue, highlightColor: Colors.lightBlue, ), ), ); } }
在routes 中,必須提供
/
這樣的一個根目錄,否則是會報錯的,就像這樣
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(); }, }); } }
錯誤:
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端處理之后,交由界面顯示,這時候就需要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; }); } } }
運行程序就可正確的獲取到 shareText
了
在看官方demo中,發(fā)現(xiàn)了
await
字眼,說明應該是延時調(diào)用,所以,嘗試在activity
中也延時返回數(shù)據(jù),發(fā)現(xiàn),并不會導致程序阻塞,但是會導致數(shù)據(jù)獲取失敗,在實際測試中,當延遲在1600
的時候,有時成功獲取shareText
,有時則失敗。具體代碼如下:
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í)行延時線程去返回方法。
在Android原生編程中,可以使用
startActivityForResult
來獲取另一個頁面的返回值,那么,在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("點擊返回"), 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)到另一個頁面,然后使用 Future.then
獲取返回,這個返回值是在另一個頁面中,通過Navigator.of(context).pop("this is result text")
傳遞而來。
日期:2018-10 瀏覽次數(shù):7257
日期:2018-12 瀏覽次數(shù):4332
日期:2018-07 瀏覽次數(shù):4882
日期:2018-12 瀏覽次數(shù):4178
日期:2018-09 瀏覽次數(shù):5505
日期:2018-12 瀏覽次數(shù):9926
日期:2018-11 瀏覽次數(shù):4809
日期:2018-07 瀏覽次數(shù):4586
日期:2018-05 瀏覽次數(shù):4863
日期:2018-12 瀏覽次數(shù):4329
日期:2018-10 瀏覽次數(shù):5144
日期:2018-12 瀏覽次數(shù):6218
日期:2018-11 瀏覽次數(shù):4471
日期:2018-08 瀏覽次數(shù):4594
日期:2018-11 瀏覽次數(shù):12641
日期:2018-09 瀏覽次數(shù):5586
日期:2018-12 瀏覽次數(shù):4839
日期:2018-10 瀏覽次數(shù):4194
日期:2018-11 瀏覽次數(shù):4533
日期:2018-12 瀏覽次數(shù):6069
日期:2018-06 瀏覽次數(shù):4010
日期:2018-08 瀏覽次數(shù):5441
日期:2018-10 瀏覽次數(shù):4460
日期:2018-12 瀏覽次數(shù):4533
日期:2018-07 瀏覽次數(shù):4364
日期:2018-12 瀏覽次數(shù):4504
日期:2018-06 瀏覽次數(shù):4387
日期:2018-11 瀏覽次數(shù):4377
日期:2018-12 瀏覽次數(shù):4253
日期:2018-12 瀏覽次數(shù):5287
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.