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

歡迎您光臨深圳塔燈網(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://m.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.    

主站蜘蛛池模板: 成人国产精品免费网站| 成人精品视频在线观看| 艺术片 快播| 高清午夜福利电影在线| 国产成年人在线观看| 九九热这里只有精品视频免费| 欧美精品专区免费观看| 人妻 中文无码 中出| 琪琪SEE色原网色原网站18| 亚洲精品m在线观看| 中文字幕A片视频一区二区| 99久久精品国产亚洲AV| 动漫美女的禁| 国产精品自在自线亚洲| 九色PORNY真实丨国产免费| 青青草原国产| 2021国产精品久久久久精品免费网 | 56prom在线精品国产| 国产亚洲精品久久无码98| 久九九精品免费视频| 乡土女性网动态图解| 东日韩二三区| 奇米色偷偷| 99精品免费在线观看| 久久怡红院国产精品| 色噜噜噜噜亚洲第一| 99精品视频免费观看| 老年日本老年daddy| 一手揉着乳头一手模仿抽插视频 | 日本wwwxx| jizz日本黄色| 嗯啊…嗯np男男双性总受| 综合色中色| 国产精品免费观看视频| 日本精品久久久久中文字幕 1| 亚洲人成电影网站在线观看| md2.pud 麻豆传媒官网| 免费夜色污私人影院网站| 5g在线视讯年龄确认海外禁止进入| 久久青草免费91线频观看站街| 少妇伦子伦精品无码|