|
| 1 | +import 'dart:ui'; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter/widgets.dart'; |
| 6 | +import 'package:timelines/src/timelines_theme.dart'; |
| 7 | + |
| 8 | +/// Controls the default color and size of indicators in a widget subtree. |
| 9 | +/// |
| 10 | +/// The timelines indicator theme is honored by [CircleIndicator] and [T O D O] widgets. |
| 11 | +class TimelinesIndicatorTheme extends InheritedTheme { |
| 12 | + /// Creates an timelines indicator theme that controls the color and size of descendant widgets. |
| 13 | + /// |
| 14 | + /// Both [data] and [child] arguments must not be null. |
| 15 | + const TimelinesIndicatorTheme({ |
| 16 | + Key key, |
| 17 | + @required this.data, |
| 18 | + @required Widget child, |
| 19 | + }) : assert(data != null), |
| 20 | + assert(child != null), |
| 21 | + super(key: key, child: child); |
| 22 | + |
| 23 | + /// The color and size to use for timelines indicators in this subtree. |
| 24 | + final TimelinesIndicatorThemeData data; |
| 25 | + |
| 26 | + /// The data from the closest instance of this class that encloses the given |
| 27 | + /// context. |
| 28 | + /// |
| 29 | + /// Defaults to the current [TimelinesThemeData.indicatorTheme]. |
| 30 | + /// |
| 31 | + /// Typical usage is as follows: |
| 32 | + /// |
| 33 | + /// ```dart |
| 34 | + /// TimelinesIndicatorThemeData theme = TimelinesIndicatorTheme.of(context); |
| 35 | + /// ``` |
| 36 | + static TimelinesIndicatorThemeData of(BuildContext context) { |
| 37 | + final TimelinesIndicatorThemeData timelinesIndicatorThemeData = |
| 38 | + _getInheritedTimelinesIndicatorThemeData(context).resolve(context); |
| 39 | + return timelinesIndicatorThemeData.isConcrete |
| 40 | + ? timelinesIndicatorThemeData |
| 41 | + : timelinesIndicatorThemeData.copyWith( |
| 42 | + size: timelinesIndicatorThemeData.size ?? const TimelinesIndicatorThemeData.fallback().size, |
| 43 | + color: timelinesIndicatorThemeData.color ?? const TimelinesIndicatorThemeData.fallback().color, |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + static TimelinesIndicatorThemeData _getInheritedTimelinesIndicatorThemeData(BuildContext context) { |
| 48 | + final TimelinesIndicatorTheme timelinesIndicatorTheme = |
| 49 | + context.dependOnInheritedWidgetOfExactType<TimelinesIndicatorTheme>(); |
| 50 | + return timelinesIndicatorTheme?.data ?? const TimelinesIndicatorThemeData.fallback(); |
| 51 | + } |
| 52 | + |
| 53 | + @override |
| 54 | + bool updateShouldNotify(TimelinesIndicatorTheme oldWidget) => data != oldWidget.data; |
| 55 | + |
| 56 | + @override |
| 57 | + Widget wrap(BuildContext context, Widget child) { |
| 58 | + final TimelinesIndicatorTheme iconTheme = context.findAncestorWidgetOfExactType<TimelinesIndicatorTheme>(); |
| 59 | + return identical(this, iconTheme) ? child : TimelinesIndicatorTheme(data: data, child: child); |
| 60 | + } |
| 61 | + |
| 62 | + @override |
| 63 | + void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
| 64 | + super.debugFillProperties(properties); |
| 65 | + data.debugFillProperties(properties); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Defines the color and size of indicators. |
| 70 | +/// |
| 71 | +/// Used by [TimelinesIndicatorTheme] to control the color and size of indicators in a |
| 72 | +/// widget subtree. |
| 73 | +/// |
| 74 | +/// To obtain the current timelines indicator theme, use [TimelinesIndicatorTheme.of]. To convert an timelines indicator |
| 75 | +/// theme to a version with all the fields filled in, use [new TimelinesIndicatorThemeData.fallback]. |
| 76 | +@immutable |
| 77 | +class TimelinesIndicatorThemeData with Diagnosticable { |
| 78 | + /// Creates an timelines indicator theme data. |
| 79 | + const TimelinesIndicatorThemeData({this.color, this.size}); |
| 80 | + |
| 81 | + /// Creates an timelines indicator them with some reasonable default values. |
| 82 | + /// |
| 83 | + /// The [color] is blue and the [size] is 15.0. |
| 84 | + const TimelinesIndicatorThemeData.fallback() |
| 85 | + : color = Colors.blue, |
| 86 | + size = 15.0; |
| 87 | + |
| 88 | + /// Creates a copy of this timelines indicator theme but with the given fields replaced with |
| 89 | + /// the new values. |
| 90 | + TimelinesIndicatorThemeData copyWith({Color color, double opacity, double size}) { |
| 91 | + return TimelinesIndicatorThemeData( |
| 92 | + color: color ?? this.color, |
| 93 | + size: size ?? this.size, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /// Called by [TimelinesIndicatorTheme.of] to convert this instance to an [TimelinesINdicatorThemeData] |
| 98 | + /// that fits the given [BuildContext]. |
| 99 | + /// |
| 100 | + /// This method gives the ambient [TimelinesIndicatorThemeData] a chance to update itself, |
| 101 | + /// after it's been retrieved by [TimelinesIndicatorTheme.of], and before being returned as |
| 102 | + /// the final result. |
| 103 | + /// |
| 104 | + /// The default implementation returns this [TimelinesIndicatorThemeData] as-is. |
| 105 | + TimelinesIndicatorThemeData resolve(BuildContext context) => this; |
| 106 | + |
| 107 | + /// Whether all the properties of this object are non-null. |
| 108 | + bool get isConcrete => color != null && size != null; |
| 109 | + |
| 110 | + /// The default color for indicators. |
| 111 | + final Color color; |
| 112 | + |
| 113 | + /// The default size for indicators. |
| 114 | + final double size; |
| 115 | + |
| 116 | + /// Linearly interpolate between two themes. |
| 117 | + /// |
| 118 | + /// {@macro dart.ui.shadow.lerp} |
| 119 | + static TimelinesIndicatorThemeData lerp(TimelinesIndicatorThemeData a, TimelinesIndicatorThemeData b, double t) { |
| 120 | + assert(t != null); |
| 121 | + return TimelinesIndicatorThemeData( |
| 122 | + color: Color.lerp(a?.color, b?.color, t), |
| 123 | + size: lerpDouble(a?.size, b?.size, t), |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + @override |
| 128 | + bool operator ==(Object other) { |
| 129 | + if (other.runtimeType != runtimeType) return false; |
| 130 | + return other is TimelinesIndicatorThemeData && other.color == color && other.size == size; |
| 131 | + } |
| 132 | + |
| 133 | + @override |
| 134 | + int get hashCode => hashValues(color, size); |
| 135 | + |
| 136 | + @override |
| 137 | + void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
| 138 | + super.debugFillProperties(properties); |
| 139 | + properties.add(ColorProperty('color', color, defaultValue: null, level: DiagnosticLevel.debug)); |
| 140 | + properties.add(DoubleProperty('size', size, defaultValue: null, level: DiagnosticLevel.debug)); |
| 141 | + } |
| 142 | +} |
0 commit comments