The onTapped of my Drawer tiles gets executed when i click my drawer menu button

Issue

So I made a custom app drawer for my flutter app. but now when I click my open drawer menu button, The first else statement of my widget tree gets executed. I am relatively new to flutter. Does anyone know what I’m doing wrong here?

This is The CustomDrawer Code.

import 'package:flutter/material.dart';
import 'package:open_cart/utils/colors.dart';
import 'package:open_cart/utils/styles.dart';
import 'package:open_cart/widgets/drawer_tile_custom_widget.dart';

class CustomAppDrawerWidget extends StatelessWidget {
  final bool _isLoggedIn = true;
  const CustomAppDrawerWidget({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
        child: Container(
      color: colorDarkGrey,
      child: ListView(
        children: [
          DrawerHeader(
            child: Center(
              child: Text(
                'The BurgerSpot',
                style: tsCOrangeCustomFFPrimaryS45,
              ),
            ),
          ),
          if (_isLoggedIn)
            DrawerTileCustomWidget(
              title: 'Saved Addresses',
              iconData: Icons.home,
              onPressed: () {
                print('test3');
              },
            ),
          if (!_isLoggedIn)
            DrawerTileCustomWidget(
              title: 'My Account',
              iconData: Icons.person,
              onPressed: () {
                print('test2');
              },
            )
          else
            DrawerTileCustomWidget(
              title: 'Login/Register',
              iconData: Icons.login,
              onPressed: () {
                print('test1');
              },
            ),
          if (_isLoggedIn)
            DrawerTileCustomWidget(
              title: 'Orders',
              iconData: Icons.store,
              onPressed: () {},
            ),
          DrawerTileCustomWidget(
            title: 'Deals and Offers',
            iconData: Icons.star_border,
            onPressed: () {},
          ),
          DrawerTileCustomWidget(
            title: 'Contact us',
            iconData: Icons.login,
            onPressed: () {},
          ),
        ],
      ),
    ));
  }
}

This is the CustomDrawerTile Code.

import 'package:flutter/material.dart';
import 'package:open_cart/utils/colors.dart';
import 'package:open_cart/utils/styles.dart';

class DrawerTileCustomWidget extends StatelessWidget {
  final IconData? iconData;
  final String? title;
  final Function? onPressed;
  const DrawerTileCustomWidget({
    Key? key,
    required this.iconData,
    required this.title,
    required this.onPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: Icon(
        iconData,
        color: colorFF,
      ),
      title: Text(
        title.toString(),
        style: tsCwhiteFFPrimaryS15,
      ),
      onTap: onPressed!(),
    );
  }
}

This is the output from my debug console.

Launching lib\main.dart on AOSP on IA Emulator in debug mode...
√  Built build\app\outputs\flutter-apk\app-debug.apk.
W/FlutterActivityAndFragmentDelegate(24256): A splash screen was provided to Flutter, but this is deprecated. See flutter.dev/go/android-splash-migration for migration steps.
D/eglCodecCommon(24256): setVertexArrayObject: set vao to 0 (0) 1 2
I/OpenGLRenderer(24256): Davey! duration=702ms; Flags=1, IntendedVsync=63537921942406, Vsync=63538505275716, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=63538521171700, AnimationStart=63538521225400, PerformTraversalsStart=63538521936600, DrawStart=63538529983700, SyncQueued=63538537071200, SyncStart=63538538794200, IssueDrawCommandsStart=63538539191100, SwapBuffers=63538601435900, FrameCompleted=63538626192900, DequeueBufferDuration=17778000, QueueBufferDuration=331000,
Connecting to VM Service at ws://127.0.0.1:54707/W23b5t3IEOE=/ws
D/eglCodecCommon(24256): setVertexArrayObject: set vao to 0 (0) 1 0
W/DynamiteModule(24256): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule(24256): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(24256): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
I/m.opencart.sho(24256): The ClassLoaderContext is a special shared library.
I/m.opencart.sho(24256): The ClassLoaderContext is a special shared library.
I/chatty  (24256): uid=10086(com.opencart.shop) AsyncTask #2 identical 1 line
I/m.opencart.sho(24256): The ClassLoaderContext is a special shared library.
W/m.opencart.sho(24256): Accessing hidden field Ljava/nio/Buffer;->address:J (light greylist, reflection)
W/ProviderInstaller(24256): Failed to report request stats: reportRequestStats [class android.content.Context, long, long]
V/NativeCrypto(24256): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 286 native methods...
W/m.opencart.sho(24256): Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (light greylist, reflection)
D/NetworkSecurityConfig(24256): No Network Security Config specified, using platform default
I/ProviderInstaller(24256): Installed default security provider GmsCore_OpenSSL
W/m.opencart.sho(24256): Accessing hidden field Ljava/net/Socket;->impl:Ljava/net/SocketImpl; (light greylist, reflection)
W/m.opencart.sho(24256): Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (light greylist, linking)
W/m.opencart.sho(24256): Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (light greylist, linking)
W/m.opencart.sho(24256): Accessing hidden field Ljava/io/FileDescriptor;->descriptor:I (light greylist, JNI)
W/m.opencart.sho(24256): Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (light greylist, reflection)
W/m.opencart.sho(24256): Accessing hidden method Ldalvik/system/BlockGuard;->getThreadPolicy()Ldalvik/system/BlockGuard$Policy; (light greylist, linking)
W/m.opencart.sho(24256): Accessing hidden method Ldalvik/system/BlockGuard$Policy;->onNetwork()V (light greylist, linking)
I/flutter (24256): test3
I/flutter (24256): test1

Solution

I haven’t tested it, but I suspect the error is here: onTap: onPressed!(),. Change it to onTap: () {onPressed!();} , or even better onTap: onPressed,.

Answered By – Ralph Bergmann

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *