home.dart 3.67 KB
Newer Older
1 2 3 4 5 6 7 8
// 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 'drawer.dart';
9
import 'item.dart';
10

11 12
const double _kFlexibleSpaceMaxHeight = 256.0;

13 14 15 16 17 18 19 20
List<GalleryItem> _itemsWithCategory(String category) {
  return kAllGalleryItems.where((GalleryItem item) => item.category == category).toList();
}

final List<GalleryItem> _demoItems = _itemsWithCategory('Demos');
final List<GalleryItem> _componentItems = _itemsWithCategory('Components');
final List<GalleryItem> _styleItems = _itemsWithCategory('Style');

21
class GalleryHome extends StatefulWidget {
22 23
  GalleryHome({
    Key key,
Eric Seidel's avatar
Eric Seidel committed
24
    this.useLightTheme,
25 26
    this.onThemeChanged,
    this.timeDilation,
Eric Seidel's avatar
Eric Seidel committed
27 28 29
    this.onTimeDilationChanged,
    this.showPerformanceOverlay,
    this.onShowPerformanceOverlayChanged
30 31 32
  }) : super(key: key) {
    assert(onThemeChanged != null);
    assert(onTimeDilationChanged != null);
Eric Seidel's avatar
Eric Seidel committed
33
    assert(onShowPerformanceOverlayChanged != null);
34 35
  }

Eric Seidel's avatar
Eric Seidel committed
36
  final bool useLightTheme;
37 38 39 40
  final ValueChanged<bool> onThemeChanged;

  final double timeDilation;
  final ValueChanged<double> onTimeDilationChanged;
41

Eric Seidel's avatar
Eric Seidel committed
42 43 44
  final bool showPerformanceOverlay;
  final ValueChanged<bool> onShowPerformanceOverlayChanged;

45
  @override
46 47 48 49
  GalleryHomeState createState() => new GalleryHomeState();
}

class GalleryHomeState extends State<GalleryHome> {
50
  final Key _homeKey = new ValueKey<String>("Gallery Home");
51
  final List<Widget> _listItems = <Widget>[];
52

53
  @override
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  void initState() {
    super.initState();

    // The first item in the list just exists to occupy the space behind
    // the flexible app bar. As it's scrolled out of the way, the app bar's
    // height will shrink.
    final double statusBarHeight = (MediaQuery.of(context)?.padding ?? EdgeInsets.zero).top;
    _listItems.add(new SizedBox(height: _kFlexibleSpaceMaxHeight + statusBarHeight));

    final ThemeData themeData = Theme.of(context);
    final TextStyle headerStyle = themeData.textTheme.body2.copyWith(color: themeData.primaryColor);
    String category;
    for (GalleryItem galleryItem in kAllGalleryItems) {
      if (category != galleryItem.category) {
        if (category != null)
          _listItems.add(new Divider());
        _listItems.add(
          new Container(
            height: 48.0,
            padding: const EdgeInsets.only(left: 16.0),
            child: new Align(
              alignment: FractionalOffset.centerLeft,
              child: new Text(galleryItem.category, style: headerStyle)
            )
          )
        );
        category = galleryItem.category;
      }
      _listItems.add(galleryItem);
    }
  }
85

86 87
  @override
  Widget build(BuildContext context) {
88
    return new Scaffold(
89
      key: _homeKey,
90
      drawer: new GalleryDrawer(
Eric Seidel's avatar
Eric Seidel committed
91
        useLightTheme: config.useLightTheme,
92 93
        onThemeChanged: config.onThemeChanged,
        timeDilation: config.timeDilation,
Eric Seidel's avatar
Eric Seidel committed
94 95 96
        onTimeDilationChanged: config.onTimeDilationChanged,
        showPerformanceOverlay: config.showPerformanceOverlay,
        onShowPerformanceOverlayChanged: config.onShowPerformanceOverlayChanged
97
      ),
98
      appBar: new AppBar(
99
        expandedHeight: _kFlexibleSpaceMaxHeight,
100
        flexibleSpace: new FlexibleSpaceBar(
101 102
          background: new Image(
            image: new AssetImage('packages/flutter_gallery_assets/appbar_background.jpg'),
103 104 105
            fit: ImageFit.cover,
            height: _kFlexibleSpaceMaxHeight
          ),
106
          title: new Text('Flutter gallery')
107
        )
108
      ),
109
      appBarBehavior: AppBarBehavior.under,
110
      body: new Block(children: _listItems)
111 112 113
    );
  }
}