如何在PopupMenuItem中使用onTap或onPressed

时间:2019-12-25 12:22:36

标签: flutter dart

如何在onTap中使用onPressedPopupMenuItem

这是我的代码:

actions: <Widget>[
  PopupMenuButton(
    icon: Icon(Icons.settings),
    itemBuilder: (context) => [
      PopupMenuItem(
        child: Text("Settings"),
      ),
      PopupMenuItem(
        child: Text("Flutter.io"),
      ),
      PopupMenuItem(
        child: Text("Google.com"),
      ),
    ],
  ),
]

我想在点击或单击“设置PopupMenuItem”时导航到SettingPage()。
即使遵循以下提到的解决方案,甚至在导入dart:js

之后,我仍然收到此错误
Error: Not found: 'dart:js'
import 'dart:js';

这是我的依赖项:

import 'package:bfdi_app/Profile/editProfile.dart';
import 'package:bfdi_app/models/user.dart';
import 'package:bfdi_app/services/collection.dart';
import 'package:bfdi_app/settings.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'dart:js';

5 个答案:

答案 0 :(得分:1)

有一个名为onSelected的属性,您应该使用它来处理onTap事件。

PopupMenuButton(
  icon: Icon(Icons.settings),
  onSelected: (newValue) { // add this property
    setState(() {
      _value = newValue; // it gives the value which is selected
    });
  },
  itemBuilder: (context) => [
    PopupMenuItem(
      child: Text("Settings"),
      value: 0,
    ),
    PopupMenuItem(
      child: Text("Flutter.io"),
      value: 1,
    ),
    PopupMenuItem(
      child: Text("Google.com"),
      value: 2,
    ),
  ],
)

答案 1 :(得分:1)

-根据评论进行编辑-

就这样:

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var items = [{'name':'Settings','value':0}, {'name':'Flutter.io','value':1}, {'name':'Google.com',,'value':2}];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: PopupMenuButton(
              onSelected: (x) {
           if(x==0){
                Navigator.push(
                     context,
                     MaterialPageRoute(builder: (context) => SettingPage()), );}
              },
              icon: Icon(Icons.settings),
              itemBuilder: (context) => items
                  .map<PopupMenuItem>((element) => PopupMenuItem(
                        child: Text(element['name]),
                        value: element['value'],
                      ))
                  .toList()),


    ));
  }
}

答案 2 :(得分:0)

只需将其添加到您的PopupMenuButton:

onSelected: (result) {
    if (result == 0) {
        Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => SettingPage()),
        );
    }
},

并将您的设置按钮更改为:

PopupMenuItem(
    child: Text("Settings"),
    value: 0,
),

答案 3 :(得分:0)

可调用的值。

PopupMenuButton(
  icon: Icon(Icons.settings),
  onSelected: (value) {
    value();
  },
  itemBuilder: (context) => [
    PopupMenuItem(
      child: Text('Settings'),
      value: () {
        debugPrint('open Settings');
      },
    ),
    PopupMenuItem(
      child: Text('Flutter.io'),
      value: () {
        debugPrint('goto Flutter.io');
      },
    ),
  ],
)

答案 4 :(得分:0)

显示带有 showMenu(...) 的弹出窗口时使用继承

class PopupItem<T> extends PopupMenuItem<T> {

  final Function() onTap;

  PopupItem({this.onTap, Widget child, Key key})
      : super(child: child, key: key);

  @override
  _PopupItemState<T> createState() => _PopupItemState<T>();
}

class _PopupItemState<T> extends PopupMenuItemState<T, PopupItem<T>> {
  @override
  void handleTap() {
    widget.onTap?.call();
    super.handleTap();
  }
}

用法:

  Widget _itemDelete() => PopupItem(
        onTap: () {
          // Handle tap here
        },
        child: Text(...)
      );