一区二区三区欧美日韩-一区二区三区欧美-一区二区三区免费在线视频-一区二区三区免费在线观看-久久精品店-久久精品第一页

歡迎您光臨深圳塔燈網(wǎng)絡(luò)科技有限公司!
電話圖標 余先生:13699882642

網(wǎng)站百科

為您解碼網(wǎng)站建設(shè)的點點滴滴

Flutter 中的Intents

發(fā)表日期:2018-12 文章編輯:小燈 瀏覽次數(shù):2201

Flutter 中的 Intents

可以看看這篇 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

routes 定義多個頁面并省略 home 的寫法

在 Flutter中,編寫一個頁面,必須提供 homereturn 一個 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' 

Flutter中如何在dart 和 java 文件之間傳遞數(shù)據(jù)?

某些數(shù)據(jù)需要在java端處理之后,交由界面顯示,這時候就需要flutter 和java 之間的交互了

首先,在 java 文件ActivityonCreate 中定義如下方法:

 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í)行延時線程去返回方法。

Flutter 中如何跳轉(zhuǎn)到另一個頁面后,獲取返回值?

在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") 傳遞而來。


本頁內(nèi)容由塔燈網(wǎng)絡(luò)科技有限公司通過網(wǎng)絡(luò)收集編輯所得,所有資料僅供用戶學習參考,本站不擁有所有權(quán),如您認為本網(wǎng)頁中由涉嫌抄襲的內(nèi)容,請及時與我們聯(lián)系,并提供相關(guān)證據(jù),工作人員會在5工作日內(nèi)聯(lián)系您,一經(jīng)查實,本站立刻刪除侵權(quán)內(nèi)容。本文鏈接:http://www.junxiaosheng.cn/17755.html
相關(guān)APP開發(fā)
 八年  行業(yè)經(jīng)驗

多一份參考,總有益處

聯(lián)系深圳網(wǎng)站公司塔燈網(wǎng)絡(luò),免費獲得網(wǎng)站建設(shè)方案及報價

咨詢相關(guān)問題或預約面談,可以通過以下方式與我們聯(lián)系

業(yè)務熱線:余經(jīng)理:13699882642

Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.    

主站蜘蛛池模板: 成人在线视频免费| 高h肉文合集| 啊灬啊灬啊灬快高潮视频| 大陆老太交xxxxxhd在线| 国产交换丝雨巅峰| 极品少妇高潮啪啪AV无码吴梦梦| 九九热精品免费观看| 蜜桃日本MV免费观看| 日产亚洲一区二区三区| 小xav导航| 2020亚洲色噜噜狠狠网站| 成年人视频免费在线播放| 国产精品自在在线午夜蜜芽tv在线| 精品久久伊人| 欧洲精品一区二区不卡观看 | 激情内射亚洲一区二区三区| 快插我我好湿啊公交车上做| 日韩高清特级特黄毛片| 亚洲乱码一区二区三区香蕉| 97超碰97资源在线观看| 国产成人无码AV麻豆| 久草草在线视视频| 青娱乐视觉盛宴国产视频| 亚洲AV无码一区二区色情蜜芽 | 岛国大片在线播放免费| 果冻传媒妈妈要儿子| 男男高h浪荡受h| 羞羞漫画在线播放| 2021国产精品国产精华| 国产成人自产拍免费视频| 久久亚洲网站| 晚夜免费禁用十大亏亏| 中文字幕伊人香蕉在线| 国产成人无码区免费内射一片色欲| 久久精品美女久久| 石原莉奈rbd806中文字幕| 最新国自产拍 高清完整版| 国产精品爽爽久久久久久无码 | 久久精品国产96精品亚洲| 日产日韩亚洲欧美综合搜索| 欲插爽乱浪伦骨|