widgets.dart 4.26 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12
// 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 'sections.dart';

const double kSectionIndicatorWidth = 32.0;

// The card for a single section. Displays the section's gradient and background image.
class SectionCard extends StatelessWidget {
13 14
  const SectionCard({ Key? key, required this.section })
    : super(key: key);
15 16 17 18 19

  final Section section;

  @override
  Widget build(BuildContext context) {
20
    return Semantics(
21 22
      label: section.title,
      button: true,
23 24 25
      child: DecoratedBox(
        decoration: BoxDecoration(
          gradient: LinearGradient(
26 27 28
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
            colors: <Color>[
29 30
              section.leftColor!,
              section.rightColor!,
31 32 33
            ],
          ),
        ),
34
        child: Image.asset(
35
          section.backgroundAsset!,
36 37 38 39
          package: section.backgroundAssetPackage,
          color: const Color.fromRGBO(255, 255, 255, 0.075),
          colorBlendMode: BlendMode.modulate,
          fit: BoxFit.cover,
40
        ),
41
      ),
42 43 44 45 46 47 48
    );
  }
}

// The title is rendered with two overlapping text widgets that are vertically
// offset a little. It's supposed to look sort-of 3D.
class SectionTitle extends StatelessWidget {
49
  const SectionTitle({
50 51 52 53 54
    Key? key,
    required this.section,
    required this.scale,
    required this.opacity,
  }) : assert(opacity >= 0.0 && opacity <= 1.0),
55
       super(key: key);
56 57 58 59 60

  final Section section;
  final double scale;
  final double opacity;

61 62 63 64 65 66 67 68 69 70 71 72 73
  static const TextStyle sectionTitleStyle = TextStyle(
    fontFamily: 'Raleway',
    inherit: false,
    fontSize: 24.0,
    fontWeight: FontWeight.w500,
    color: Colors.white,
    textBaseline: TextBaseline.alphabetic,
  );

  static final TextStyle sectionTitleShadowStyle = sectionTitleStyle.copyWith(
    color: const Color(0x19000000),
  );

74 75
  @override
  Widget build(BuildContext context) {
76 77
    return IgnorePointer(
      child: Opacity(
78
        opacity: opacity,
79 80
        child: Transform(
          transform: Matrix4.identity()..scale(scale),
81
          alignment: Alignment.center,
82
          child: Stack(
83
            children: <Widget>[
84
              Positioned(
85
                top: 4.0,
86
                child: Text(section.title!, style: sectionTitleShadowStyle),
87
              ),
88
              Text(section.title!, style: sectionTitleStyle),
89 90 91 92 93 94 95 96 97 98
            ],
          ),
        ),
      ),
    );
  }
}

// Small horizontal bar that indicates the selected section.
class SectionIndicator extends StatelessWidget {
99
  const SectionIndicator({ Key? key, this.opacity = 1.0 }) : super(key: key);
100 101 102 103 104

  final double opacity;

  @override
  Widget build(BuildContext context) {
105 106
    return IgnorePointer(
      child: Container(
107 108
        width: kSectionIndicatorWidth,
        height: 3.0,
109
        color: Colors.white.withOpacity(opacity),
110 111 112 113 114 115 116
      ),
    );
  }
}

// Display a single SectionDetail.
class SectionDetailView extends StatelessWidget {
117 118
  SectionDetailView({ Key? key, required this.detail })
    : assert(detail.imageAsset != null),
119 120
      assert((detail.imageAsset ?? detail.title) != null),
      super(key: key);
121 122 123 124 125

  final SectionDetail detail;

  @override
  Widget build(BuildContext context) {
126 127 128 129 130
    final Widget image = DecoratedBox(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(6.0),
        image: DecorationImage(
          image: AssetImage(
131
            detail.imageAsset!,
132 133
            package: detail.imageAssetPackage,
          ),
134
          fit: BoxFit.cover,
135
          alignment: Alignment.center,
136 137 138 139 140 141
        ),
      ),
    );

    Widget item;
    if (detail.title == null && detail.subtitle == null) {
142
      item = Container(
143 144
        height: 240.0,
        padding: const EdgeInsets.all(16.0),
145
        child: SafeArea(
146 147 148 149
          top: false,
          bottom: false,
          child: image,
        ),
150 151
      );
    } else {
152
      item = ListTile(
153 154
        title: Text(detail.title!),
        subtitle: Text(detail.subtitle!),
155
        leading: SizedBox(width: 32.0, height: 32.0, child: image),
156 157 158
      );
    }

159 160
    return DecoratedBox(
      decoration: BoxDecoration(color: Colors.grey.shade200),
161 162 163 164
      child: item,
    );
  }
}