Flutter 颤振图标小部件
我正在使用一个图标小部件,当我按下图标跳转到另一个地方(比如呼叫图标),并且当我按下图标(比如三点图标)时打开另一个小部件的选项,我的问题是在颤振图标小部件中没有onlongpress。。。 有什么样的代码可以帮助你做到这一点吗 这是我目前的代码:Flutter 颤振图标小部件,flutter,widget,icons,onpress,Flutter,Widget,Icons,Onpress,我正在使用一个图标小部件,当我按下图标跳转到另一个地方(比如呼叫图标),并且当我按下图标(比如三点图标)时打开另一个小部件的选项,我的问题是在颤振图标小部件中没有onlongpress。。。 有什么样的代码可以帮助你做到这一点吗 这是我目前的代码: child: ListTile( leading: CircleAvatar( radius: 25.0, backgroundColor: Colors.brown, ), tit
child: ListTile(
leading: CircleAvatar(
radius: 25.0,
backgroundColor: Colors.brown,
),
title: Text(helpRequest.category),
subtitle: Text(helpRequest.description),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(
Icons.call,
size: 20.0,
color: Colors.brown[900],
),
onPressed: () {
},
),
IconButton(
icon: Icon(
Icons.more_vert,
size: 20.0,
color: Colors.brown[900],
),
onPressed: () {
},
),
],
),
),
child:ListTile(
领先:CircleAvatar(
半径:25.0,
背景颜色:Colors.brown,
),
标题:文本(helpRequest.category),
字幕:文本(helpRequest.description),
尾随:行(
mainAxisSize:mainAxisSize.min,
儿童:[
图标按钮(
图标:图标(
图标。呼叫,
尺寸:20.0,
颜色:颜色。棕色[900],
),
已按下:(){
},
),
图标按钮(
图标:图标(
图标。更多信息,
尺寸:20.0,
颜色:颜色。棕色[900],
),
已按下:(){
},
),
],
),
),
不要使用图标按钮,而是使用GestureDetector将图标包裹起来,这将为您提供onLongPress和onTap(onPressed)。请参阅下面的代码-
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("Flutter Demo")),
body: MyStatefulWidget(),
),
);
}
}
class MyStatefulWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
Offset _tapPosition;
void _storePosition(TapDownDetails details) {
_tapPosition = details.globalPosition;
}
return ListTile(
leading: const CircleAvatar(
radius: 25.0,
backgroundColor: Colors.brown,
),
title: const Text("helpRequest.category"),
subtitle: const Text("helpRequest.description"),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
GestureDetector(
onTap: () => print("Tap: call"),
onLongPress: () => print("Long Press: Call"),
child: Icon(
Icons.call,
size: 20.0,
color: Colors.brown[900],
),
),
GestureDetector(
onTap: () => print("Tap: more_vert"),
onTapDown: _storePosition,
onLongPress: () async {
final RenderBox overlay =
Overlay.of(context).context.findRenderObject();
final int _selected = await showMenu(
items: [
PopupMenuItem(
value: 1,
child: Row(
children: <Widget>[
const Icon(Icons.delete),
const Text("Delete"),
],
),
),
PopupMenuItem(
value: 2,
child: Row(
children: <Widget>[
const Icon(Icons.edit),
const Text("Edit"),
],
),
)
],
context: context,
position: RelativeRect.fromRect(
_tapPosition &
const Size(40, 40), // smaller rect, the touch area
Offset.zero & overlay.size // Bigger rect, the entire screen
),
);
switch (_selected) {
case 1:
print("delete seleted");
break;
case 2:
print("edit seleted");
break;
}
},
child: Icon(
Icons.more_vert,
size: 20.0,
color: Colors.brown[900],
),
),
],
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(标题:常量文本(“颤振演示”),
正文:MyStatefulWidget(),
),
);
}
}
类MyStatefulWidget扩展了无状态Widget{
@凌驾
小部件构建(构建上下文){
偏移位置;
void\u存储位置(tapdown详细信息){
_t位置=详细信息。全局位置;
}
返回列表块(
前导:康斯特圆环星(
半径:25.0,
背景颜色:Colors.brown,
),
标题:const Text(“helpRequest.category”),
字幕:const Text(“helpRequest.description”),
尾随:行(
mainAxisSize:mainAxisSize.min,
儿童:[
手势检测器(
onTap:()=>打印(“点击:呼叫”),
onLongPress:()=>打印(“长按:呼叫”),
子:图标(
图标。呼叫,
尺寸:20.0,
颜色:颜色。棕色[900],
),
),
手势检测器(
onTap:()=>打印(“点击:更多内容”),
onTapDown:\u存储位置,
onLongPress:()异步{
最终渲染器覆盖=
Overlay.of(context.context.finderObject();
最终整数选择=等待显示菜单(
项目:[
PopupMenuItem(
价值:1,
孩子:排(
儿童:[
常量图标(Icons.delete),
常量文本(“删除”),
],
),
),
PopupMenuItem(
价值:2,
孩子:排(
儿童:[
常量图标(Icons.edit),
常量文本(“编辑”),
],
),
)
],
上下文:上下文,
位置:relative.fromRect(
_t定位&
常量大小(40,40),//较小的矩形,触摸区域
Offset.zero&overlay.size//biger rect,整个屏幕
),
);
开关(选中){
案例1:
打印(“删除所选内容”);
打破
案例2:
打印(“编辑选择”);
打破
}
},
子:图标(
图标。更多信息,
尺寸:20.0,
颜色:颜色。棕色[900],
),
),
],
),
);
}
}
InkWell还具有onLongpress参数,并且使用customBorder参数作为circleBorder,它也可以工作。是否有一种方法可以使用onLongpress中的showmenu小部件显示菜单?我试着去实现它,但它不起作用,在widget中实现widget这件事我不太满意,你能给我一个简单的提示吗?我已经更新了代码,在图标的长按上显示showmenu。更多垂直图标。您可以看到“删除和编辑”菜单选项。此外,当您点击任何这些菜单选项时,也可以在控制台日志中看到打印语句。如果答案有助于你解决问题,请喜欢并接受它。