Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何从Get.Dialog返回值?_Flutter_Getx - Fatal编程技术网

Flutter 如何从Get.Dialog返回值?

Flutter 如何从Get.Dialog返回值?,flutter,getx,Flutter,Getx,如何从打开对话框小部件/AlertDialog的get.对话框中获取返回值?1。GetMaterialApp 在main.dart中,确保MyApp返回的是GetMaterialApp而不是MaterialApp class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) {

如何从打开对话框小部件/AlertDialog的get.对话框中获取返回值?

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'),
    );
  }
}
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(结果:X)
关闭对话框时返回,其中
X
是通过
Get.dialog
返回的动态值:

onPressed: () => Get.back(result: true),
完整示例:
我尝试了这种方式,但对话框再次打开。
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),
                              ),
                            ],
                          )
                      )
                  );},
              ),
            )
          ],
        ),
      ),
    );
  }
}