【问题标题】:How to check Alert Dialog is open only one times instead of multiple new dialog box after onTap in flutterflutter onTap 后如何检查警报对话框只打开一次而不是打开多个新对话框
【发布时间】:2022-11-25 12:24:48
【问题描述】:

我正在处理我的 flutter 应用程序,我想检查屏幕上的警报对话框是否打开。谁能告诉我该怎么做,现在每次我按 ontap 时都会出现一个新对话框。我怎样才能只出现一个对话框而不是多个新对话框?

我已经尝试 bool,ontap 取消所有不工作。

Future? _dialog;

Future<void> _checkTimer() async {

  if (_dialog == null) {
    _dialog =  await Future.delayed(Duration(seconds: 5));

    showTimer(context);
    await _dialog;
    _dialog = null;
  } else {
    //do nothing
  }


}
showTimer(BuildContext context) {
  // set up the buttons
  // ignore: deprecated_member_use

  if (didUserTouchedScreen = true){
    Container alert = Container(child: _imageslideshowProductDetailstimer());
    // show the dialog
    showDialog(
      barrierDismissible: true,
      context: context,
      builder: (BuildContext context) {
        return WillPopScope(
            onWillPop: () async {

              didUserTouchedScreen = false;
              // _checkTimer();
              return true;
            },
            child: alert);
      },
    ).then((_) => didUserTouchedScreen = false);
  }}
behavior: HitTestBehavior.translucent,
onTapDown: (tapdown) {
  print("down");

_checkTimer();

},
onTapCancel: (){print('up');_checkTimer();}

【问题讨论】:

    标签: flutter dialog


    【解决方案1】:

    您可以使用布尔状态来实现这一点,我们称它为isButtonActive。根据此状态的值启用/禁用按钮。按下按钮时,将状态设置为假,当对话框关闭时,将状态设置为真。

    下面是一个示例代码:

    class _HomePageState extends State<HomePage> {
      bool isButtonActive = true;
    
      showTimer(BuildContext context) async {
        setState(() {
          isButtonActive = false;
        });
    
        await Future.delayed(Duration(seconds: 2));
    
        showDialog(
          context: context,
          builder: (BuildContext context) {
            return Column(
              children: const [
                Text('qwerty'),
              ],
            );
          },
        ).then((value) {
          setState(() {
            isButtonActive = true;
          });
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('총톤수'),
          ),
          body: Center(
            child: ElevatedButton(
              onPressed: isButtonActive ? () => showTimer(context) : null,
              child: const Text('총톤수'),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 2019-09-02
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多