简体   繁体   English

Flutter中的可拖动FloatingActionButton

[英]Draggable FloatingActionButton in Flutter

我想制作一个可拖动的浮动按钮,并在可能的情况下在控制台中打印拖动位置。

Due to the ability to compose Flutter widgets in whatever way you want to, you can simply do it like this: 由于可以按照您希望的任何方式编写Flutter小部件,因此您可以像这样简单地进行操作:

Draggable(
    feedback: FloatingActionButton(child: Icon(Icons.drag_handle), onPressed: () {}),
    child: FloatingActionButton(child: Icon(Icons.drag_handle), onPressed: () {}),
    childWhenDragging: Container(),
    onDragEnd: (details) => print(details.offset))

This Draggable can be passed as the floatingActionButton argument to a Scaffold . Draggable可以作为传递floatingActionButton参数的Scaffold
The feedback parameter controls how the widget that is dragged about in the end will look like and child specifies how the stationary Draggable should look like. feedback参数控制最后拖动的窗口小部件的外观, child指定静止的Draggable外观。 As you probably only want to see one FloatingActionButton at a time , you can pass an empty Container to childWhenDragging . 因为您可能一次只想看到一个 FloatingActionButton ,所以可以将一个空的Container传递给childWhenDragging

The Offset (position) of the drag will be printed to the console when releasing the drag. 释放拖动时,拖动的Offset (位置)将被打印到控制台。

您可以使用DraggableFloatingButton包: https : //pub.dartlang.org/packages/draggable_floating_button

I needed to add a positioned widget to set the position of my button 我需要添加一个定位的小部件来设置按钮的位置

class _MyHomePageState extends State<MyHomePage> {
  Offset position =Offset(20.0, 20.0);
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: Stack(
      children: <Widget>[
        Container(
          decoration: new BoxDecoration(
            image: new DecorationImage(
              image: new AssetImage("data_repo/img/bg1.jpg"),
              fit: BoxFit.cover,
            ),
          ),
        ),
        Positioned(
          left : position.dx,
          top  : position.dy ,
          child : Draggable(
          feedback: Container(
            child : FloatingActionButton(child: Icon(Icons.add), onPressed: () {})
            ),
          child: Container(
            child : FloatingActionButton(child: Icon(Icons.add), onPressed: () {}),
          ),
          childWhenDragging: Container(),
          onDragEnd: (details){
            setState(() {
              position = details.offset;
            });
            print(position);
            print(position.dx);
            print(position.dy);
            }))
      ],
    ));
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM