flutter - Get路由,传递参数

/**
 * Author : wn
 * Email : maoning20080808@163.com
 * Date : 2025/7/20 16:50
 * Description : Get路由,入口
 */

class GetNavigatorDemoWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    return GetMaterialApp(
      initialRoute: "/",
      getPages: [
        GetPage(name: "/", page: ()=> GetHomePageWidget()),
        GetPage(name: "/second", page:() => GetSecondPageWidget()),
        GetPage(name: "/third", page: () => GetThirdPageWidget()),
        GetPage(name: "/four", page: () => GetFourPageWidget(),
          binding: BindingsBuilder((){
            Get.lazyPut<FourController>(() => FourController());
          })
        ),
        GetPage(name: "/five/:id", page: () => GetFivePageWidget()),
        GetPage(name: "/six", page: () => GetSixPageWidget()),
      ],
      title: "Get导航",
      home: Scaffold(
        appBar: AppBar(
          title: Text("Get导航bar"),
        ),

        body: Column(
          children: [
            Text("使用Get.toName()传递不同的参数", style: TextStyle(fontSize: 22, color: Colors.red, fontWeight: FontWeight.bold),),
            SizedBox(height: 20,),
            ElevatedButton(onPressed: (){
              Get.to(GetHomePageWidget());
            }, child: Text("导航 - 首页", style: TextStyle(fontSize: 22, color: Colors.red),)),
            SizedBox(height: 20,),

            ElevatedButton(onPressed: (){
              Get.toNamed("/second", arguments: {"key1":"aaaaa", "key2":"bbbbb"});
            }, child: Text("导航 - 第二页面 - arguments", style: TextStyle(fontSize: 22, color: Colors.green),)),
            SizedBox(height: 20,),

            ElevatedButton(onPressed: (){
              Get.toNamed("/third");
            }, child: Text("导航 - 第三页面", style: TextStyle(fontSize: 22, color: Colors.blue),)),
            SizedBox(height: 20,),

            ElevatedButton(onPressed: (){
              Get.toNamed("/four", arguments: {
                "name":"aaa",
                "age":22
              });
            }, child: Text("导航 - 第四页面 - 动态路由绑定参数", style: TextStyle(fontSize: 22, color: Colors.red),)),
            SizedBox(height: 20,),

            ElevatedButton(onPressed: (){
              Get.toNamed("/five/aabb");
            }, child: Text("导航 - 第5个页面 - /:param", style: TextStyle(fontSize: 22, color: Colors.green),)),
            SizedBox(height: 20,),

            ElevatedButton(onPressed: (){
              Get.toNamed("/six", arguments: SixPageArguments(name: "wnflutter", age: 8),);
            }, child: Text("导航 - 第6个页面 - 对象", style: TextStyle(fontSize: 22, color: Colors.blue),)),

          ],
        ),
      ),
    );
  }
}



//第5个页面
class GetFivePageWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    //接收参数
    final param1 = Get.parameters["id"];

    return Scaffold(
      appBar: AppBar(
        title: Text("GetFivePageWidget组件bar"),
      ),

      body: Container(
        child: Text("第5个组件 , 5a参数:${param1}", style: TextStyle(color: Colors.green, fontSize: 30),),
      ),
    );
  }
}

/**
 * Author : wn
 * Email : maoning20080808@163.com
 * Date : 2025/7/20 16:55
 * Description :
 */
class GetHomePageWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text("GetHomePage组件bar"),
      ),

      body: Container(
        child: Text("GetHomePage组件", style: TextStyle(color: Colors.red, fontSize: 30),),
      ),
    );
  }
}

/**
 * Author : wn
 * Email : maoning20080808@163.com
 * Date : 2025/7/20 16:55
 * Description :
 */
class GetSecondPageWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    //获取参数
    final arguments = Get.arguments as Map<String, String>;
    final value1 = arguments["key1"];
    final value2 = arguments["key2"];

    return Scaffold(
      appBar: AppBar(
        title: Text("GetSecondPage组件bar"),
      ),

      body: Container(
        child: Column(
          children: [
            Text("GetSecondPage组件", style: TextStyle(color: Colors.green, fontSize: 30),),

            Text("获取到参数:value1 = ${value1} , value2 = ${value2}"),
          ],
        ),
      ),
    );
  }
}

/**
 * Author : wn
 * Email : maoning20080808@163.com
 * Date : 2025/7/20 16:55
 * Description :
 */
class GetThirdPageWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text("GetThirdPageWidget组件bar"),
      ),

      body: Container(
        child: Column(
          children: [
            Text("GetThirdPageWidget组件", style: TextStyle(color: Colors.blue, fontSize: 30),),

            ElevatedButton(onPressed: (){
              //清除历史记录
              Get.offAllNamed("/");
            }, child: Text("导航到首页", style: TextStyle(fontSize: 20, color: Colors.red),)),
          ],
        ),
        //child: Text("GetThirdPageWidget组件", style: TextStyle(color: Colors.blue, fontSize: 30),),
      ),
    );
  }
}

/**
 * Author : wn
 * Email : maoning20080808@163.com
 * Date : 2025/7/20 16:55
 * Description : 第4个页面
 */
class GetFourPageWidget extends StatelessWidget {

  FourController fourController = Get.put(FourController());

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text("GetFourPageWidget组件bar"),
      ),

      body: Container(
        child: Column(
          children: [
            Text("GetFourPageWidget组件", style: TextStyle(color: Colors.blue, fontSize: 30),),
            Text("接收参数name =  ${fourController.name}", style: TextStyle(color: Colors.red, fontSize: 30),),
            Text("接收参数age =  ${fourController.age}", style: TextStyle(color: Colors.green, fontSize: 30),),
          ],
        ),

      ),
    );
  }
}

/**
 * Author : wn
 * Email : maoning20080808@163.com
 * Date : 2025/7/20 18:18
 * Description :
 */
class FourController extends GetxController {
  late String name;
  late int age;

  @override
  void onInit() {
    super.onInit();

    final args = Get.arguments as Map<String, dynamic>;
    name = args["name"];
    age = args["age"];

  }

}

/**
 * Author : wn
 * Email : maoning20080808@163.com
 * Date : 2025/7/20 16:55
 * Description : 第6个页面
 */
class GetSixPageWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    print("接收参数:${Get.arguments}");

    //获取参数
    final arguments = Get.arguments as SixPageArguments;
    final name = arguments.name;
    final age = arguments.age;

    return Scaffold(
      appBar: AppBar(
        title: Text("GetSixPage组件bar"),
      ),

      body: Container(
        child: Column(
          children: [
            Text("GetSixPage组件", style: TextStyle(color: Colors.green, fontSize: 30),),

            Text("获取到参数:name = ${name} , age = ${age}"),
          ],
        ),
      ),
    );
  }
}
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