Flutter 使用 Getx 关闭选定的对话框

标签 flutter flutter-getx

我使用 flutter 有一段时间了,最​​近使用 Get 来实现状态管理。
我在打开加载对话框 1 和消息对话框时遇到问题。然后我想关闭加载对话框,但消息对话框是保持关闭的对话框。

import 'package:flutter/material.dart';
import 'package:get/get.dart';

class HomeController extends GetxController {

  Future<void> openAndCloseLoadingDialog() async {
    showDialog(
      context: Get.overlayContext,
      barrierDismissible: false,
      builder: (_) => WillPopScope(
        onWillPop: () async => false,
        child: Center(
          child: SizedBox(
            width: 60,
            height: 60,
            child: CircularProgressIndicator(
              strokeWidth: 10,
            ),
          ),
        ),
      ),
    );

    await Future.delayed(Duration(seconds: 3));

    Get.dialog(
      AlertDialog(
        title: Text("This should not be closed automatically"),
        content: Text("This should not be closed automatically"),
        actions: <Widget>[
          FlatButton(
            child: Text("CLOSE"),
            onPressed: () {
              Get.back();
            },
          )
        ],
      ),
      barrierDismissible: false,
    );

    await Future.delayed(Duration(seconds: 3));

    Navigator.of(Get.overlayContext).pop();
  }
}
上面的代码关闭了第二个对话框,而不是我想要的第一个对话框。
任何人都可以就这个问题提供建议。

最佳答案

AlertDialog的原因正在被解雇而不是 CircularProgressIndicator是因为 AlertDialog位于堆栈的顶部。您可以在这里拨打 Navigator.of(Get.overlayContext).pop();解雇 CircularProgressIndicator在显示 AlertDialog 之前.
Demo
基于提供的片段的示例代码。

import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  final HomeController c = Get.put(HomeController());

  void _incrementCounter() {
    c.openAndCloseLoadingDialog();
    // setState(() {
    //   _counter++;
    // });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class HomeController extends GetxController {

  Future<void> openAndCloseLoadingDialog() async {
    showDialog(
      context: Get.overlayContext,
      barrierDismissible: false,
      builder: (_) => WillPopScope(
        onWillPop: () async => false,
        child: Center(
          child: SizedBox(
            width: 60,
            height: 60,
            child: CircularProgressIndicator(
              strokeWidth: 10,
            ),
          ),
        ),
      ),
    );

    await Future.delayed(Duration(seconds: 3));
    // Dismiss CircularProgressIndicator
    Navigator.of(Get.overlayContext).pop();

    Get.dialog(
      AlertDialog(
        title: Text("This should not be closed automatically"),
        content: Text("This should not be closed automatically"),
        actions: <Widget>[
          FlatButton(
            child: Text("CLOSE"),
            onPressed: () {
              Get.back();
            },
          )
        ],
      ),
      barrierDismissible: false,
    );

    // await Future.delayed(Duration(seconds: 3));
    // Navigator.of(Get.overlayContext).pop();
  }
}

关于Flutter 使用 Getx 关闭选定的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63841151/

相关文章:

flutter - 在Dart中显示小部件的内置列表

Flutter 用短信打开 whatsapp

Flutter:如何使用 getX 更改文本颜色?

flutter - 避免 ListView 不必要的刷新

flutter - 当此属性更改时,如何对显示对象属性的文本小部件进行 react 性更新?这与 GetX

flutter - 如何在flutter中使用GetX和Get Connect解决ssl证书错误

flutter - 如何在 flutter web 中支持 HTTP header NetworkImage

internationalization - 如何将国际化对象传递给 Flutter 中的子控件

flutter - 使用提供程序全局更改字体大小

Flutter GetX RxList 分配问题