Flutter “跳轉(zhuǎn)頁(yè)面”(二)

前言

寫(xiě)了這么多文章绣否,有翻譯文檔的,有自己理解的洪鸭,也不知道到底是怎么樣的風(fēng)格更能讓人接受样刷,希望大家能給點(diǎn)意見(jiàn)或建議。

正文

一里面已經(jīng)把“頁(yè)面跳轉(zhuǎn)”的操作基本上都說(shuō)完了览爵,那當(dāng)一個(gè)頁(yè)面打開(kāi)后是需要一些返回信息的時(shí)候應(yīng)該怎么做呢置鼻?還是繼續(xù)看原文吧

Routes can return a value route們可以返回一個(gè)值

When a route is pushed to ask the user for a value, the value can be returned via the pop method's result parameter.
Methods that push a route return a Future. The Future resolves when the route is popped and the Future's value is the pop method's result parameter.
For example if we wanted to ask the user to press 'OK' to confirm an operation we could await the result of Navigator.push:

當(dāng)一個(gè)route加載到頁(yè)面并需要返回一個(gè)值的時(shí)候,這個(gè)值會(huì)在pop方法返回
Navigatorpush方法會(huì)返回一個(gè)Future蜓竹,這個(gè)東西可以在dart教程里面找到箕母。這個(gè)Future會(huì)在routepop的時(shí)候處理储藐,而這個(gè)Future的值就是pop方法里的result參數(shù)。
比如嘶是,當(dāng)我們想在用戶(hù)點(diǎn)擊ok的時(shí)候去確認(rèn)一個(gè)操作钙勃,這個(gè)時(shí)候我們可以await這個(gè)push的結(jié)果。await的用法也可以在dart的教程里找到聂喇。

bool value = await Navigator.push(context, new MaterialPageRoute<bool>(
  builder: (BuildContext context) {
    return new Center(
      child: new GestureDetector(
        child: new Text('OK'),
        onTap: () { Navigator.pop(context, true); }
      ),
    );
  }
));

上面的代碼辖源,如果用戶(hù)按了ok鍵,則返回的值是true希太,如果用戶(hù)是按的返回鍵克饶,則返回值是null

When a route is used to return a value, the route's type parameter must match the type of pop's result. That's why we've used MaterialPageRoute<bool> instead of MaterialPageRoute<void> or justMaterialPageRoute. (If you prefer to not specify the types, though, that's fine too.)

當(dāng)一個(gè)route被用來(lái)返回一個(gè)值的時(shí)候,這個(gè)route的參數(shù)類(lèi)型必須和結(jié)果返回的類(lèi)型一致誊辉,這就是為什么我們用MaterialPageRoute<bool>而不用MaterialPageRoute<void>或直接用MaterialPageRoute矾湃。(如果你就不想指定返回類(lèi)型,也是可以的)

Popup routes “彈出route”

Routes don't have to obscure the entire screen. PopupRoutes cover the screen with a ModalRoute.barrierColor that can be only partially opaque to allow the current screen to show through. Popup routes are "modal" because they block input to the widgets below.
There are functions which create and show popup routes. For example: showDialog, showMenu, and showModalBottomSheet. These functions return their pushed route's Future as described above. Callers can await the returned value to take an action when the route is popped, or to discover the route's value.
There are also widgets which create popup routes, like PopupMenuButton and DropdownButton. These widgets create internal subclasses of PopupRoute and use the Navigator's push and pop methods to show and dismiss them.

Route并不一定非要跟屏幕一樣大芥映,彈窗可以用ModalRoute.barrierColor使部分當(dāng)前屏幕不透明洲尊,用來(lái)顯示內(nèi)容。彈窗是一種“模態(tài)”窗口奈偏,因?yàn)樗i定了他下面的控件坞嘀。這里有一些創(chuàng)建和顯示彈窗的方法。比如: showDialog, showMenu, 和 showModalBottomSheet惊来。這些方法的返回的Future和上面所說(shuō)的是一樣的丽涩。當(dāng)這個(gè)彈窗出棧的時(shí)候,調(diào)用者可以通過(guò)await拿到這個(gè)返回值裁蚁。
這里還有一些其他的方法來(lái)創(chuàng)建彈窗矢渊。比如:PopupMenuButton and DropdownButton。這些控件也通過(guò)Navigatorpushpop來(lái)控制顯示消失枉证。

