flutter 拖拽视图

关键字:Draggable

import 'package:flutter/material.dart';

class DraggableWiget extends StatefulWidget {
  final Offset offset;
  final Color widgetColor;

  DraggableWiget({Key key,this.offset,this.widgetColor}) : super(key: key);

  @override
  _DraggableWigetState createState() => _DraggableWigetState();
}

class _DraggableWigetState extends State<DraggableWiget> {

Offset offset = Offset(0.0, 0.0);

@override
  void initState() {
    // TODO: implement initState
    super.initState();
    offset = widget.offset;
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: offset.dx,
      top: offset.dy,
      child: Draggable(
        data: widget.widgetColor,
        child: Container(
          width: 100.0,
          height: 100.0,
          color: widget.widgetColor,
        ),
        feedback: Container(
          width: 100.0,
          height: 100.0,
          color: widget.widgetColor.withOpacity(0.5),
        ),
        onDraggableCanceled: (Velocity velocity,Offset offset){

            setState(() {
              this.offset = offset;
            });
        },
      ));
  }
}

关键词:DragTarget

import 'package:flutter/material.dart';
import 'draggabe_widget.dart';

class DraggableDemo extends StatefulWidget {
  DraggableDemo({Key key}) : super(key: key);

  @override
  _DraggableDemoState createState() => _DraggableDemoState();
}

class _DraggableDemoState extends State<DraggableDemo> {
  Color _draggableColor = Colors.grey;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          DraggableWiget(
            offset: Offset(80.0,80.0),
            widgetColor: Colors.redAccent,
          ),
          DraggableWiget(
            offset: Offset(180.0,80.0),
            widgetColor: Colors.blueAccent,
          ),
          Center(
            child: DragTarget(onAccept: (Color color){
              _draggableColor = color;
            },builder: (context,candidateData,rejectedData){
              return Container(
                width: 200.0,
                height: 200.0,
                color: _draggableColor,
              );
            },),
          ),
        ],
      ),
    );
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值