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:
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,
),
),
],
),
),
),
);
}