首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GetX -如何从Get.Dialog返回值?

GetX -如何从Get.Dialog返回值?
EN

Stack Overflow用户
提问于 2020-11-27 10:30:53
回答 1查看 8.2K关注 0票数 2

如何从打开Dialog Widget / AlertDialog的Get.Dialog获取返回值?

EN

回答 1

Stack Overflow用户

发布于 2020-11-27 10:30:53

1. GetMaterialApp

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

代码语言:javascript
运行
复制
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作为应用程序的根,当调用任何导航方法时,您将看到一个(令人困惑的)错误:

代码语言:javascript
运行
复制
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.

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

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

代码语言:javascript
运行
复制
onPressed: () async {
  // assign return value to an observable
  return lx.result.value = await Get.dialog(

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

代码语言:javascript
运行
复制
onPressed: () => Get.back(result: true),

完整示例:

代码语言:javascript
运行
复制
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),
                              ),
                            ],
                          )
                      )
                  );},
              ),
            )
          ],
        ),
      ),
    );
  }
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65031381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档