Search
 
SCRIPT & CODE EXAMPLE
 

DART

Flutter find if offset inside a polygon

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Draggable Custom Painter',
      home: Scaffold(
        body: CustomPainterDraggable(),
      ),
    );
  }
}

class CustomPainterDraggable extends StatefulWidget {
  @override
  _CustomPainterDraggableState createState() => _CustomPainterDraggableState();
}

class _CustomPainterDraggableState extends State<CustomPainterDraggable> {
  var xPos = 0.0;
  var yPos = 0.0;
  final width = 100.0;
  final height = 100.0;
  bool _dragging = false;

  /// Is the point (x, y) inside the rect?
  bool _insideRect(double x, double y) =>
      x >= xPos && x <= xPos + width && y >= yPos && y <= yPos + height;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onPanStart: (details) => _dragging = _insideRect(
        details.globalPosition.dx,
        details.globalPosition.dy,
      ),
      onPanEnd: (details) {
        _dragging = false;
      },
      onPanUpdate: (details) {
        if (_dragging) {
          setState(() {
            xPos += details.delta.dx;
            yPos += details.delta.dy;
          });
        }
      },
      child: Container(
        color: Colors.white,
        child: CustomPaint(
          painter: RectanglePainter(Rect.fromLTWH(xPos, yPos, width, height)),
          child: Container(),
        ),
      ),
    );
  }
}

class RectanglePainter extends CustomPainter {
  RectanglePainter(this.rect);
  final Rect rect;

  @override
  void paint(Canvas canvas, Size size) {
    canvas.drawRect(rect, Paint());
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}
Comment

PREVIOUS NEXT
Code Example
Dart :: single clone data in flutter 
Dart :: hive dart type adapter 
Dart :: toolbar image dart 
Dart :: multi-dimensional list in dart 
Dart :: scrolling to top sliverlist flutter with back button 
Dart :: future as a parameter with async in flutter 
Dart :: flutter gesturedetector space also clickable 
Dart :: flutter how to load a future function in main function 
Dart :: flutter compare two list 
Dart :: Remove space between widgets in row flutter 
Dart :: teledart flutter 
Dart :: limited box flutter 
Dart :: dart code examples 
Swift :: swift open url 
Swift :: swift + data to string 
Swift :: get length of array swift 
Swift :: swift open web page 
Swift :: swift collection view cell size 
Swift :: swiftui delay 
Swift :: uicolor from hex swift 
Swift :: swiftui vstack alignment 
Swift :: how to disable uitableview scrolling in swift 
Swift :: swift do while 
Swift :: string to double swift 
Swift :: swift find difference between two arrays 
Swift :: changing color of background swift 
Swift :: swift screenshot 
Swift :: check notification permission ios swift 
Swift :: swift paged scrollview get current page 
Swift :: debounce in swift 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =