Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3.  
  4. import 'dart:ui' show lerpDouble;
  5.  
  6. class FooThemeData extends Diagnosticable {
  7. const FooThemeData({
  8. this.color,
  9. this.shape,
  10. });
  11.  
  12. final Color color;
  13. final ShapeBorder shape;
  14.  
  15. FooThemeData copyWith({
  16. Color color,
  17. ShapeBorder shape,
  18. }) {
  19. return FooThemeData(
  20. color: color ?? this.color,
  21. shape: shape ?? this.shape,
  22. );
  23. }
  24.  
  25. static FooThemeData lerp(FooThemeData a, FooThemeData b, double t) {
  26. assert(t != null);
  27. if (a == null && b == null)
  28. return null;
  29. return FooThemeData(
  30. color: Color.lerp(a?.color, b?.color, t),
  31. shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
  32. );
  33. }
  34.  
  35. @override
  36. int get hashCode {
  37. return hashValues(
  38. color,
  39. shape,
  40. );
  41. }
  42.  
  43. @override
  44. bool operator ==(Object other) {
  45. if (identical(this, other))
  46. return true;
  47. if (other.runtimeType != runtimeType)
  48. return false;
  49. final FooThemeData typedOther = other;
  50. return typedOther.color == color
  51. && typedOther.shape == shape;
  52. }
  53.  
  54. @override
  55. void debugFillProperties(DiagnosticPropertiesBuilder properties) {
  56. super.debugFillProperties(properties);
  57. properties.add(ColorProperty('color', color, defaultValue: null));
  58. properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
  59. }
  60. }
  61.  
  62. class FooTheme extends InheritedWidget {
  63. FooTheme({
  64. Key key,
  65. Color color,
  66. ShapeBorder shape,
  67. Widget child,
  68. }) : data = FooThemeData(
  69. color: color,
  70. shape: shape,
  71. ),
  72. super(key: key, child: child);
  73.  
  74. final FooThemeData data;
  75.  
  76. static FooThemeData of(BuildContext context) {
  77. FooTheme fooTheme = context.inheritFromWidgetOfExactType(FooTheme);
  78. return fooTheme?.data ?? Theme.of(context).fooThemeData;
  79. }
  80.  
  81. @override
  82. bool updateShouldNotify(FooTheme oldWidget) => data != oldWidget.data;
  83. }
  84.  
  85. /*
  86. ThemeData gets a new field and all of the related plumbing:
  87.  
  88. final FooThemeData fooTheme;
  89.  
  90. Which is initialized like this:
  91.  
  92. fooTheme ??= const FooThemeData();
  93.  
  94. */
  95.  
  96.  
  97. class Foo extends StatelessWidget {
  98. const Foo({ Key key, this.color, this.shape }) : super(key: key);
  99.  
  100. final Color color;
  101. final ShapeBorder shape;
  102.  
  103. @override
  104. Widget build(BuildContext context) {
  105. final TextTheme textTheme = Theme.of(context).textTheme;
  106. final ColorScheme colorScheme = Theme.of(context).colorScheme;
  107. final FooThemeData fooTheme = FooTheme.of(context);
  108. return Scaffold(
  109. body: Center(
  110. child: SizedBox(
  111. width: 300,
  112. height: 300,
  113. child: Card(
  114. color: color ?? fooTheme.color ?? colorScheme.primary,
  115. shape: shape ?? fooTheme.shape,
  116. child: Center(
  117. child: Text(
  118. 'Hello Word',
  119. style: textTheme.display1.copyWith(color: colorScheme.onPrimary),
  120. ),
  121. ),
  122. ),
  123. ),
  124. ),
  125. );
  126. }
  127. }
  128.  
  129. void main() {
  130. runApp(MaterialApp(home: FooTheme(color: Colors.purple, child: Foo())));
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement