cards_demo.dart 4.93 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright 2016 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.

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class TravelDestination {
  const TravelDestination({ this.assetName, this.title, this.description });

  final String assetName;
  final String title;
  final List<String> description;

  bool get isValid => assetName != null && title != null && description?.length == 3;
}

final List<TravelDestination> destinations = <TravelDestination>[
  const TravelDestination(
20
    assetName: 'packages/flutter_gallery_assets/top_10_australian_beaches.jpg',
Hans Muller's avatar
Hans Muller committed
21 22 23 24 25 26 27 28
    title: 'Top 10 Australian beaches',
    description: const <String>[
      'Number 10',
      'Whitehaven Beach',
      'Whitsunday Island, Whitsunday Islands'
    ]
  ),
  const TravelDestination(
29
    assetName: 'packages/flutter_gallery_assets/kangaroo_valley_safari.jpg',
Hans Muller's avatar
Hans Muller committed
30 31 32 33 34 35 36 37 38
    title: 'Kangaroo Valley Safari',
    description: const <String>[
      '2031 Moss Vale Road',
      'Kangaroo Valley 2577',
      'New South Wales'
    ]
  )
];

39
class TravelDestinationItem extends StatelessWidget {
Hans Muller's avatar
Hans Muller committed
40 41 42 43
  TravelDestinationItem({ Key key, this.destination }) : super(key: key) {
    assert(destination != null && destination.isValid);
  }

44
  static final double height = 328.0;
Hans Muller's avatar
Hans Muller committed
45 46
  final TravelDestination destination;

47
  @override
Hans Muller's avatar
Hans Muller committed
48 49
  Widget build(BuildContext context) {
    ThemeData theme = Theme.of(context);
50 51
    TextStyle titleStyle = theme.textTheme.headline.copyWith(color: Colors.white);
    TextStyle descriptionStyle = theme.textTheme.subhead;
Hans Muller's avatar
Hans Muller committed
52

53 54 55
    return new SizedBox(
      height: height,
      child: new Card(
Hans Muller's avatar
Hans Muller committed
56
        child: new Column(
57
          crossAxisAlignment: CrossAxisAlignment.start,
Hans Muller's avatar
Hans Muller committed
58 59 60 61 62 63
          children: <Widget>[
            // photo and title
            new SizedBox(
              height: 184.0,
              child: new Stack(
                children: <Widget>[
64
                  new Positioned.fill(
65 66
                    child: new Image.asset(
                      destination.assetName,
Hans Muller's avatar
Hans Muller committed
67 68 69 70 71 72
                      fit: ImageFit.cover
                    )
                  ),
                  new Positioned(
                    bottom: 16.0,
                    left: 16.0,
73 74 75 76 77 78 79 80 81
                    right: 16.0,
                    child: new FittedBox(
                      fit: ImageFit.scaleDown,
                      alignment: FractionalOffset.centerLeft,
                      child: new Text(destination.title,
                        style: titleStyle,
                      ),
                    ),
                  ),
Hans Muller's avatar
Hans Muller committed
82 83 84 85 86 87
                ]
              )
            ),
            // description and share/expore buttons
            new Flexible(
              child: new Padding(
88
                padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
89 90 91 92 93 94 95 96 97 98 99 100 101
                child: new DefaultTextStyle(
                  softWrap: false,
                  overflow: TextOverflow.ellipsis,
                  style: descriptionStyle,
                  child: new Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      // three line description
                      new Text(destination.description[0]),
                      new Text(destination.description[1]),
                      new Text(destination.description[2]),
                    ]
                  )
Hans Muller's avatar
Hans Muller committed
102 103
                )
              )
104 105 106
            ),
            // share, explore buttons
            // TODO(abarth): The theme and the bar should be part of card.
107
            new ButtonTheme.bar(
108 109 110 111 112 113 114 115 116 117 118 119 120 121
              child: new ButtonBar(
                alignment: MainAxisAlignment.start,
                children: <Widget>[
                  new FlatButton(
                    child: new Text('SHARE'),
                    onPressed: () { /* do nothing */ }
                  ),
                  new FlatButton(
                    child: new Text('EXPLORE'),
                    onPressed: () { /* do nothing */ }
                  ),
                ]
              )
            ),
Hans Muller's avatar
Hans Muller committed
122 123 124 125 126 127 128
          ]
        )
      )
    );
  }
}

129
class CardsDemo extends StatelessWidget {
130
  static final GlobalKey<ScrollableState> _scrollableKey = new GlobalKey<ScrollableState>();
131 132
  static const String routeName = '/cards';

133
  @override
Hans Muller's avatar
Hans Muller committed
134 135
  Widget build(BuildContext context) {
    return new Scaffold(
136
      scrollableKey: _scrollableKey,
137
      appBar: new AppBar(
138
        title: new Text('Travel stream')
Hans Muller's avatar
Hans Muller committed
139
      ),
140
      body: new ScrollableList(
141
        scrollableKey: _scrollableKey,
142
        itemExtent: TravelDestinationItem.height,
143
        padding: const EdgeInsets.only(top: 8.0, left: 8.0, right: 8.0),
Hans Muller's avatar
Hans Muller committed
144 145
        children: destinations.map((TravelDestination destination) {
          return new Container(
146
            margin: const EdgeInsets.only(bottom: 8.0),
Hans Muller's avatar
Hans Muller committed
147 148 149 150 151 152 153 154
            child: new TravelDestinationItem(destination: destination)
          );
        })
        .toList()
      )
    );
  }
}