Skip to content

Instantly share code, notes, and snippets.

@codesxt
Created October 9, 2023 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codesxt/3e880ce6b566f55869ca416f9a38def8 to your computer and use it in GitHub Desktop.
Save codesxt/3e880ce6b566f55869ca416f9a38def8 to your computer and use it in GitHub Desktop.
GestureDetector example 02
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Gestures'),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double _top = 100;
double _left = 100;
bool _isPanned = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Stack(
children: [
Positioned(
top: _top,
left: _left,
child: GestureDetector(
onPanUpdate: (details) {
_top = max(0, _top + details.delta.dy);
_left = max(0, _left + details.delta.dx);
setState(() {});
},
onPanStart: (details) {
_isPanned = true;
setState(() {});
},
onPanEnd: (details) {
_isPanned = false;
setState(() {});
},
child: Container(
height: 50,
width: 50,
color: _isPanned ? Colors.red : Colors.amber,
),
),
),
Positioned(
bottom: 50,
left: 50,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Top: $_top'),
Text('Left: $_left'),
],
),
),
],
),
// This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment