material.dart 1.7 KB
Newer Older
1 2 3 4 5
// 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.

import 'package:sky/painting/box_painter.dart';
6 7
import 'package:sky/theme/shadows.dart';
import 'package:sky/widgets/animated_container.dart';
8 9 10 11 12 13 14 15 16 17 18 19 20
import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/default_text_style.dart';
import 'package:sky/widgets/theme.dart';

enum MaterialType { canvas, card, circle, button }

const Map<MaterialType, double> edges = const {
  MaterialType.canvas: null,
  MaterialType.card: 2.0,
  MaterialType.circle: null,
  MaterialType.button: 2.0,
};

21
class Material extends Component {
22
  Material({
23
    Key key,
24 25 26 27 28 29 30 31
    this.child,
    this.type: MaterialType.card,
    this.level: 0,
    this.color
  }) : super(key: key) {
    assert(level != null);
  }

32 33 34 35
  final Widget child;
  final MaterialType type;
  final int level;
  final Color color;
36

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

  Widget build() {
51
    return new AnimatedContainer(
52
      behavior: implicitlyAnimate(const Duration(milliseconds: 200)),
53 54 55 56 57 58 59 60 61 62
      decoration: new BoxDecoration(
        backgroundColor: _backgroundColor,
        borderRadius: edges[type],
        boxShadow: level == 0 ? null : shadows[level],
        shape: type == MaterialType.circle ? Shape.circle : Shape.rectangle
      ),
      child: new DefaultTextStyle(
        style: Theme.of(this).text.body1,
        child: child
      )
63 64 65
    );
  }
}