Skip to content

Instantly share code, notes, and snippets.

@kangabru
Last active August 17, 2020 08:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kangabru/5ed6754e50fb9c36a096b16160afcbc7 to your computer and use it in GitHub Desktop.
Save kangabru/5ed6754e50fb9c36a096b16160afcbc7 to your computer and use it in GitHub Desktop.
A Flutter scrollable horizontal list widget which animates into place on load.
import 'package:flutter/material.dart';
import './animation_mixins.dart'; // See my other gist for this
/// A scrollable horizontal list which animates into place on load.
class ListPicker extends StatefulWidget {
final List<Widget> children;
final double paddingX, paddingY;
const ListPicker({
this.children,
this.paddingX = 12.0,
this.paddingY = 12.0,
});
@override
_ListPickerState createState() => _ListPickerState();
}
class _ListPickerState extends State<ListPicker> with StateDelay {
ScrollController _controller;
_getController() {
if (_controller != null) return _controller;
var cont = ScrollController(initialScrollOffset: 100);
// This delay is required to allow the controller to bind the scroll view
delay(0, () => cont.animateTo(0,
duration: Duration(milliseconds: 500), curve: Curves.easeOut));
setState(() => _controller = cont);
return cont;
}
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.zero,
margin: EdgeInsets.all(10),
child: SingleChildScrollView(
controller: _getController(),
scrollDirection: Axis.horizontal,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: widget.paddingX, vertical: widget.paddingY),
child: Row(children: widget.children),
)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment