'flutter show dialog Will not let me go back

I build a show dialog for my flutter pdfViewer , and When a user selects a word, he see similar words on show dialog and then when user choose the word in the second show dialog can see details in the last when user want to go to the previous page he/she have to push the back button The same number as used to show dialog for example if user see the details of a word he/she have to two time press the back button for navigation ,and i don't know how can fixed it ,

with google translate

it's my pdf view code

enter code here        

            

    import 'dart:io';

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:flutter_bloc/flutter_bloc.dart';
    import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';

    import '../cubit/dictionary_cubit/dictionary_cubit.dart';
    import '../widgets/showSearchScreenDialog.dart';

    class pdfView extends StatefulWidget {
      //
      // final String directoryLink;
      //
      const pdfView({
        Key? key,
      }) : super(key: key);

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

    class _pdfView extends State<pdfView> {
      late PdfViewerController _pdfViewerController = PdfViewerController();
      final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();
      late bool _showScrollHead;
      OverlayEntry? _overlayEntry;
      PdfTextSearchResult? _searchResult;

      @override
      void initState() {
        _showScrollHead = true;
        _pdfViewerController = PdfViewerController();
        super.initState();
      }

      /// Ensure the entry history of text search.

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(actions: [
            // IconButton(
            //   icon: Icon(
            //     Icons.bookmark_border,
            //     color: Colors.black,
            //   ),
            //   onPressed: () {
            //     _pdfViewerKey.jumpToBookmark(_pdfBookmark);
            //   },
            // ),
          ], automaticallyImplyLeading: false, backgroundColor: Color(0xFFFAFAFA)),
          body: Stack(
            children: [
              SfPdfViewer.asset(
                'assets/pdf/68.pdf',
                key: _pdfViewerKey,
                controller: _pdfViewerController,
                canShowScrollHead: _showScrollHead,
                onTextSelectionChanged: (PdfTextSelectionChangedDetails details) {
                  if (details.selectedText == null && _overlayEntry != null) {
                    _overlayEntry!.remove();
                    _overlayEntry = null;
                  } else if (details.selectedText != null &&
                      _overlayEntry == null) {
                    _showContextMenu(details);
                  }
                },
              ),
              Visibility(
                visible: false,
                child: Align(
                  alignment: Alignment.center,
                  child: Flex(
                    direction: Axis.horizontal,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        padding: const EdgeInsets.only(
                            left: 15, top: 7, right: 15, bottom: 7),
                        decoration: BoxDecoration(
                          color: Colors.grey[600],
                          borderRadius: const BorderRadius.all(
                            Radius.circular(16.0),
                          ),
                        ),
                        // child: Mtoast(),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      }

      void _showContextMenu(PdfTextSelectionChangedDetails details) {
        final OverlayState _overlayState = Overlay.of(context)!;

        _overlayEntry = OverlayEntry(
          builder: (context) => Positioned(
              top: details.globalSelectedRegion!.center.dy - 55,
              left: details.globalSelectedRegion!.bottomLeft.dx,
              child: Row(
                children: [
                  ElevatedButton(
                    onPressed: () {
                      Clipboard.setData(ClipboardData(text: details.selectedText));
                      print('Text copied to clipboard: ' +
                          details.selectedText.toString());
                      _pdfViewerController.clearSelection();
                    },
                    child: Text('Copy', style: TextStyle(fontSize: 17)),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      Clipboard.setData(ClipboardData(text: details.selectedText));
                      _pdfViewerController.clearSelection();
                      Cubitcontroller.clear();
                      Cubitcontroller.text = details.selectedText!;
                      BlocProvider.of<DictionaryCubit>(context).seter();
                      SohwSearchscreenDialog(context);
                    },
                    child: const Text('Translate', style: TextStyle(fontSize: 17)),
                  ),
                ],
              )),
        );
        _overlayState.insert(_overlayEntry!);
      }
    }

and first show dialog

enter code here

    import 'package:flutter/material.dart';
    import 'package:flutter_application_1/Logics/responsive.dart';
    import 'package:flutter_application_1/widgets/textFiled.dart';
    import 'package:flutter_bloc/flutter_bloc.dart';

    import '../../Logics/models/database_helper.dart';
    import '../../Logics/models/database_model.dart';
    import '../cubit/dictionary_cubit/dictionary_cubit.dart';
    import 'ListOfWords.dart';

    final dbService = DatabaseService();
    Future SohwSearchscreenDialog(context) {
      return showDialog(
        context: context,
        builder: (context) => AlertDialog(
          content: BlocBuilder<DictionaryCubit, DictionaryCubitState>(
            builder: (context, state) {
              return FutureBuilder<List<Words>>(
                  future: dbService.getTrails(state.keyWord.toString()),
                  builder: (context, snapshot) {
                    if (!snapshot.hasData) {
                      return const Center(
                        child: CircularProgressIndicator(),
                      );
                    }
                    return SingleChildScrollView(
                      child: Column(
                        children: [
                          const textfeild(hintText: "Edit"),
                          const SizedBox(
                            height: 10,
                          ),
                          Container(
                            color: Colors.red,
                            width: 300,
                            height: Responsive.isMobile(context)
                                ? MediaQuery.of(context).size.height * 0.6
                                : MediaQuery.of(context).size.height * 0.3,
                            child: listOfWords(),
                          ),
                          const SizedBox(
                            height: 7.5,
                          ),
                          IconButton(
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                            icon: const Icon(
                              Icons.close,
                              color: Colors.redAccent,
                            ),
                          )
                        ],
                      ),
                    );
                  });
            },
          ),
        ),
      );
    }

in the last details dialog

enter code here
    import 'package:flutter/material.dart';

    import '../screens/deitails.dart';

    Future SohwDialog(context) => showDialog(
          context: context,
          builder: (context) => AlertDialog(
            content: DetailsScreen(),
            actions: [
              IconButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                icon: const Icon(
                  Icons.close,
                  color: Colors.redAccent,
                ),
              )
            ],
          ),
        );

enter image description here



Solution 1:[1]

I think you have context issue. When user click to open secound dialog you should pop first dialog then you should open secound dailog. For pop first dailog you should use :-

Navigator.pop(firstDialogContext);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harsh Sureja