当我点击我的抽屉菜单按钮时,我的抽屉瓷砖的 onTapped 被执行

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

所以我为我的 flutter 应用程序制作了一个自定义应用程序抽屉。但是现在当我点击我打开的抽屉菜单按钮时,我的小部件树的第一个 else 语句被执行。我对扑动比较陌生。有谁知道我在这里做错了什么?

这是 CustomDrawer 代码。

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

这是 CustomDrawerTile 代码。

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!(),
    );
  }
}

这是我的调试控制台的输出。

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

我没有测试过,但我怀疑错误在这里:onTap: onPressed!(),。将其更改为 onTap: () {onPressed!();} , 或更好的 onTap: onPressed,.