【问题标题】:GetX - How to return a value from Get.Dialog?GetX - 如何从 Get.Dialog 返回一个值?
【发布时间】:2021-03-09 21:14:11
【问题描述】:

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

【问题讨论】:

    标签: flutter flutter-getx


    【解决方案1】:

    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.
    

    2。 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),
    

    完整示例:

    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),
                                  ),
                                ],
                              )
                          )
                      );},
                  ),
                )
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 我试过这种方式,但对话框又打开了。
    猜你喜欢
    • 2013-06-18
    • 2018-11-23
    • 1970-01-01
    • 2015-07-12
    • 2010-11-30
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多