section.dart 3.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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';

import 'demo.dart';

class GallerySection extends StatelessComponent {
  GallerySection({ this.title, this.image, this.colors, this.demos });

  final String title;
  final String image;
  final Map<int, Color> colors;
  final List<GalleryDemo> demos;

  void showDemo(GalleryDemo demo, BuildContext context, ThemeData theme) {
19
    Navigator.push(context, new MaterialPageRoute<Null>(
20 21 22 23 24 25 26 27 28 29 30 31 32 33
      builder: (BuildContext context) {
        Widget child = (demo.builder == null) ? null : demo.builder();
        return new Theme(data: theme, child: child);
      }
    ));
  }

  void showDemos(BuildContext context) {
    final ThemeData theme = new ThemeData(
      brightness: Theme.of(context).brightness,
      primarySwatch: colors
    );
    final double appBarHeight = 200.0;
    final Key scrollableKey = new ValueKey<String>(title); // assume section titles differ
34
    Navigator.push(context, new MaterialPageRoute<Null>(
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      builder: (BuildContext context) {
        return new Theme(
          data: theme,
          child: new Scaffold(
            appBarHeight: appBarHeight,
            appBarBehavior: AppBarBehavior.scroll,
            scrollableKey: scrollableKey,
            toolBar: new ToolBar(
              flexibleSpace: (BuildContext context) => new FlexibleSpaceBar(title: new Text(title))
            ),
            body: new Material(
              child: new MaterialList(
                scrollableKey: scrollableKey,
                scrollablePadding: new EdgeDims.only(top: appBarHeight),
                type: MaterialListType.oneLine,
                children: (demos ?? const <GalleryDemo>[]).map((GalleryDemo demo) {
                  return new ListItem(
Hans Muller's avatar
Hans Muller committed
52
                    primary: new Text(demo.title),
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
                    onTap: () { showDemo(demo, context, theme); }
                  );
                })
              )
            )
          )
        );
      }
    ));
  }

  Widget build (BuildContext context) {
    final ThemeData theme = new ThemeData(
      brightness: Theme.of(context).brightness,
      primarySwatch: colors
    );
    final TextStyle titleTextStyle = theme.text.title.copyWith(
70
      color: Colors.white
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    );
    return new Flexible(
      child: new GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () { showDemos(context); },
        child: new Container(
          height: 256.0,
          margin: const EdgeDims.all(4.0),
          decoration: new BoxDecoration(backgroundColor: theme.primaryColor),
          child: new Column(
            children: <Widget>[
              new Flexible(
                child: new Padding(
                  padding: const EdgeDims.symmetric(horizontal: 12.0),
                  child: new AssetImage(
                    name: image,
                    alignment: const FractionalOffset(0.5, 0.5),
                    fit: ImageFit.contain
                  )
                )
              ),
              new Padding(
                padding: const EdgeDims.all(16.0),
                child: new Align(
                  alignment: const FractionalOffset(0.0, 1.0),
                  child: new Text(title, style: titleTextStyle)
                )
              )
            ]
          )
        )
      )
    );
  }
}