为什么我抽屉里的按钮没有任何作用?

Why dont the buttons in my Drawer Do anything?

我的抽屉里有一些图标,但其中 none 似乎导航到另一个页面,我不知道为什么。没有错误等等,只是一个导航抽屉,有几个按钮,MainDrawer.dart包含抽屉,而DrawerListTile.dart包含抽屉上每个tile的属性。

这是MainDrawer.dart

import 'package:iona_central/Screens/News/news.dart';
import 'package:iona_central/Screens/Exam/Exam_Rseult.dart';
import 'package:iona_central/Screens/Leave_Apply/Leave_apply.dart';
import 'package:iona_central/Screens/home.dart';
import 'package:iona_central/Widgets/DrawerListTile.dart';

class MainDrawer extends StatefulWidget {
  @override
  _MainDrawerState createState() => _MainDrawerState();
}

class _MainDrawerState extends State<MainDrawer> {
  @override
  Widget build(BuildContext context) {
    return Drawer(
        child: ListView(
      children: [
        DrawerListTile(
            imgpath: "home.png",
            name: "Home",
            ontap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (BuildContext context) => Home(),
                ),
              );
            }),
        DrawerListTile(
          imgpath: "news.png",
          name: "News",
          ontap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (BuildContext context) => attendance(),
              ),
            );
          },
        ),

        //DrawerListTile(imgpath: "profile.png", name: "Profile", ontap: () {}),
        DrawerListTile(
          imgpath: "ipsports.png",
          name: "Sports",
          ontap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (BuildContext context) => ExamResult(),
              ),
            );
          },
        ),
        /*DrawerListTile(
            imgpath: "calendar.png", name: "Time Table", ontap: () {}), */
        DrawerListTile(
          imgpath: "gfl.png",
          name: "GFL",
          ontap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (BuildContext context) => LeaveApply(),
              ),
            );
          },
        ),
        DrawerListTile(
          imgpath: "club.png",
          name: "Clubs",
          ontap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (BuildContext context) => LeaveApply(),
              ),
            );
          },
        ),
      ],
    ));
  }
}

这里是DrawerListTile.dart

import 'package:flutter/material.dart';

class DrawerListTile extends StatelessWidget {
  final String name;
  final String imgpath;
  final Function ontap;

  const DrawerListTile(
      {Key? key,
      required this.name,
      required this.imgpath,
      required this.ontap})
      : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ListTile(
      onTap: () => ontap,
      leading: Image.asset(
        "assets/${imgpath}",
        height: 30,
      ),
      contentPadding: EdgeInsets.only(
        left: 70,
        top: 5,
        bottom: 5,
      ),
      title: Text(
        "${name}",
        style: TextStyle(
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }
}

问题来自 onTap: () => ontap - 它没有调用传递的 ontap 函数。

使用 tear-off 如:onTap: ontap

看看这个:

class DrawerListTile extends StatelessWidget {
  final String name;
  final String imgpath;
  final VoidCallback ontap;

  const DrawerListTile(
      {Key? key,
      required this.name,
      required this.imgpath,
      required this.ontap})
      : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ListTile(
      onTap: ontap,
      leading: Image.asset(
        "assets/${imgpath}",
        height: 30,
      ),
      contentPadding: EdgeInsets.only(
        left: 70,
        top: 5,
        bottom: 5,
      ),
      title: Text(
        "${name}",
        style: TextStyle(
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }
}

换行

onTap: () => ontap,

onTap: ontap,

onTap: () => ontap.call(),