Commit 6d587704 authored by Ian Hickson's avatar Ian Hickson

Fix the fixed height card demo

Turns out card_collection had all kinds of bugs.
parent 794b7051
...@@ -30,6 +30,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -30,6 +30,7 @@ class CardCollectionState extends State<CardCollection> {
static const String _sunshineURL = "http://www.walltor.com/images/wallpaper/good-morning-sunshine-58540.jpg"; static const String _sunshineURL = "http://www.walltor.com/images/wallpaper/good-morning-sunshine-58540.jpg";
static const double kCardMargins = 8.0; static const double kCardMargins = 8.0;
static const double kFixedCardHeight = 100.0;
final TextStyle backgroundTextStyle = final TextStyle backgroundTextStyle =
Typography.white.title.copyWith(textAlign: TextAlign.center); Typography.white.title.copyWith(textAlign: TextAlign.center);
...@@ -59,10 +60,9 @@ class CardCollectionState extends State<CardCollection> { ...@@ -59,10 +60,9 @@ class CardCollectionState extends State<CardCollection> {
void _initFixedSizedCardModels() { void _initFixedSizedCardModels() {
const int cardCount = 27; const int cardCount = 27;
const double cardHeight = 100.0;
_cardModels = new List<CardModel>.generate( _cardModels = new List<CardModel>.generate(
cardCount, cardCount,
(int i) => new CardModel(i, cardHeight) (int i) => new CardModel(i, kFixedCardHeight)
); );
} }
...@@ -73,6 +73,11 @@ class CardCollectionState extends State<CardCollection> { ...@@ -73,6 +73,11 @@ class CardCollectionState extends State<CardCollection> {
_initVariableSizedCardModels(); _initVariableSizedCardModels();
} }
Iterable<int> get _cardIndices sync* {
for (int i = 0; i < _cardModels.length; i += 1)
yield i;
}
@override @override
void initState() { void initState() {
super.initState(); super.initState();
...@@ -99,9 +104,8 @@ class CardCollectionState extends State<CardCollection> { ...@@ -99,9 +104,8 @@ class CardCollectionState extends State<CardCollection> {
} }
double _fixedSizeToSnapOffset(double scrollOffset) { double _fixedSizeToSnapOffset(double scrollOffset) {
double cardHeight = _cardModels[0].height; int cardIndex = (scrollOffset.clamp(0.0, kFixedCardHeight * (_cardModels.length - 1)) / kFixedCardHeight).floor();
int cardIndex = (scrollOffset.clamp(0.0, cardHeight * (_cardModels.length - 1)) / cardHeight).floor(); return cardIndex * kFixedCardHeight + kFixedCardHeight * 0.5;
return cardIndex * cardHeight + cardHeight * 0.5;
} }
double _toSnapOffset(double scrollOffset, Size containerSize) { double _toSnapOffset(double scrollOffset, Size containerSize) {
...@@ -300,7 +304,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -300,7 +304,7 @@ class CardCollectionState extends State<CardCollection> {
Widget card = new Dismissable( Widget card = new Dismissable(
key: new ObjectKey(cardModel), key: new ObjectKey(cardModel),
direction: _dismissDirection, direction: _dismissDirection,
onResize: () { _invalidator(<int>[index]); }, onResize: () { if (_invalidator != null) _invalidator(<int>[index]); },
onDismissed: (DismissDirection direction) { dismissCard(cardModel); }, onDismissed: (DismissDirection direction) { dismissCard(cardModel); },
child: new Card( child: new Card(
color: _primaryColor[cardModel.color], color: _primaryColor[cardModel.color],
...@@ -321,7 +325,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -321,7 +325,7 @@ class CardCollectionState extends State<CardCollection> {
) )
: new DefaultTextStyle( : new DefaultTextStyle(
style: DefaultTextStyle.of(context).merge(cardLabelStyle).merge(_textStyle).copyWith( style: DefaultTextStyle.of(context).merge(cardLabelStyle).merge(_textStyle).copyWith(
fontSize: _varyFontSizes ? _cardModels.length.toDouble() : null fontSize: _varyFontSizes ? 5.0 + index : null
), ),
child: new Column( child: new Column(
children: <Widget>[ children: <Widget>[
...@@ -409,10 +413,11 @@ class CardCollectionState extends State<CardCollection> { ...@@ -409,10 +413,11 @@ class CardCollectionState extends State<CardCollection> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
Widget cardCollection; Widget cardCollection;
if (_fixedSizeCards) { if (_fixedSizeCards) {
cardCollection = new ScrollableList ( _invalidator = null;
cardCollection = new ScrollableList(
snapOffsetCallback: _snapToCenter ? _toSnapOffset : null, snapOffsetCallback: _snapToCenter ? _toSnapOffset : null,
itemExtent: _cardModels[0].height, itemExtent: kFixedCardHeight,
children: _cardModels.map((CardModel card) => _buildCard(context, card.value)) children: _cardIndices.map/*<Widget>*/((int index) => _buildCard(context, index))
); );
} else { } else {
cardCollection = new ScrollableMixedWidgetList( cardCollection = new ScrollableMixedWidgetList(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment