Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. import 'dart:math';
  2.  
  3. import 'package:floop/floop.dart';
  4. import 'package:flutter/material.dart';
  5.  
  6. void main() {
  7. runApp(MaterialApp(title: 'Color boxes', home: ColoredGrid()));
  8. }
  9.  
  10. class Dyn {
  11. static final _dyn = ObservedMap();
  12.  
  13. static int get selectedBoxId => _dyn[#selectedBoxId];
  14. static set selectedBoxId(int id) => _dyn[#selectedBoxId] = id;
  15.  
  16. static List<Widget> get widgets => _dyn[#widgets].cast<Widget>();
  17. static set widgets(List<Widget> newWidgets) => _dyn[#widgets] = newWidgets;
  18.  
  19. static int get totalClicks => _dyn[#totalClicks];
  20. static set totalClicks(int n) => _dyn[#totalClicks] = n;
  21.  
  22. static bool get useRepeatingWidgets => _dyn[#useRepeatingWidgets];
  23. static set useRepeatingWidgets(bool value) =>
  24. _dyn[#useRepeatingWidgets] = value;
  25. }
  26.  
  27. class ColoredGrid extends FloopWidget {
  28. @override
  29. void initContext(BuildContext context) {
  30. Dyn.totalClicks ??= 0;
  31. Dyn.useRepeatingWidgets = true;
  32. createRandomColorBoxList();
  33. }
  34.  
  35. createRandomColorBoxList() {
  36. Dyn.selectedBoxId = null;
  37. final baseWidgets = List.generate(4, (i) => ColorBox(i)..forceInit());
  38. Dyn.widgets = List.generate(50, (i) => baseWidgets[i % 4])..shuffle();
  39. }
  40.  
  41. @override
  42. Widget build(BuildContext context) {
  43. final clicks = Dyn.totalClicks;
  44. return Scaffold(
  45. appBar: AppBar(
  46. title: Text('Total clicks: $clicks'),
  47. ),
  48. body: Dyn.useRepeatingWidgets
  49. ? GridView.count(crossAxisCount: 5, children: Dyn.widgets)
  50. : GridView.count(
  51. crossAxisCount: 4,
  52. children: List.generate(
  53. 20,
  54. (i) => ColorBox(clicks - i, key: ValueKey(clicks - i)),
  55. ),
  56. ),
  57. floatingActionButton: FloatingActionButton(
  58. child: Icon(Icons.replay),
  59. onPressed: () {
  60. createRandomColorBoxList();
  61. Dyn.useRepeatingWidgets =
  62. !Dyn.useRepeatingWidgets || clicks % 2 == 0;
  63. }),
  64. );
  65. }
  66. }
  67.  
  68. class ColorBox extends DynamicWidget {
  69. final int id;
  70. ColorBox(this.id, {Key key}) : super(key: key);
  71.  
  72. Color get color => dyn[#color];
  73. set color(Color c) => dyn[#color] = c;
  74.  
  75. Color get textColor => dyn[#textColor];
  76. set textColor(Color c) => dyn[#textColor] = c;
  77.  
  78. int get clicks => dyn[#clicks];
  79. set clicks(int n) => dyn[#clicks] = n;
  80.  
  81. initDyn() {
  82. color = randomColor();
  83. textColor = contrastColor(color);
  84. clicks = 0;
  85. }
  86.  
  87. @override
  88. Widget build(BuildContext context) {
  89. return RaisedButton(
  90. child: Dyn.selectedBoxId == id
  91. ? Opacity(
  92. opacity: 1 - transition(2000, key: id),
  93. child: Text(
  94. '$clicks',
  95. style: TextStyle(
  96. color: textColor,
  97. fontSize: 26,
  98. ),
  99. ))
  100. : null,
  101. color: color,
  102. onPressed: () {
  103. clicks++;
  104. final switchMode = clicks % 4 == 0;
  105. Dyn.useRepeatingWidgets ^= switchMode;
  106. Dyn.selectedBoxId = switchMode ? null : id;
  107. Dyn.totalClicks++;
  108. Transitions.restart(key: id);
  109. // Note that [Dyn.widgets] is copied into a new list prior to
  110. // shuffling. This is because when [List] instances are stored in an
  111. // [ObservedMap], they are copied and stored as unmodifiable lists.
  112. Dyn.widgets = Dyn.widgets.toList()..shuffle();
  113. },
  114. );
  115. }
  116. }
  117.  
  118. Color contrastColor(Color color) {
  119. const gamma = 2.2;
  120. final sum = 0.2126 * pow(color.red / 255, gamma) +
  121. 0.7152 * pow(color.green / 255, gamma) +
  122. 0.0722 * pow(color.blue / 255, gamma);
  123. return sum > pow(0.5, gamma) ? Colors.black : Colors.white;
  124. }
  125.  
  126. Color randomColor() {
  127. const blend = 0xFA000000;
  128. return Color(Random().nextInt(1 << 32) | blend);
  129. }
  130.  
  131. /// The same ColorBox widget implemented using StatefulWidget.
  132. class ColorBoxStateful extends StatefulWidget with FloopStateful {
  133. final int id;
  134. ColorBoxStateful(this.id, {Key key}) : super(key: key);
  135.  
  136. @override
  137. State<StatefulWidget> createState() => ColorBoxState<ColorBoxStateful>(id);
  138. }
  139.  
  140. class ColorBoxState<ColorBoxStateful> extends State {
  141. final int id;
  142. ColorBoxState(this.id, {Key key});
  143.  
  144. Color color;
  145. Color textColor;
  146. int clicks = 0;
  147.  
  148. @override
  149. void initState() {
  150. clicks = 0;
  151. color = randomColor();
  152. textColor = contrastColor(color);
  153. super.initState();
  154. }
  155.  
  156. @override
  157. Widget build(BuildContext context) {
  158. return RaisedButton(
  159. child: Dyn.selectedBoxId == id
  160. ? Opacity(
  161. opacity: 1 - transition(2000, key: id),
  162. child: Text(
  163. '$clicks',
  164. style: TextStyle(
  165. color: textColor,
  166. fontSize: 26,
  167. ),
  168. ))
  169. : null,
  170. color: color,
  171. onPressed: () {
  172. setState(() => clicks++);
  173. final switchMode = clicks % 4 == 0;
  174. Dyn.useRepeatingWidgets ^= switchMode;
  175. Dyn.selectedBoxId = switchMode ? null : id;
  176. Dyn.totalClicks++;
  177. Transitions.restart(key: id);
  178. Dyn.widgets = Dyn.widgets.toList()..shuffle();
  179. },
  180. );
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement