我的应用程序中,我需要在长按FloatingActionButton
时执行第二个操作。
floatingActionButton: FloatingActionButton(
onPressed: someFunction,
child: Icon(Icons.add),
onLongPressed: //Something like this or any other solution
),
我的应用程序中,我需要在长按FloatingActionButton
时执行第二个操作。
floatingActionButton: FloatingActionButton(
onPressed: someFunction,
child: Icon(Icons.add),
onLongPressed: //Something like this or any other solution
),
我建议使用InkWell
,这样您还可以拥有涟漪效果。
这是如何使用它。
floatingActionButton: InkWell(
splashColor: Colors.blue,
onLongPress: () {
// handle your long press functionality here
},
child: FloatingActionButton(
child: Icon(Icons.add),
onPressed: someFunction,
),
)
class CustomWidget extends StatefulWidget {
final int index;
final bool longPressEnabled;
final VoidCallback callback;
const CustomWidget({Key key, this.index, this.longPressEnabled, this.callback}) : super(key: key);
@override
_CustomWidgetState createState() => new _CustomWidgetState();
}
class _CustomWidgetState extends State<CustomWidget> {
bool selected = false;
@override
Widget build(BuildContext context) {
return new GestureDetector(
onLongPress: () {
setState(() {
selected = !selected;
});
widget.callback();
},
GestureDetector中存在一个可以与onLongPress手势一起使用的方法。但是FloatingActionButton没有类似的直接解决方案。
但是如果你想要使用FloatingActionButton而不仅仅是GestureDetector,那么你可以将FAB嵌套在GestureDetector中,像这样轻松实现相同的结果:
GestureDetector(
onLongPress: () {
print("Long press made");
},
child: FloatingActionButton(
child: Text("FAB"),
onPressed: () {},
),
)
InkWell
更直观,因为GestureDetector
的存在是为了检测手势 :) 我唯一能看到的区别是,InkWell
(不使用splashColor
)默认情况下会给您提供触觉反馈。 - Grzegorz D.基于我得到的解决方案,我创建了自己的解决方案,似乎也具有所需的涟漪效果。谢谢大家。
floatingActionButton: FloatingActionButton(
onPressed: normalPressAction,
child: GestureDetector(
child: Icon(Icons.add),
onLongPress: longPressAction,
),
),
InkWell
包裹在ClipOval
中以达到这个效果。 - CopsOnRoad