How to prevent exiting if other pages loaded in home page

30 Views Asked by At

I used a bottom navigation bar that exit from app in home page after double press but I have an issue in home page.

If I changed the home screen body to productlist page and I press back i didn't get the home page instead I get the exit message from snackbar. How to I get this function once I press back from the product list page i need to get back to home page instead of getting the message and after I get to the home page I should follow the rules in the bottom bar (double press to exit).

BottomBar:

class _BottomBarState extends State<BottomBar> {

  final List<Widget> _pages = const [
    HomePage(),
    CartPage(),
    SubscriptionList(),
    ProfilePage(),
  ];

  @override
  Widget build(BuildContext context) {
    
    return PopScope(
      canPop: false,
      onPopInvoked: (didPop) async {
        if(widget.selectedIndex != 0 ){
          setState(() {
            widget.selectedIndex = 0;
          });
          return;
        } else {
          final now = DateTime.now();
          if(currentPress == null || now.difference(currentPress!) > const Duration(seconds: 2)){
            currentPress = now;
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                elevation: 1,
                behavior: SnackBarBehavior.floating,
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)),
                backgroundColor:  const Color(0xFF60B47B),
                content: const Text('Press back again to exit', style: TextStyle(color: Colors.white, fontWeight: FontWeight.w600),),
                duration: const Duration(seconds: 1),
              ),
            );
            return;
          } else {
            SystemNavigator.pop();
          }
        }
      },
      child: Scaffold(
        backgroundColor: Colors.white,
        body: Stack(
          children: [
            // Your main content goes here
            Positioned.fill(
              child: PageStorage(
                bucket: PageStorageBucket(),
                child: _pages[widget.selectedIndex],
              ),
            ),
            Positioned(
              left: 0,
              right: 0,
              bottom: 0,
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(20.0), // Adjust the corner radius
                    topRight: Radius.circular(20.0),
                  ),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.2),
                      spreadRadius: 2,
                      blurRadius: 1,
                      offset: const Offset(0, 2),
                    ),
                  ],
                ),
                child: BottomNavigationBar(
                  
                  items: List.generate(4, (index) {
                    return BottomNavigationBarItem(
                      icon: Icon(
                        _selectedIcons[index],
                        color: widget.selectedIndex == index ? Colors.blue : Colors.grey.shade500,
                        size: 28.0,
                      ),
                      label: _getLabel(index),
                    );
                  }),
                  type: BottomNavigationBarType.fixed,
                  onTap: (value) {
                    setState(() {
                      widget.selectedIndex = value;
                    });
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

}

HomePage:

class _HomePageState extends State<HomePage> {
  bool isClicked = false;
  @override
  Widget build(BuildContext context) {
    if(!isClicked){
      return homePage() 
    }else {
      return ProductPage()
    }
  }
  widget homePage(){
    return ElevatedButton(onPressed: (){
       setState((){isClicked = !isClicked})
     })
  }
}
class ProductListPage extends StatefulWidget {
  const ProductListPage({super.key});

  @override
  State<ProductListPage> createState() => _ProductListPageState();
}

class _ProductListPageState extends State<ProductListPage> {
   @override
   widget build(Buildcontext context){
     //return List of Product 
   }
}
0

There are 0 best solutions below