GestureDetector防止MouseRegion-颤振

时间:2020-10-01 18:05:44

标签: flutter dart gesture

我有两个类Matrix和CardSqure。 Matrix包括GestureDetector,而CardSqure是Matrix的子级之一,其中包括MouseRegion。 当GestureDetectore捕获移动(平移时)并等待其完成(移动时) 它阻止了鼠标检测器的onHover功能起作用,这两个类

class CardSquare extends StatefulWidget {
  bool _isActivited = false;
  bool isInTheSamePressed = false;
  Function parentIsPressed;
  double size;
  CardSquare({@required this.parentIsPressed, @required this.size});
  void activate() {
    _isActivited = true;
  }

  void deactivate() {
    _isActivited = false;
  }

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

class _CardSquareState extends State<CardSquare> {
  final Color activateColor = Color(0xff1abc9c);
  final Color deactivateColor = Color(0xff34495e);

  Color selectColor() {
    if (widget._isActivited) return activateColor;
    return deactivateColor;
  }

  changeColor() {
    setState(() {
      widget._isActivited = !widget._isActivited;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      onHover: (pos) {
        if (widget.parentIsPressed()) changeColor();
      },
      child: Container(
        color: selectColor(),
        width: widget.size,
        height: widget.size,
      ),
    );
  }
}



class Matrix extends StatefulWidget {
  int _length_x = 10;
  int _length_y = 10;

  void changeLengthX(int newLengthX) {
    _length_x = newLengthX;
  }

  void changeLengthY(int newLengthY) {
    _length_y = newLengthY;
  }

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

class _MatrixState extends State<Matrix> {
  List<CardSquare> cardList = new List<CardSquare>();
  final double spacing = 2.0;
  bool _isPressed = false;

  @override
  void initState() {
    super.initState();
    for (int i = 0; i < widget._length_x; i++) {
      for (int j = 0; j < widget._length_y; j++) {
        cardList.add(
          CardSquare(
            parentIsPressed: () {
              // print(_isPressed);
              return _isPressed;
            },
            size: 10,
          ),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onPanStart: (pos) {
        print("Start");
        _isPressed = true;
      },
      onPanEnd: (pos) {
        _isPressed = false;
        print("End");
      },
      child: Container(
        alignment: Alignment.center,
        color: Colors.white,
        margin: EdgeInsets.all(10.0),
        child: GridView(
          physics: const NeverScrollableScrollPhysics(),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: widget._length_x,
            crossAxisSpacing: spacing,
            mainAxisSpacing: spacing,
          ),
          shrinkWrap: true,
          children: cardList,
        ),
      ),
    );
  }
}

预先感谢:)

0 个答案:

没有答案