0

I created two widgets and I want to change between this two in the backPress like what I do in the text field but it doesn't work and I didn't get any debug print in onWillPop. but I have a navigation button and it connect to mainScreen page and I can change backPress in this page for all the pages but I can't do the same thing in the pages:

1

I want to change onWillPopScope in the pages not in MainScreen

**//MY Page (SearchScreen)**
bool change = true;

Future<bool> _FuturePop() async {
  if (onText) {
    FocusScope.of(context).unfocus();
    debugPrint("if onText => $onText");
    onText = false;
    return false;
  } else {
    debugPrint("else onText => $onText");
    return true;
  }

  // return false;
}

@override
Widget build(BuildContext context) {
 
  debugPrint("onText => $onText");
  return WillPopScope(
    onWillPop: _FuturePop,
    child: Scaffold(
      backgroundColor: const Color(0xffEFF4F3),
      body: SafeArea(
        child: Column(
          children: [
            //topBar
            const SpacerEmpty(),

            Padding(
              padding: const EdgeInsets.fromLTRB(12, 0, 12, 0),
              child: Stack(
                children: [
                  SizedBox(
                    height: 36,
                    child: Flexible(
                        child: TextField(
                      onTap: () {
                        setState(() {
                          onText = true;
                          debugPrint("onTap => $onText");
                        });
                      },
                      textInputAction: TextInputAction.done,
                      onEditingComplete: () {
                        onText = false;
                        debugPrint("onEdCpm => $onText");
                        FocusScope.of(context).unfocus();
                      },
                      decoration: const InputDecoration(
                          fillColor: Colors.white,
                          floatingLabelBehavior: FloatingLabelBehavior.never,
                          contentPadding:
                              EdgeInsets.only(bottom: 8, right: 8),
                          border: OutlineInputBorder(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(12)),
                              borderSide: BorderSide(
                                  width: .5, color: Color(0xaaacacac)))),
                    )),
                  ),
                  const Positioned(
                      left: 8,
                      top: 0,
                      bottom: 0,
                      child: Icon(CupertinoIcons.search)),
                ],
              ),
            ),
            ...

//My MainScreen Page

Future<bool> _onWillPop() async {
  // if (_homeKey.currentState!.canPop()){
  //   _homeKey.currentState!.pop() ;
  // }


  if (_scaffoldKey.currentState!.isDrawerOpen) {
    _scaffoldKey.currentState!.closeDrawer();
    return false;
  } else if (selectedScreenIndex == homeIndex) {
    if (doubleTapedToExit) {
      return true;
    } else {
      doubleTapedToExit = true;

      ShowToast(message: 'برای خروج از برنامه دو بار دکمه بازگشت را بزنید.');

      Future.delayed(const Duration(seconds: 2)).then((value) {
        doubleTapedToExit = false;
      });
      return false;
    }
  }

  final NavigatorState currentSelectedTabNavigatorState =
      map[selectedScreenIndex]!.currentState!;
  if (currentSelectedTabNavigatorState.canPop()) {
    currentSelectedTabNavigatorState.pop();
    return false;
  } else if (_history.isNotEmpty) {
    setState(() {
      selectedScreenIndex = homeIndex;
      _history.clear();
    });
    return false;
  }

  return true;
}

@override
Widget build(BuildContext context) {
  return WillPopScope(
    onWillPop: _onWillPop,
    child: SafeArea(
      child: Scaffold(
        key: _scaffoldKey,
        resizeToAvoidBottomInset: false,
        drawer: Drawer(
          child: DrawerWidget(
            bottomNavClick: (int index) {
              setState(() {
                _history.remove(selectedScreenIndex);
                _history.add(selectedScreenIndex);
                selectedScreenIndex = index;
              });
            },
            closeDrawer: () {
              _scaffoldKey.currentState!.closeDrawer();
            },
          ),
        ),
        body: Stack(
          children: [
            Positioned.fill(
              bottom: 0,
              child: IndexedStack(
                index: selectedScreenIndex,
                children: [
                  _navigator(_homeKey, homeIndex, const HomeScreen()),
                  _navigator(_searchKey, searchIndex, const SearchScreen()),
                  _navigator(_addEstateKey, plusIndex, Container()),
                  _navigator(
                      _favoritesKey, favoriteIndex, const FavoriteScreen()),
                  _navigator(
                      _myEstateKey, myEstateIndex, const MyEstateScreen()),
                ],
              ),
            ),
            const Positioned(top: 12, right: 12, left: 12, child: _Toolbar()),
            Positioned(
              bottom: 12,
              right: 12,
              left: 12,
              child: _BottomNavigation(
                onTap: (int index) {
                  setState(() {
                    _history.remove(selectedScreenIndex);
                    _history.add(selectedScreenIndex);
                    selectedScreenIndex = index;
                  });
                },
                selectedIndex: selectedScreenIndex,
              ),
            ),
          ],
        ),
      ),
    ),
  );
}
Zahra
  • 2,231
  • 3
  • 21
  • 41
Kianoosh
  • 1
  • 1

1 Answers1

0

I guess you want the user to tap the back key twice to exit the app while they are on home screen.

You can see the sample code below.

  class MyWidget extends StatefulWidget {
    const MyWidget({super.key});

    @override
    State<MyWidget> createState() => _MyWidgetState();
  }

  class _MyWidgetState extends State<MyWidget> {
    bool willPopScope = false;
    @override
    Widget build(BuildContext context) {
      return WillPopScope(
        onWillPop: _onWillPop,
        child: Container(),
      );
    }

    Future<bool> _onWillPop() async {
      if (willPopScope) {
        return true;
      } else {
        Fluttertoast.showToast(
          msg: "Press back button again to exit",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
          timeInSecForIosWeb: 1,
        );

        setState(() {
          willPopScope = true;
        });
        Future.delayed(const Duration(milliseconds: 1200), () {
          setState(() {
            willPopScope = false;
          });
        });
        return false;
      }
    }
  }

You can change duration to any number according to your case.