GetX -从Get.Dialog返回值

  1. GetMaterialApp

在main.dart中,确保MyApp返回的是GetMaterialApp而不是MaterialApp

class MyApp extends StatelessWidget {

// This widget is the root of your application.

@override

Widget build(BuildContext context) {

return GetMaterialApp( // <-- Use GetMaterialApp

title: 'Flutter Demo',

home: MyHomePage(title: 'Flutter Examples'),

);

}

}

这允许Get处理导航/路由,使导航方法可用,例如:Get.to()、Get.dialog()、Get.back()等。如果没有GetMaterialApp作为应用程序的根,当调用任何导航方法时,您将看到一个(令人困惑的)错误:

E/flutter (11139): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception:

'package:flutter/src/widgets/localizations.dart': Failed assertion:

line 453 pos 12: 'context != null': is not true.

复制

  1. Get.dialog +Get.back(结果: X)

让你对Get.dialog的调用期望一个异步返回值...

onPressed: () async {

// assign return value to an observable

return lx.result.value = await Get.dialog(

...。在使用Get.back(result: X)关闭对话框时返回,其中X是通过Get.dialog返回的动态值

onPressed: () => Get.back(result: true),

复制

完整示例:

dart 复制代码
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class LoginX extends GetxController {
  RxBool result = false.obs;
}

class GetDialogReturnPage extends StatelessWidget {
  final LoginX lx = Get.put(LoginX());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GetDialog Return Example'),
      ),
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Container(
              child: Obx(() => Text('Value shows here: ${lx.result.value}')),
            ),
            Container(
              alignment: Alignment.center,
              child: RaisedButton(
                child: Text('Login'),
                onPressed: () async {
                  // ** assign return value to an observable **
                  return lx.result.value = await Get.dialog(
                      AlertDialog(
                          content: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              RaisedButton(
                                child: Text('Good Login'),
                                onPressed: () => Get.back(result: true),
                                // ** result: returns this value up the call stack **
                              ),
                              SizedBox(width: 5,),
                              RaisedButton(
                                child: Text('Bad Login'),
                                onPressed: () => Get.back(result: false),
                              ),
                            ],
                          )
                      )
                  );},
              ),
            )
          ],
        ),
      ),
    );
  }
}

我的例子参考:

dart 复制代码
  Future<bool?> showRemoveDialog(

      String title, String content, bool showCheckbox) async {

      debugPrint(title);

      return await Get.defaultDialog(
        title: title,
        content: Text(content),
        onConfirm: ()=>Get.back(result: true),
        onCancel: ()=>Get.back(result: false),
      );
    }
相关推荐
程序员阿鹏7 分钟前
Git的安装和配置(idea中配置Git)
java·开发语言·ide·git·intellij-idea·idea
m0_513962538 分钟前
vue-ganttastic甘特图label标签横向滚动固定方法
javascript·vue.js·甘特图
景天科技苑19 分钟前
【Rust trait特质】如何在Rust中使用trait特质,全面解析与应用实战
开发语言·后端·rust·trait·rust trait·rust特质
PacosonSWJTU22 分钟前
python使用matplotlib画图
开发语言·python·matplotlib
Inverse16228 分钟前
C语言_自定义类型:结构体
c语言·开发语言·算法
奔跑吧 android35 分钟前
【android bluetooth 协议分析 12】【A2DP详解 1】【车机侧蓝牙音乐免切源介绍】
android·bluetooth·bt·gd·a2dpsink·免切源·aosp14
enyp8037 分钟前
Qt原型模式实现与应用
开发语言·qt·原型模式
码农黛兮_461 小时前
HTML、CSS 和 JavaScript 基础知识点
javascript·css·html
CryptoRzz1 小时前
印度尼西亚数据源对接技术指南
开发语言·python·websocket·金融·区块链
zyx没烦恼1 小时前
unordered_map和unordered的介绍和使用
开发语言·c++