datetime_picker_theme_data.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. class DatePickerThemeData extends Diagnosticable {
  4. /// Whether null values are replaced with their value in an ancestor text
  5. /// style (e.g., in a [TextSpan] tree).
  6. ///
  7. /// If this is false, properties that don't have explicit values will revert
  8. /// to the defaults: white in color, a font size of 10 pixels, in a sans-serif
  9. /// font face.
  10. final bool inherit;
  11. final TextStyle cancelStyle;
  12. final TextStyle doneStyle;
  13. final TextStyle itemStyle;
  14. final Color backgroundColor;
  15. final Color barrierColor;
  16. final double containerHeight;
  17. final double titleHeight;
  18. final double itemHeight;
  19. const DatePickerThemeData({
  20. this.inherit,
  21. this.cancelStyle,
  22. this.doneStyle,
  23. this.itemStyle,
  24. this.backgroundColor,
  25. this.barrierColor,
  26. this.containerHeight = 210.0,
  27. this.titleHeight = 44.0,
  28. this.itemHeight = 36.0,
  29. });
  30. /// Creates a copy of this theme data but with the given fields replaced with
  31. /// the new values.
  32. DatePickerThemeData copyWith({
  33. TextStyle cancelStyle,
  34. TextStyle doneStyle,
  35. TextStyle itemStyle,
  36. Color backgroundColor,
  37. Color barrierColor,
  38. double pickerHeight,
  39. double pickerTitleHeight,
  40. double pickerItemHeight,
  41. }) {
  42. return DatePickerThemeData(
  43. inherit: inherit,
  44. cancelStyle: cancelStyle != null ? cancelStyle.merge(this.cancelStyle) : this.cancelStyle,
  45. doneStyle: doneStyle != null ? doneStyle.merge(this.doneStyle) : this.doneStyle,
  46. itemStyle: itemStyle != null ? itemStyle.merge(this.itemStyle) : this.itemStyle,
  47. backgroundColor: backgroundColor ?? this.backgroundColor,
  48. barrierColor: barrierColor ?? this.barrierColor,
  49. containerHeight: pickerHeight ?? this.containerHeight,
  50. titleHeight: pickerTitleHeight ?? this.titleHeight,
  51. itemHeight: pickerItemHeight ?? this.itemHeight,
  52. );
  53. }
  54. /// Returns a new theme data that is a combination of this style and the given
  55. /// [other] style.
  56. ///
  57. /// If the given [other] theme data has its [DatePickerThemeData.inherit] set to true,
  58. /// its null properties are replaced with the non-null properties of this text
  59. /// style. The [other] style _inherits_ the properties of this style. Another
  60. /// way to think of it is that the "missing" properties of the [other] style
  61. /// are _filled_ by the properties of this style.
  62. ///
  63. /// If the given [other] theme data has its [DatePickerThemeData.inherit] set to false,
  64. /// returns the given [other] style unchanged. The [other] style does not
  65. /// inherit properties of this style.
  66. ///
  67. /// If the given theme data is null, returns this theme data.
  68. DatePickerThemeData merge(DatePickerThemeData other) {
  69. if (other == null) return this;
  70. if (!other.inherit) return other;
  71. return copyWith(
  72. cancelStyle: other.cancelStyle,
  73. doneStyle: other.doneStyle,
  74. itemStyle: other.itemStyle,
  75. backgroundColor: other.backgroundColor,
  76. barrierColor: other.barrierColor,
  77. pickerHeight: other.containerHeight,
  78. pickerTitleHeight: other.titleHeight,
  79. pickerItemHeight: other.itemHeight,
  80. );
  81. }
  82. }
  83. /// The theme data to apply to descendant [DatePicker] widgets without explicit style.
  84. class DefaultDatePickerThemeData extends InheritedWidget {
  85. /// Creates a default theme data for the given subtree.
  86. ///
  87. /// Consider using [DefaultDatePickerThemeData.merge] to inherit styling information
  88. /// from the current default theme data for a given [BuildContext].
  89. ///
  90. const DefaultDatePickerThemeData({
  91. Key key,
  92. @required this.theme,
  93. @required Widget child,
  94. }) : assert(theme != null),
  95. assert(child != null),
  96. super(key: key, child: child);
  97. /// A const-constructible default theme data that provides fallback values.
  98. ///
  99. /// Returned from [of] when the given [BuildContext] doesn't have an enclosing default theme data.
  100. ///
  101. /// This constructor creates a [DefaultDatePickerThemeData] that lacks a [child], which
  102. /// means the constructed value cannot be incorporated into the tree.
  103. const DefaultDatePickerThemeData.fallback() : theme = const DatePickerThemeData();
  104. /// Creates a default theme data that overrides the theme datas in scope at
  105. /// this point in the widget tree.
  106. ///
  107. /// The given [style] is merged with the [style] from the default theme data
  108. /// for the [BuildContext] where the widget is inserted, and any of the other
  109. /// arguments that are not null replace the corresponding properties on that
  110. /// same default theme data.
  111. ///
  112. /// This constructor cannot be used to override the [maxLines] property of the
  113. /// ancestor with the value null, since null here is used to mean "defer to
  114. /// ancestor". To replace a non-null [maxLines] from an ancestor with the null
  115. /// value (to remove the restriction on number of lines), manually obtain the
  116. /// ambient [DefaultDatePickerThemeData] using [DefaultDatePickerThemeData.of], then create a new
  117. /// [DefaultDatePickerThemeData] using the [new DefaultDatePickerThemeData] constructor directly.
  118. /// See the source below for an example of how to do this (since that's
  119. /// essentially what this constructor does).
  120. static Widget merge({
  121. Key key,
  122. DatePickerThemeData theme,
  123. @required Widget child,
  124. }) {
  125. assert(child != null);
  126. return Builder(
  127. builder: (BuildContext context) {
  128. final DefaultDatePickerThemeData parent = DefaultDatePickerThemeData.of(context);
  129. return DefaultDatePickerThemeData(
  130. key: key,
  131. theme: parent.theme.merge(theme),
  132. child: child,
  133. );
  134. },
  135. );
  136. }
  137. /// The theme data to apply.
  138. final DatePickerThemeData theme;
  139. /// The closest instance of this class that encloses the given context.
  140. ///
  141. /// If no such instance exists, returns an instance created by
  142. /// [DefaultDatePickerThemeData.fallback], which contains fallback values.
  143. ///
  144. /// Typical usage is as follows:
  145. ///
  146. /// ```dart
  147. /// DefaultDatePickerThemeData style = DefaultDatePickerThemeData.of(context);
  148. /// ```
  149. static DefaultDatePickerThemeData of(BuildContext context) {
  150. return context.inheritFromWidgetOfExactType(DefaultDatePickerThemeData) ?? const DefaultDatePickerThemeData.fallback();
  151. }
  152. @override
  153. bool updateShouldNotify(DefaultDatePickerThemeData oldWidget) {
  154. return theme != oldWidget.theme;
  155. }
  156. @override
  157. void debugFillProperties(DiagnosticPropertiesBuilder properties) {
  158. super.debugFillProperties(properties);
  159. theme?.debugFillProperties(properties);
  160. }
  161. }