Last active
August 30, 2020 08:06
-
-
Save Abhilash-Chandran/12fd9650f7cdf7780b3e6831ce6cf915 to your computer and use it in GitHub Desktop.
smooth scrolling for pageview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// [https://github.com/flutter/flutter/issues/35687] | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/gestures.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) => MaterialApp( | |
home: const MyHomePage(), | |
); | |
} | |
final PageController _pageController = PageController(); | |
class MyHomePage extends StatelessWidget { | |
const MyHomePage({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
// Local dragStartDetail. | |
DragStartDetails dragStartDetails; | |
// Current drag instance - should be instantiated on overscroll and updated alongside. | |
Drag drag; | |
return DefaultTabController( | |
length: 2, | |
child: Scaffold( | |
appBar: AppBar( | |
title: const Center(child: Text('use the mouse wheel to scroll')), | |
bottom: TabBar( | |
tabs: const [ | |
Center(child: Text('ScrollView')), | |
Center(child: Text('PageView')) | |
], | |
), | |
), | |
body: TabBarView( | |
children: [ | |
SingleChildScrollView( | |
child: Column( | |
children: [ | |
for (int i = 0; i < 10; i++) | |
Container( | |
height: MediaQuery.of(context).size.height, | |
child: const Center( | |
child: FlutterLogo(size: 80), | |
), | |
), | |
], | |
), | |
), | |
NotificationListener( | |
onNotification: (notification) { | |
if (notification is ScrollStartNotification) { | |
dragStartDetails = notification.dragDetails; | |
} | |
if (notification is ScrollUpdateNotification) { | |
drag = _pageController.position.drag(dragStartDetails, () {}); | |
drag.update(notification.dragDetails); | |
} | |
if (notification is ScrollEndNotification) { | |
drag?.cancel(); | |
} | |
return true; | |
}, | |
child: PageView( | |
scrollDirection: Axis.vertical, | |
controller: _pageController, | |
children: [ | |
for (int i = 0; i < 10; ++i) | |
const Center( | |
child: FlutterLogo(size: 80), | |
), | |
], | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment