cards_demo.dart 6.41 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4
// 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.

5
import 'package:flutter/foundation.dart';
Hans Muller's avatar
Hans Muller committed
6 7
import 'package:flutter/material.dart';

8 9
import '../../gallery/demo.dart';

10 11
const String _kGalleryAssetsPackage = 'flutter_gallery_assets';

Hans Muller's avatar
Hans Muller committed
12
class TravelDestination {
13 14 15 16 17 18
  const TravelDestination({
    this.assetName,
    this.assetPackage,
    this.title,
    this.description,
  });
Hans Muller's avatar
Hans Muller committed
19 20

  final String assetName;
21
  final String assetPackage;
Hans Muller's avatar
Hans Muller committed
22 23 24 25 26 27 28 29
  final String title;
  final List<String> description;

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

final List<TravelDestination> destinations = <TravelDestination>[
  const TravelDestination(
30
    assetName: 'places/india_thanjavur_market.png',
31
    assetPackage: _kGalleryAssetsPackage,
32
    title: 'Top 10 Cities to Visit in Tamil Nadu',
33
    description: <String>[
Hans Muller's avatar
Hans Muller committed
34
      'Number 10',
35 36
      'Thanjavur',
      'Thanjavur, Tamil Nadu',
37
    ],
Hans Muller's avatar
Hans Muller committed
38 39
  ),
  const TravelDestination(
40
    assetName: 'places/india_chettinad_silk_maker.png',
41
    assetPackage: _kGalleryAssetsPackage,
42
    title: 'Artisans of Southern India',
43
    description: <String>[
44 45 46
      'Silk Spinners',
      'Chettinad',
      'Sivaganga, Tamil Nadu',
47
    ],
Hans Muller's avatar
Hans Muller committed
48 49 50
  )
];

51
class TravelDestinationItem extends StatelessWidget {
Hans Muller's avatar
Hans Muller committed
52
  TravelDestinationItem({ Key key, @required this.destination, this.shape })
53 54
    : assert(destination != null && destination.isValid),
      super(key: key);
Hans Muller's avatar
Hans Muller committed
55

56
  static const double height = 366.0;
Hans Muller's avatar
Hans Muller committed
57
  final TravelDestination destination;
Hans Muller's avatar
Hans Muller committed
58
  final ShapeBorder shape;
Hans Muller's avatar
Hans Muller committed
59

60
  @override
Hans Muller's avatar
Hans Muller committed
61
  Widget build(BuildContext context) {
62 63 64
    final ThemeData theme = Theme.of(context);
    final TextStyle titleStyle = theme.textTheme.headline.copyWith(color: Colors.white);
    final TextStyle descriptionStyle = theme.textTheme.subhead;
Hans Muller's avatar
Hans Muller committed
65

66
    return SafeArea(
67 68
      top: false,
      bottom: false,
69
      child: Container(
70 71
        padding: const EdgeInsets.all(8.0),
        height: height,
72
        child: Card(
Hans Muller's avatar
Hans Muller committed
73
          shape: shape,
74
          child: Column(
75 76 77
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              // photo and title
78
              SizedBox(
79
                height: 184.0,
80
                child: Stack(
81
                  children: <Widget>[
82 83
                    Positioned.fill(
                      child: Image.asset(
84 85 86 87
                        destination.assetName,
                        package: destination.assetPackage,
                        fit: BoxFit.cover,
                      ),
88
                    ),
89
                    Positioned(
90 91 92
                      bottom: 16.0,
                      left: 16.0,
                      right: 16.0,
93
                      child: FittedBox(
94 95
                        fit: BoxFit.scaleDown,
                        alignment: Alignment.centerLeft,
96
                        child: Text(destination.title,
97 98
                          style: titleStyle,
                        ),
99 100
                      ),
                    ),
101 102
                  ],
                ),
103
              ),
Josh Soref's avatar
Josh Soref committed
104
              // description and share/explore buttons
105 106
              Expanded(
                child: Padding(
107
                  padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
108
                  child: DefaultTextStyle(
109 110 111
                    softWrap: false,
                    overflow: TextOverflow.ellipsis,
                    style: descriptionStyle,
112
                    child: Column(
113 114 115
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        // three line description
116
                        Padding(
117
                          padding: const EdgeInsets.only(bottom: 8.0),
118
                          child: Text(
119 120 121
                            destination.description[0],
                            style: descriptionStyle.copyWith(color: Colors.black54),
                          ),
122
                        ),
123 124
                        Text(destination.description[1]),
                        Text(destination.description[2]),
125 126
                      ],
                    ),
127 128 129
                  ),
                ),
              ),
130
              // share, explore buttons
131 132
              ButtonTheme.bar(
                child: ButtonBar(
133 134
                  alignment: MainAxisAlignment.start,
                  children: <Widget>[
135
                    FlatButton(
136 137 138 139
                      child: const Text('SHARE'),
                      textColor: Colors.amber.shade500,
                      onPressed: () { /* do nothing */ },
                    ),
140
                    FlatButton(
141 142 143 144 145 146
                      child: const Text('EXPLORE'),
                      textColor: Colors.amber.shade500,
                      onPressed: () { /* do nothing */ },
                    ),
                  ],
                ),
147
              ),
148 149
            ],
          ),
150 151
        ),
      ),
Hans Muller's avatar
Hans Muller committed
152 153 154 155
    );
  }
}

Hans Muller's avatar
Hans Muller committed
156 157

class CardsDemo extends StatefulWidget {
158
  static const String routeName = '/material/cards';
159

Hans Muller's avatar
Hans Muller committed
160
  @override
161
  _CardsDemoState createState() => _CardsDemoState();
Hans Muller's avatar
Hans Muller committed
162 163 164 165 166
}

class _CardsDemoState extends State<CardsDemo> {
  ShapeBorder _shape;

167
  @override
Hans Muller's avatar
Hans Muller committed
168
  Widget build(BuildContext context) {
169 170
    return Scaffold(
      appBar: AppBar(
Hans Muller's avatar
Hans Muller committed
171 172
        title: const Text('Travel stream'),
        actions: <Widget>[
173
          MaterialDemoDocumentationButton(CardsDemo.routeName),
174
          IconButton(
Hans Muller's avatar
Hans Muller committed
175 176 177 178
            icon: const Icon(Icons.sentiment_very_satisfied),
            onPressed: () {
              setState(() {
                _shape = _shape != null ? null : const RoundedRectangleBorder(
179 180 181 182 183
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(16.0),
                    topRight: Radius.circular(16.0),
                    bottomLeft: Radius.circular(2.0),
                    bottomRight: Radius.circular(2.0),
Hans Muller's avatar
Hans Muller committed
184 185 186 187 188 189
                  ),
                );
              });
            },
          ),
        ],
Hans Muller's avatar
Hans Muller committed
190
      ),
191
      body: ListView(
192
        itemExtent: TravelDestinationItem.height,
193
        padding: const EdgeInsets.only(top: 8.0, left: 8.0, right: 8.0),
194
        children: destinations.map<Widget>((TravelDestination destination) {
195
          return Container(
196
            margin: const EdgeInsets.only(bottom: 8.0),
197
            child: TravelDestinationItem(
Hans Muller's avatar
Hans Muller committed
198 199 200
              destination: destination,
              shape: _shape,
            ),
Hans Muller's avatar
Hans Muller committed
201
          );
202
        }).toList()
Hans Muller's avatar
Hans Muller committed
203 204 205 206
      )
    );
  }
}