datetime_picker_theme_data.dart 6.2 KB

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