gpt4 book ai didi

flutter - 我正在尝试在“弹出菜单”中实现onTap()函数,但无法这样做

转载 作者:行者123 更新时间:2023-12-03 04:21:56 25 4
gpt4 key购买 nike

  • 当PopMenuItem中的一项时,我希望它执行某些特定任务。
  • 我尝试实现,但是它没有做任何尝试实现onTap()函数的操作,但是它给了我错误,要求保持不变。
  • 这是代码
  • import 'package:flutter/material.dart';
    import 'package:ssrpa/ui/forgot_password.dart';
    import 'package:ssrpa/admin/admin_page2.dart';

    class PopUpMenu extends StatelessWidget {
    VoidCallback onEnable;
    VoidCallback onDisable;
    VoidCallback onDelete;

    PopUpMenu({this.onEnable, this.onDisable, this.onDelete});

    void showMenuSelection(String value) {
    switch (value) {
    case 'Enable':
    onEnable();
    break;
    case 'Disable':
    onDisable();
    break;
    case 'Delete':
    onDelete();
    break;
    // Other cases for other menu options
    }
    }

    @override
    Widget build(BuildContext context) {
    return PopupMenuButton<String>(
    padding: EdgeInsets.zero,
    icon: Icon(Icons.more_vert),
    onSelected: showMenuSelection,
    itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
    const PopupMenuItem<String>(
    value: 'Enable User',
    child: ListTile(
    title: Text('Enable User'))),
    const PopupMenuItem<String>(
    value: 'Disable User',
    child: ListTile(
    title: Text('Disable User'))),
    const PopupMenuItem<String>(
    value: 'Delete User',
    child: ListTile(
    title: Text('Delete User')))

    ]
    );
    }
    }
  • 从PopMenuItem中选择菜单之一时,可以调用其他程序中定义的函数吗?
  • 屏幕截图如下所示。
    enter image description here
  • 最佳答案

    您在弹出菜单项中发送的值字符串与您的开关盒不匹配

    import 'package:flutter/material.dart';
    import 'package:ssrpa/ui/forgot_password.dart';
    import 'package:ssrpa/admin/admin_page2.dart';

    class PopUpMenu extends StatelessWidget {
    VoidCallback onEnable;
    VoidCallback onDisable;
    VoidCallback onDelete;

    PopUpMenu({this.onEnable, this.onDisable, this.onDelete});

    void showMenuSelection(String value) {
    switch (value) {
    case 'Enable User':
    onEnable();
    break;
    case 'Disable User':
    onDisable();
    break;
    case 'Delete User':
    onDelete();
    break;
    // Other cases for other menu options
    }
    }

    @override
    Widget build(BuildContext context) {
    return PopupMenuButton<String>(
    padding: EdgeInsets.zero,
    icon: Icon(Icons.more_vert),
    onSelected: showMenuSelection,
    itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
    const PopupMenuItem<String>(
    value: 'Enable User',
    child: ListTile(
    title: Text('Enable User'))),
    const PopupMenuItem<String>(
    value: 'Disable User',
    child: ListTile(
    title: Text('Disable User'))),
    const PopupMenuItem<String>(
    value: 'Delete User',
    child: ListTile(
    title: Text('Delete User')))

    ]
    );
    }
    }

    关于flutter - 我正在尝试在“弹出菜单”中实现onTap()函数,但无法这样做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62112411/

    25 4 0