skip to Main Content

I have used Get.defaultDialog() to display a dialog in a Flutter app.

Now, I want to close the dialog when the user clicks the cancel button using Get.back(), but it’s not working, and I don’t know why."

I’m using

get: ^4.6.5

This is my code:

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

class MainScreen extends StatelessWidget {
  const MainScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: SizedBox(
                width: double.infinity,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    MaterialButton(
                        child: Text('show dialog'),
                        onPressed: (){
                          Get.defaultDialog(
                            title: 'Dialog Title',
                            titleStyle: const TextStyle(
                              color: Colors.white,
                            ),
                            middleText: 'Dialog content',
                            middleTextStyle: const TextStyle(color: Colors.lightGreen),
                            backgroundColor: Colors.black,
                            radius: 8,
                            textCancel: 'cancel',
                            cancelTextColor: Colors.white,
                            textConfirm: 'OK',
                            confirmTextColor: Colors.white,
                            onCancel: (){
                              Get.back();
                              print('cancel clicked....');
                            },
                            onConfirm: () {
                              print('ok clicked...');
                            },
                          );
                    })
                  ]
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

2

Answers


  1. You can use Get.back(closeOverlays: true);

    Or you can use Navigator.pop() passing the context from GetX, using overlayContext from Get

    Login or Signup to reply.
  2. you can use this code
     int navigation= 0;
                                  Navigator.of(context).popUntil((_) => navigation++ >= 0);
                                  kPrint('cancel clicked....');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search