Custom routes 自定義route

You can create your own subclass of one of the widget library route classes like PopupRoute, ModalRoute, or PageRoute, to control the animated transition employed to show the route, the color and behavior of the route's modal barrier, and other aspects of the route.
The PageRouteBuilder class makes it possible to define a custom route in terms of callbacks. Here's an example that rotates and fades its child when the route appears or disappears. This route does not obscure the entire screen because it specifies opaque: false, just as a popup route does.

你可以創(chuàng)建任何一個(gè)route的子類(lèi)矮男,并控制他們的動(dòng)畫(huà)、顏色室谚、行為等等毡鉴。PageRouteBuilder類(lèi)用各種回調(diào)來(lái)控制這些,下面的例子是一個(gè)頁(yè)面以旋轉(zhuǎn)的方式來(lái)顯示和隱藏秒赤。這個(gè)route不會(huì)讓整個(gè)屏幕變灰猪瞬,因?yàn)樗付?code>opaque:false:

Navigator.push(context, new PageRouteBuilder(
  opaque: false,
  pageBuilder: (BuildContext context, _, __) {
    return new Center(child: new Text('My PageRoute'));
  },
  transitionsBuilder: (___, Animation<double> animation, ____, Widget child) {
    return new FadeTransition(
      opacity: animation,
      child: new RotationTransition(
        turns: new Tween<double>(begin: 0.5, end: 1.0).animate(animation),
        child: child,
      ),
    );
  }
));
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市入篮,隨后出現(xiàn)的幾起案子陈瘦,更是在濱河造成了極大的恐慌,老刑警劉巖潮售,帶你破解...
    沈念sama閱讀 218,546評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件痊项,死亡現(xiàn)場(chǎng)離奇詭異锅风,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)线婚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)遏弱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人塞弊,你說(shuō)我怎么就攤上這事漱逸。” “怎么了游沿?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,911評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵饰抒,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我诀黍,道長(zhǎng)袋坑,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,737評(píng)論 1 294
  • 正文 為了忘掉前任眯勾,我火速辦了婚禮枣宫,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吃环。我一直安慰自己也颤,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布郁轻。 她就那樣靜靜地躺著翅娶,像睡著了一般。 火紅的嫁衣襯著肌膚如雪好唯。 梳的紋絲不亂的頭發(fā)上竭沫,一...
    開(kāi)封第一講書(shū)人閱讀 51,598評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音骑篙,去河邊找鬼蜕提。 笑死,一個(gè)胖子當(dāng)著我的面吹牛靶端,可吹牛的內(nèi)容都是我干的谎势。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼躲查,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼它浅!你這毒婦竟也來(lái)了译柏?” 一聲冷哼從身側(cè)響起镣煮,我...
    開(kāi)封第一講書(shū)人閱讀 39,249評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鄙麦,沒(méi)想到半個(gè)月后典唇,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體镊折,經(jīng)...
    沈念sama閱讀 45,696評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評(píng)論 3 336
  • 正文 我和宋清朗相戀三年介衔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了恨胚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,013評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡炎咖,死狀恐怖赃泡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情乘盼,我是刑警寧澤升熊,帶...
    沈念sama閱讀 35,731評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站绸栅,受9級(jí)特大地震影響级野,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜粹胯,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評(píng)論 3 330
  • 文/蒙蒙 一蓖柔、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧风纠,春花似錦况鸣、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,929評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至栈幸,卻和暖如春愤估,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背速址。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,048評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工玩焰, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人芍锚。 一個(gè)月前我還...
    沈念sama閱讀 48,203評(píng)論 3 370
  • 正文 我出身青樓昔园,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親并炮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子默刚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,332評(píng)論 0 10
  • 8月的天氣還是這么的悶熱,我還是有點(diǎn)受不了的逃魄,但是心情煩悶跟天氣一點(diǎn)兒關(guān)系也沒(méi)有荤西,倒跟家里的瑣事扯上關(guān)系。 我一直...
    漪白閱讀 168評(píng)論 0 0