Skip to content

Instantly share code, notes, and snippets.

@justinmc
Created May 2, 2022 21:45
Show Gist options
  • Select an option

  • Save justinmc/8b64cae32e4fabe7208036996f71d6ca to your computer and use it in GitHub Desktop.

Select an option

Save justinmc/8b64cae32e4fabe7208036996f71d6ca to your computer and use it in GitHub Desktop.
Pan gesture localPosition bug?
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// 1. Open the console so you can see the prints.
// 2. Scroll to the bottom of the red/blue container and tap.
// The location of the tap is logged.
// Notice that the local dy includes the scrolling and global does not.
// 3. Scroll back up to the top and start dragging.
// The location of the drag is continuously logged.
// After 2 seconds, the scroll position jump to the bottom.
// Expected: The localPosition includes the scroll.
// Actual: The localPosition is the same as if the scroll didn't happen.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final ScrollController _scrollController = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
controller: _scrollController,
child: GestureDetector(
onTapDown: (TapDownDetails details) {
print('tapped at ${details.localPosition}, ${details.globalPosition}');
},
onPanStart: (DragStartDetails details) {
Future<void>.delayed(const Duration(seconds: 2)).then((_) {
_scrollController.jumpTo(1200.0);
});
},
onPanUpdate: (DragUpdateDetails details) {
print('pan update at ${details.localPosition}, ${details.globalPosition}');
},
child: Container(
width: 200.0,
height: 2000.0,
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: <Color>[
Colors.red,
Colors.blue,
],
),
),
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment