[Flutter] Get路由传值

Get路由传值方法

跳转路由First --> Second

1:第一种方法

Second页面的的controller利用构造函数接收由First传过来的值,在binding注入时接收First页面传过来的值。

///second_state
///定义要从First接收的变量,并在构造函数里初始化
class SecondState {
  SecondState({required this.phoneNumber, required this.password});
  RxString phoneNumber = ''.obs;
  RxString password = ''.obs;
}
///second_controller
///利用构造函数初始化state,这样binding就需要传入state
class SecondController extends GetxController {
  SecondController({required this.state});
  SecondState state;
}
///second_binding
///在binding注入时接收从First页面传过来的state
class SecondBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut<SecondController>(
      () => SecondController(state: Get.arguments),
    );
  }
}
///first_state
///定义controller,获取输入框的text
class FirstState {
  TextEditingController phoneController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
}

///first_controller
///把SecondState的phoneNumber和password变量赋值并传过Second页面
class FirstController extends GetxController {
  FirstState state = FirstState();
///跳转方法
  void onSubmit() {
    Get.toNamed(Routes.SECOND,
        arguments: SecondState(
            phoneNumber: state.phoneController.text.obs,
            password: state.passwordController.text.obs));
  }
}
///second_view
///用Obx包裹,显示从First页面传过来的值
Center(
    child: Obx(() => Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
            Text(
                '手机号码:${controller.state.phoneNumber.value}\n 密码:${controller.state.password.value}')
        ],
    )),
)

2:第二种方法

First页面直接传state到Second页面,Second页面接收First页面传过来的state,并赋值给Second页面state的变量。

///first_state
///定义controller,获取输入框的text
class FirstState {
  TextEditingController phoneController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
}
///first_controller
///直接把state传给Second页面
class FirstController extends GetxController {
  FirstState state = FirstState();
  void onSubmit() {
    Get.toNamed(Routes.SECOND, arguments: state);
  }
}
///second_state
///定义变量接收First页面传过来的值
class SecondState {
  RxString phoneNumber = ''.obs;
  RxString password = ''.obs;
}
///second_controller
///定义方法接收First传来的值并赋值给state的变量
class SecondController extends GetxController {
    SecondState state = SecondState();
    
///定义方法接收传来的值
    void fetchData() {
        final FirstState result = Get.arguments;
        state.phoneNumber.value = result.phoneController.text;
        state.password.value = result.passwordController.text;
    }
    
///在onReady里初始化方法
    @override
    void onReady() {
        super.onReady();
        fetchData();
    }

}
///secondd_view
///用Obx包裹,显示传来的值
Center(
    child: Obx(() => Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
            Text(
                '手机号码:${controller.state.phoneNumber.value}\n 密码:${controller.state.password.value}')
        ],
    )),
)

返回路由Second --> First

直接把Second的state传回去,First定义变量接收就行。

///second_controller
///定义返回到First页面方法,把Second页面的state返回
class SecondController extends GetxController {
  SecondState state = SecondState();
  void backFirst() {
    Get.back(result: state);
  }
}
///first_state
///定义两个变量接收Second页面返回的值
class FirstState {
  TextEditingController phoneController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
    
  RxString phoneNumber = ''.obs;
  RxString password = ''.obs;
}

///first_controller
///First跳转到Second页面时,等待Second页面返回的值,接收到值后赋值给自身的state里的变量
class FirstController extends GetxController {
  FirstState state = FirstState();
  Future onSubmit() async {
    final SecondState result =
        await Get.toNamed(Routes.SECOND, arguments: state);
    state.phoneNumber.value = result.phoneNumber.value;
    state.password.value = result.password.value;
  }
}
///first_view
///显示从Second页面返回的值
Padding(
    padding: const EdgeInsets.all(8.0),
    child: Obx(() => Text(
        '从Second页面返回的值\n手机号码:${controller.state.phoneNumber.value}\n密码:${controller.state.password.value}')),
),
posted @ 2022-03-27 15:04  漫游者杰特  阅读(768)  评论(0编辑  收藏  举报