Home > Back-end >  How can we dismiss an Alert dialogue without ontap or any user interaction.?
How can we dismiss an Alert dialogue without ontap or any user interaction.?

Time:03-14

I need to dissmiss an alert dialgue when can callback is been called. How can i achieve it.

CodePudding user response:

One way is you can add timer for it.

Try this code:-

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

void main() {
  runApp(GetMaterialApp(title: 'Flutter', home: Flutter()));
}

class Flutter extends StatefulWidget {
  const Flutter({Key? key}) : super(key: key);

  @override
  State<Flutter> createState() => _FlutterState();
}

class _FlutterState extends State<Flutter> {
  int seconds = 3;
  bool visible = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter'),
        centerTitle: true,
      ),
      body: Center(
          child: ElevatedButton(
        child: Text('Open Alert'),
        onPressed: () {
          RemoveAlert();
          visible = true;
          Get.defaultDialog(
              title: 'Alert',
              middleText: 'Alert will go in ${seconds} seconds').then((value){
                visible = false;
              });
        },
      )),
    );
  }

  void RemoveAlert() {
    Future.delayed(Duration(seconds: seconds)).then((value){
      if(visible){
        Get.back();
        print('removed');
      }else{
        print('removed already');
      }
    });
  }
}

Note: I have used get package here.

CodePudding user response:

Yes I got the solution after a long time effort. I called Navigator.pop(context) two time. Then it worked for me.

  • Related