Flutter: Drawer items reopen after being in an open window already

32 Views Asked by At

The problem is that I am utilizing Flutter to design an application, and I have made a custom drawer and app bar. If an item in a drawer is clicked, it should open; but, if you click on item 1 again, it opens again. It shouldn't do that. Similarly, if a tab or screen is opened, it shouldn't reopen that screen; it should just close the drawer.

Padding(
                padding: const EdgeInsets.only(left: 25.0),
                child: ListTile(
                  title: Text(
                    "H O M E",
                    style: GoogleFonts.playfairDisplay(
                      color: Colors.black,
                      // fontWeight: FontWeight.bold,
                    ),
                  ),
                  leading: const Icon(Icons.home_rounded),
                  onTap: () {
                    // Check if the current page is an instance of HomePage
                    if (ModalRoute.of(context)?.settings.arguments
                        is HomePage) {
                      // If it is, just close the drawer
                      Navigator.pop(context);
                    } else {
                      // If it's not, close the drawer and navigate to the Home Page
                      Navigator.pop(context);
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const HomePage(),
                        ),
                      );
                    }
                  },
                ),
              ),

Here is my logic that I have applied in the drawer.

Here is the full Drawer Code:

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:lottie/lottie.dart';
import 'package:simplethread/src/auth/auth_service.dart';
import 'package:simplethread/src/screens/home.dart';
import 'package:simplethread/src/screens/setting_page.dart';

class MyDrawer extends StatelessWidget {
  const MyDrawer({super.key});
  final String _animation = 'assets/animation/login_message.json';

  //logout from the application
  void logout() {
    final _auth = AuthService();
    _auth.signOut();
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      backgroundColor: Theme.of(context).colorScheme.background,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Column(
            children: [
              //Logo
              const SizedBox(
                height: 60,
              ),
              Lottie.asset(
                _animation,
                fit: BoxFit.fill,
                width: 100,
                height: 100,
              ),
              Text(
                'Simple Thread',
                style: GoogleFonts.playfairDisplay(
                  textStyle: const TextStyle(
                    letterSpacing: .5,
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                    color: Colors.black,
                  ),
                ),
              ),
              const SizedBox(
                height: 25,
              ),

              //version-03
              Padding(
                padding: const EdgeInsets.only(left: 25.0),
                child: ListTile(
                  title: Text(
                    "H O M E",
                    style: GoogleFonts.playfairDisplay(
                      color: Colors.black,
                      // fontWeight: FontWeight.bold,
                    ),
                  ),
                  leading: const Icon(Icons.home_rounded),
                  onTap: () {
                    // Check if the current page is an instance of HomePage
                    if (ModalRoute.of(context)?.settings.arguments
                        is HomePage) {
                      // If it is, just close the drawer
                      Navigator.pop(context);
                    } else {
                      // If it's not, close the drawer and navigate to the Home Page
                      Navigator.pop(context);
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const HomePage(),
                        ),
                      );
                    }
                  },
                ),
              ),

              //Setting Page Item
              Padding(
                padding: const EdgeInsets.only(left: 25.0),
                child: ListTile(
                  title: Text(
                    "S E T T I N G S",
                    style: GoogleFonts.playfairDisplay(
                      color: Colors.black,
                      // fontWeight: FontWeight.bold,
                    ),
                  ),
                  leading: const Icon(Icons.settings_rounded),
                  onTap: () => {
                    //Closing the drawer and opening the Setting Page
                    Navigator.pop(context),

                    //Opening the Setting Page
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => const SettingPage(),
                      ),
                    ),
                  },
                ),
              ),
            ],
          ),
          Padding(
            padding: const EdgeInsets.only(left: 25.0, bottom: 25.0),
            child: ListTile(
              title: Text(
                "L O G O U T",
                style: GoogleFonts.playfairDisplay(
                  color: Colors.black,
                  // fontWeight: FontWeight.bold,
                ),
              ),
              leading: const Icon(Icons.logout_rounded),
              onTap: logout,
            ),
          ),
        ],
      ),
    );
  }
}

Here is my Home Screen:

import 'package:flutter/material.dart';
// import 'package:simplethread/src/compoents/My_drawer.dart';
import 'package:simplethread/src/compoents/my_appbar.dart';
import 'package:simplethread/src/compoents/my_drawer.dart';

class HomePage extends StatelessWidget {
  const HomePage({super.key});
  static const String homerouter = '/home';

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      appBar: MyAppBar(
        title: 'Home',
        // icon: Icons.home_rounded,
      ),
      drawer: MyDrawer(),
    );
  }
}

I'm attempting to open the drawer item. If a screen is open, close the drawer instead of opening it again. If the tap indicates that the screen is closed, open the screen and close the drawer.

here is my logic that I have added in the code base

 Padding(
                padding: const EdgeInsets.only(left: 25.0),
                child: ListTile(
                  title: Text(
                    "H O M E",
                    style: GoogleFonts.playfairDisplay(
                      color: Colors.black,
                      // fontWeight: FontWeight.bold,
                    ),
                  ),
                  leading: const Icon(Icons.home_rounded),
                  onTap: () {
                    // Check if the current page is an instance of HomePage
                    if (ModalRoute.of(context)?.settings.arguments
                        is HomePage) {
                      // If it is, just close the drawer
                      Navigator.pop(context);
                    } else {
                      // If it's not, close the drawer and navigate to the Home Page
                      Navigator.pop(context);
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const HomePage(),
                        ),
                      );
                    }
                  },
                ),
              ),
0

There are 0 best solutions below