• Ian Hickson's avatar
    Clean up imports and exports. · a94999ba
    Ian Hickson authored
    Each layer is supposed to reexport the parts of the previous layer
    that are part of its API.
    
    - In painting.dart, export from dart:ui all the Canvas-related APIs
      that make sense to be used at higher levels, e.g. PaintingStyle.
    
    - Delete painting/shadows.dart. It was dead code.
    
    - In rendering/object.dart, export all of painting.dart.
    
    - In widgets/basic.dart, export all of painting.dart and
      animation.dart. Some classes in animation/ are renamed to make this
      less disruptive and confusing to the namespace.
    
    - Split out Stocks back into an import model rather than a part model,
      so that it's easier to manage its dependencies on a per-file basis.
    
    - Move Ticker to scheduler library.
    
    - Remove as many redundant imports as possible now.
    
    - Some minor nit picking cleanup in various files.
    a94999ba
theme.dart 2.53 KB
// 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:flutter/widgets.dart';

import 'theme_data.dart';

export 'theme_data.dart' show ThemeData, ThemeBrightness;

const kThemeAnimationDuration = const Duration(milliseconds: 200);

class Theme extends InheritedWidget {
  Theme({
    Key key,
    this.data,
    Widget child
  }) : super(key: key, child: child) {
    assert(child != null);
    assert(data != null);
  }

  final ThemeData data;

  static final ThemeData _kFallbackTheme = new ThemeData.fallback();

  /// The data from the closest instance of this class that encloses the given context.
  ///
  /// Defaults to the fallback theme data if none exists.
  static ThemeData of(BuildContext context) {
    Theme theme = context.inheritFromWidgetOfExactType(Theme);
    return theme?.data ?? _kFallbackTheme;
  }

  bool updateShouldNotify(Theme old) => data != old.data;

  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('$data');
  }
}

/// An animated value that interpolates [ThemeData]s.
class ThemeDataTween extends Tween<ThemeData> {
  ThemeDataTween({ ThemeData begin, ThemeData end }) : super(begin: begin, end: end);

  ThemeData lerp(double t) => ThemeData.lerp(begin, end, t);
}

/// Animated version of [Theme] which automatically transitions the colours,
/// etc, over a given duration whenever the given theme changes.
class AnimatedTheme extends AnimatedWidgetBase {
  AnimatedTheme({
    Key key,
    this.data,
    Curve curve: Curves.linear,
    Duration duration,
    this.child
  }) : super(key: key, curve: curve, duration: duration) {
    assert(child != null);
    assert(data != null);
  }

  final ThemeData data;

  final Widget child;

  _AnimatedThemeState createState() => new _AnimatedThemeState();
}

class _AnimatedThemeState extends AnimatedWidgetBaseState<AnimatedTheme> {
  ThemeDataTween _data;

  void forEachTween(TweenVisitor visitor) {
    // TODO(ianh): Use constructor tear-offs when it becomes possible
    _data = visitor(_data, config.data, (dynamic value) => new ThemeDataTween(begin: value));
    assert(_data != null);
  }

  Widget build(BuildContext context) {
    return new Theme(
      child: config.child,
      data: _data.evaluate(animation)
    );
  }

  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    if (_data != null)
      description.add('$_data');
  }
}