ensure_visible.dart 3.03 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:sky/material.dart';
6
import 'package:sky/widgets.dart';
7 8 9 10 11 12 13

class CardModel {
  CardModel(this.value, this.height, this.color);
  int value;
  double height;
  Color color;
  String get label => "Card $value";
Hixie's avatar
Hixie committed
14
  Key get key => new ObjectKey(this);
15 16 17 18 19
}

class EnsureVisibleApp extends App {

  static const TextStyle cardLabelStyle =
20
    const TextStyle(color: Colors.white, fontSize: 18.0, fontWeight: bold);
21

22
  static const TextStyle selectedCardLabelStyle =
23
    const TextStyle(color: Colors.white, fontSize: 24.0, fontWeight: bold);
24

25
  List<CardModel> cardModels;
26
  MixedViewportLayoutState layoutState = new MixedViewportLayoutState();
27
  CardModel selectedCardModel;
28 29 30 31 32 33 34 35

  void initState() {
    List<double> cardHeights = <double>[
      48.0, 63.0, 82.0, 146.0, 60.0, 55.0, 84.0, 96.0, 50.0,
      48.0, 63.0, 82.0, 146.0, 60.0, 55.0, 84.0, 96.0, 50.0,
      48.0, 63.0, 82.0, 146.0, 60.0, 55.0, 84.0, 96.0, 50.0
    ];
    cardModels = new List.generate(cardHeights.length, (i) {
36
      Color color = Color.lerp(Colors.red[300], Colors.blue[900], i / cardHeights.length);
37 38 39 40 41 42
      return new CardModel(i, cardHeights[i], color);
    });

    super.initState();
  }

43
  void handleTap(Widget card, CardModel cardModel) {
44 45 46 47
    ensureWidgetIsVisible(card, duration: const Duration(milliseconds: 200))
    .then((_) {
      setState(() { selectedCardModel = cardModel; });
    });
48 49 50 51 52 53
  }

  Widget builder(int index) {
    if (index >= cardModels.length)
      return null;
    CardModel cardModel = cardModels[index];
54
    TextStyle style = (cardModel == selectedCardModel) ? selectedCardLabelStyle : cardLabelStyle;
55 56 57 58 59
    Widget card = new Card(
      color: cardModel.color,
      child: new Container(
        height: cardModel.height,
        padding: const EdgeDims.all(8.0),
60
        child: new Center(child: new Text(cardModel.label, style: style))
61 62
      )
    );
63
    return new GestureDetector(
64
      key: cardModel.key,
65
      onTap: () => handleTap(card, cardModel),
66 67 68 69 70 71 72 73
      child: card
    );
  }

  Widget build() {
    Widget cardCollection = new Container(
      padding: const EdgeDims.symmetric(vertical: 12.0, horizontal: 8.0),
      decoration: new BoxDecoration(backgroundColor: Theme.of(this).primarySwatch[50]),
74
      child: new ScrollableMixedWidgetList(
75 76 77 78 79 80 81 82 83 84 85
        builder: builder,
        token: cardModels.length,
        layoutState: layoutState
      )
    );

    return new IconTheme(
      data: const IconThemeData(color: IconThemeColor.white),
      child: new Theme(
        data: new ThemeData(
          brightness: ThemeBrightness.light,
86 87
          primarySwatch: Colors.blue,
          accentColor: Colors.redAccent[200]
88
        ),
89 90
        child: new Title(
          title: 'Cards',
91
          child: new Scaffold(
Adam Barth's avatar
Adam Barth committed
92
            toolBar: new ToolBar(center: new Text('Tap a Card')),
93 94 95 96 97 98 99 100 101 102 103
            body: cardCollection
          )
        )
      )
    );
  }
}

void main() {
  runApp(new EnsureVisibleApp());
}