material.dart 2.12 KB
Newer Older
1 2 3 4
// Copyright 2015 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 6
import 'package:flutter/animation.dart';
import 'package:flutter/widgets.dart';
7

8 9
import 'constants.dart';
import 'shadows.dart';
10
import 'theme.dart';
11 12 13

enum MaterialType { canvas, card, circle, button }

14
const Map<MaterialType, double> _kEdges = const <MaterialType, double>{
15 16 17 18 19 20
  MaterialType.canvas: null,
  MaterialType.card: 2.0,
  MaterialType.circle: null,
  MaterialType.button: 2.0,
};

21
class Material extends StatelessComponent {
22
  Material({
23
    Key key,
24
    this.child,
25
    this.type: MaterialType.canvas,
Hans Muller's avatar
Hans Muller committed
26
    this.elevation: 0,
27 28
    this.color,
    this.textStyle
29
  }) : super(key: key) {
Hans Muller's avatar
Hans Muller committed
30
    assert(elevation != null);
31 32
  }

33 34
  final Widget child;
  final MaterialType type;
Hans Muller's avatar
Hans Muller committed
35
  final int elevation;
36
  final Color color;
37
  final TextStyle textStyle;
38

39
  Color _getBackgroundColor(BuildContext context) {
40 41 42 43
    if (color != null)
      return color;
    switch (type) {
      case MaterialType.canvas:
44
        return Theme.of(context).canvasColor;
45
      case MaterialType.card:
46
        return Theme.of(context).cardColor;
47 48
      default:
        return null;
49 50 51
    }
  }

52
  Widget build(BuildContext context) {
53 54 55
    Widget contents = child;
    if (child != null) {
      contents = new DefaultTextStyle(
56
        style: textStyle ?? Theme.of(context).text.body1,
57 58
        child: contents
      );
59
      if (_kEdges[type] != null) {
60
        contents = new ClipRRect(
61 62
          xRadius: _kEdges[type],
          yRadius: _kEdges[type],
63 64 65 66
          child: contents
        );
      }
    }
67 68
    return new DefaultTextStyle(
      style: Theme.of(context).text.body1,
Adam Barth's avatar
Adam Barth committed
69
      child: new AnimatedContainer(
70
        curve: Curves.ease,
71
        duration: kThemeChangeDuration,
72
        decoration: new BoxDecoration(
73 74
          backgroundColor: _getBackgroundColor(context),
          borderRadius: _kEdges[type],
Hans Muller's avatar
Hans Muller committed
75
          boxShadow: elevation == 0 ? null : elevationToShadow[elevation],
76 77 78 79
          shape: type == MaterialType.circle ? Shape.circle : Shape.rectangle
        ),
        child: contents
      )
80 81 82
    );
  }
}