美文网首页
跳转传参

跳转传参

作者: xieyinghao | 来源:发表于2023-08-08 09:38 被阅读0次

上一级 的 UI

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:get/instance_manager.dart';
import 'package:statedemo/pages/home/controller.dart';
import 'package:statedemo/pages/list/index.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key, required this.title});
  final String title;
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        backgroundColor: Colors.yellow,
      ),
      body: GetBuilder(
        builder: (controller) {
          return _getStatusWidget(controller);
        },
        init: HomePageController(),
      ),
    );
  }

  Widget _getStatusWidget(HomePageController controller) {
    return Container(
      child: GestureDetector(
        onTap: () {
          controller.oncount();
          print('hahahah');
          _pushListPage();
        },
        child: Obx(
          // (() => Text(controller.num.toString())),
          (() => Text("GetX响应:${controller.num.toString()}")),
        ),
      ),
    );
  }

  void _pushListPage() async {
    print('准备跳转');
    dynamic result = await Get.to(() => ListPage(), arguments: "这个是上一个界面的参数");

    if (result is String) {
      print("这是返回的值$result");
    }
  }
}

上一级业务

import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';

class HomePageController extends GetxController {
  final RxInt _num = 0.obs;
  int get num => _num.value;

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

extension countClick on HomePageController {
  void oncount() async {
    _num.value++;
  }
}

下一级参数处理

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:get/get.dart';
import 'package:get/instance_manager.dart';

class ListPage extends StatefulWidget {
  const ListPage({super.key});

  @override
  State<ListPage> createState() => _ListPageState();
}

class _ListPageState extends State<ListPage> {
  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    dynamic res = Get.arguments;
    if (res is String) {
      _controller.text = res;
      print("拿到了上一个页面的参数是:${res}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          children: [
            Text(Get.arguments),
            TextField(
              controller: _controller,
              onChanged: (value) {},
            ),
            GestureDetector(
              onTap: () => _onPop(),
              child: Text("返回"),
            )
          ],
        ),
      ),
    );
  }

  void _onPop() async {
    Get.back(result: "这是返回的参数");
  }
}

相关文章

网友评论

      本文标题:跳转传参

      本文链接:https://www.haomeiwen.com/subject/yeenpdtx.html