Last active
December 22, 2020 12:52
-
-
Save magicleon94/c7df30441e8ddcef08f566f4a9f03a7c to your computer and use it in GitHub Desktop.
Sliding overflow: slides a long widget (like a text)
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
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
class SlidingOverflow extends StatefulWidget { | |
final Widget child; | |
const SlidingOverflow({Key key, this.child}) : super(key: key); | |
@override | |
_SlidingOverflowState createState() => _SlidingOverflowState(); | |
} | |
class _SlidingOverflowState extends State<SlidingOverflow> { | |
final kOffsetIncrement = 10.0; | |
final kDuration = Duration(milliseconds: 500); | |
final controller = ScrollController(); | |
var started = false; | |
@override | |
void dispose() { | |
controller?.dispose(); | |
super.dispose(); | |
} | |
void controllerLoop() async { | |
final destination = min(controller.position.maxScrollExtent, | |
controller.offset + kOffsetIncrement); | |
await controller.animateTo( | |
destination, | |
duration: kDuration, | |
curve: Curves.linear, | |
); | |
controllerLoop(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
if (!started) { | |
WidgetsBinding.instance.addPostFrameCallback((_) { | |
started = true; | |
controllerLoop(); | |
}); | |
} | |
return ListView.builder( | |
scrollDirection: Axis.horizontal, | |
controller: controller, | |
itemBuilder: (_, __) => widget.child, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment