flutter —— 使用 getx 進行路由管理


路由

基礎方法

Get.toNamed("/NextScreen");
 
Get.offNamed("/NextScreen");
 
Get.offAllNamed("/NextScreen");

 

路由傳參

Get.toNamed("/NextScreen", arguments: 'Get is the best');

print(Get.arguments);
//print out: Get is the best

 

命令路由

void main() {
  runApp(
    GetMaterialApp(
      initialRoute: '/',
      getPages: [
      GetPage(
        name: '/',
        page: () => MyHomePage(),
      ),
      GetPage(
        name: '/profile/',
        page: () => MyProfile(),
      ),
       //你可以為有參數的路由定義一個不同的頁面,也可以為沒有參數的路由定義一個不同的頁面,但是你必須在不接收參數的路由上使用斜杠"/",就像上面說的那樣。
       GetPage(
        name: '/profile/:user',
        page: () => UserProfile(),
      ),
      GetPage(
        name: '/third',
        page: () => Third(),
        transition: Transition.cupertino  
      ),
     ],
    )
  );
}

// 跳轉路由
Get.toNamed("/second/34954");

// 獲取路由參數
print(Get.parameters['user']);
// out: 34954

 

返回路由傳值

在購物車、訂單中很有用

// 導航到該路由,通過 await 獲取回調結果
var data = await Get.to(Payment());
if(data == 'success') madeAnything();

// 傳遞回調內容
Get.back(result: 'success');

  

免 context 導航

Snackbar

Get.snackbar('Hi', 'i am a modern snackbar');

 

也可以使用 Get.rawSnackbar()

 

Dialog

Get.dialog(YourDialogWidget());

Get.defaultDialog(
  onConfirm: () => print("Ok"),
  middleText: "Dialog made in 3 lines of code"
);

// 還可以使用 Get.generalDialog 代替 showGeneralDialog

 

對於所有其他的FlutterDialogs小部件,包括cupertinos,你可以使用Get.overlayContext來代替context,並在你的代碼中任何地方打開它。 對於不使用Overlay的小組件,你可以使用Get.context。 這兩個context在99%的情況下都可以代替你的UIcontext,除了在沒有導航context的情況下使用 inheritedWidget的情況。

 

BottomSheet

Get.bottomSheet(
  Container(
    child: Wrap(
      children: <Widget>[
        ListTile(
          leading: Icon(Icons.music_note),
          title: Text('Music'),
          onTap: () {}
        ),
        ListTile(
          leading: Icon(Icons.videocam),
          title: Text('Video'),
          onTap: () {},
        ),
      ],
    ),
  )
);

 

 

2333


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM