diff --git a/lib/activities/cross.dart b/lib/activities/cross.dart index f0c2dd1..4e86c01 100644 --- a/lib/activities/cross.dart +++ b/lib/activities/cross.dart @@ -1,30 +1,33 @@ +import "package:cross_array_task_app/model/interpreter/cat_interpreter.dart"; +import "package:cross_array_task_app/model/schemas/schemas_reader.dart"; import "package:cross_array_task_app/utility/helper.dart"; import "package:cross_array_task_app/utility/result_notifier.dart"; +import "package:cross_array_task_app/utility/visibility_notifier.dart"; import "package:flutter/cupertino.dart"; import "package:flutter/material.dart"; import "package:interpreter/cat_interpreter.dart"; +import "package:provider/provider.dart"; /// It's a stateful widget that displays the result of the interpreter class CrossWidgetSimple extends StatefulWidget { /// A constructor. const CrossWidgetSimple({ - required this.resultValueNotifier, + required this.reference, this.displayLetters = false, super.key, - }); + }) : resultValueNotifier = null; CrossWidgetSimple.fromBasicShape({ required BasicShape shape, this.displayLetters = false, - super.key, + this.reference = false, }) : resultValueNotifier = ResultNotifier() { - resultValueNotifier.cross = shape; + resultValueNotifier?.cross = shape; } - /// A variable that is used to store the result of the interpreter. - final ResultNotifier resultValueNotifier; - final bool displayLetters; + final bool reference; + final ResultNotifier? resultValueNotifier; @override State createState() => CrossWidgetSimpleState(); @@ -70,27 +73,51 @@ class CrossWidgetSimpleState extends State { 6, (int i) => List.filled(6, _buildDummy()), ); + if (widget.resultValueNotifier != null) { + for (int y = 0; y < buttons.length; y++) { + if ([0, 1, 4, 5].contains(y)) { + buttons[y][2] = _buttonBuilderFromColor( + widget.resultValueNotifier!.cross.getGrid[y][2], + ); + buttons[y][3] = _buttonBuilderFromColor( + widget.resultValueNotifier!.cross.getGrid[y][3], + ); + } else { + for (int x = 0; x < buttons[y].length; x++) { + buttons[y][x] = _buttonBuilderFromColor( + widget.resultValueNotifier!.cross.getGrid[y][x], + ); + } + } + } + } else if (widget.reference) { + for (int y = 0; y < buttons.length; y++) { + if ([0, 1, 4, 5].contains(y)) { + buttons[y][2] = _buttonBuilderReference(y, 2); + buttons[y][3] = _buttonBuilderReference(y, 3); + } else { + for (int x = 0; x < buttons[y].length; x++) { + buttons[y][x] = _buttonBuilderReference(y, x); + } + } + } + } else { + for (int y = 0; y < buttons.length; y++) { + if ([0, 1, 4, 5].contains(y)) { + buttons[y][2] = _buttonBuilder(y, 2); + buttons[y][3] = _buttonBuilder(y, 3); + } else { + for (int x = 0; x < buttons[y].length; x++) { + buttons[y][x] = _buttonBuilder(y, x); + } + } + } + } return SizedBox( width: widgetDimension, height: widgetDimension, - child: AnimatedBuilder( - animation: widget.resultValueNotifier, - builder: (BuildContext context, Widget? child) { - for (int y = 0; y < buttons.length; y++) { - if ([0, 1, 4, 5].contains(y)) { - buttons[y][2] = _buttonBuilder(y, 2); - buttons[y][3] = _buttonBuilder(y, 3); - } else { - for (int x = 0; x < buttons[y].length; x++) { - buttons[y][x] = _buttonBuilder(y, x); - } - } - } - - return Flex(direction: Axis.vertical, children: _buildCross()); - }, - ), + child: Flex(direction: Axis.vertical, children: _buildCross()), ); } @@ -117,17 +144,55 @@ class CrossWidgetSimpleState extends State { return result; } - Widget _buttonBuilder(int y, int x) => Container( + Widget _buttonBuilderFromColor(int color) => Container( width: containerDimension, height: containerDimension, decoration: BoxDecoration( - color: colors[widget.resultValueNotifier.cross.getGrid[y][x]], + color: colors[color], borderRadius: const BorderRadius.all(Radius.circular(100)), ), - child: Center( - child: widget.displayLetters - ? Text("${rows[y]!.toUpperCase()}${x + 1}") - : const Text(""), + child: const Center( + child: Text(""), + ), + ); + + Widget _buttonBuilderReference(int y, int x) => AnimatedBuilder( + animation: context.watch(), + builder: (_, __) => Container( + width: containerDimension, + height: containerDimension, + decoration: BoxDecoration( + color: colors[SchemasReader().current.getGrid[y][x]], + borderRadius: const BorderRadius.all(Radius.circular(100)), + ), + child: Center( + child: widget.displayLetters + ? Text("${rows[y]!.toUpperCase()}${x + 1}") + : const Text(""), + ), + ), + ); + + Widget _buttonBuilder(int y, int x) => AnimatedBuilder( + animation: context.watch(), + builder: (_, __) => AnimatedBuilder( + animation: CatInterpreter(), + builder: (_, __) => Container( + width: containerDimension, + height: containerDimension, + decoration: BoxDecoration( + color: context.read().visible + ? colors[CatInterpreter().getLastState.getGrid[y][x]] + : colors[0], + borderRadius: const BorderRadius.all(Radius.circular(100)), + ), + child: Center( + child: widget.displayLetters && + context.read().visible + ? Text("${rows[y]!.toUpperCase()}${x + 1}") + : const Text(""), + ), + ), ), ); diff --git a/lib/activities/gesture_based/model/cross_button.dart b/lib/activities/gesture_based/model/cross_button.dart index e66cb57..0450cc6 100644 --- a/lib/activities/gesture_based/model/cross_button.dart +++ b/lib/activities/gesture_based/model/cross_button.dart @@ -9,6 +9,7 @@ import "package:cross_array_task_app/utility/helper.dart"; import "package:cross_array_task_app/utility/localizations.dart"; import "package:cross_array_task_app/utility/result_notifier.dart"; import "package:cross_array_task_app/utility/selected_colors_notifier.dart"; +import "package:cross_array_task_app/utility/visibility_notifier.dart"; import "package:dartx/dartx.dart"; import "package:flutter/cupertino.dart"; import "package:interpreter/cat_interpreter.dart"; @@ -152,60 +153,67 @@ class CrossButtonState extends State { dimension = MediaQuery.of(context).size.width / 13; return AnimatedBuilder( - animation: widget.resultValueNotifier, - builder: (BuildContext context, Widget? child) { - if (!t.isActive) { - buttonColor = colors[widget.resultValueNotifier.cross - .getGrid[widget.position.first][widget.position.second]]; - } - - return Padding( - padding: EdgeInsets.all(dimension / 10), - child: CupertinoButton( - pressedOpacity: 1, - onPressed: () async { - if (widget.selectionMode.value == SelectionModes.transition) { - widget.shakeKey.currentState?.shake(); - - return; - } - if (widget.selectionMode.value == SelectionModes.select || - widget.selectionMode.value == - SelectionModes.selectCopyCells) { - if (CatInterpreter().copyCommandsBuffer.isEmpty) { - _selection(); - } else { - _selection2(); - } + animation: context.watch(), + builder: (_, __) => AnimatedBuilder( + animation: CatInterpreter(), + builder: (_, __) { + if (!t.isActive) { + buttonColor = context.read().visible + ? colors[CatInterpreter() + .getLastState + .getGrid[widget.position.first][widget.position.second]] + : colors[0]; + } - return; - } else if (widget.selectionMode.value == - SelectionModes.multiple) { - if (context.read().isEmpty) { - _selectionMultiple(); - } else { + return Padding( + padding: EdgeInsets.all(dimension / 10), + child: CupertinoButton( + pressedOpacity: 1, + onPressed: () async { + if (widget.selectionMode.value == SelectionModes.transition) { widget.shakeKey.currentState?.shake(); + + return; + } + if (widget.selectionMode.value == SelectionModes.select || + widget.selectionMode.value == + SelectionModes.selectCopyCells) { + if (CatInterpreter().copyCommandsBuffer.isEmpty) { + _selection(); + } else { + _selection2(); + } + + return; + } else if (widget.selectionMode.value == + SelectionModes.multiple) { + if (context.read().isEmpty) { + _selectionMultiple(); + } else { + widget.shakeKey.currentState?.shake(); + } + + return; } + if (widget.selectionMode.value == SelectionModes.repeat) { + await _normalColoring( + selected: + widget.selectionMode.value == SelectionModes.repeat, + ); - return; - } - if (widget.selectionMode.value == SelectionModes.repeat) { - await _normalColoring( - selected: widget.selectionMode.value == SelectionModes.repeat, - ); - - return; - } - await _normalColoring(); - }, - borderRadius: BorderRadius.circular(100), - minSize: dimension, - color: buttonColor, - padding: EdgeInsets.zero, - child: _widget(), - ), - ); - }, + return; + } + await _normalColoring(); + }, + borderRadius: BorderRadius.circular(100), + minSize: dimension, + color: buttonColor, + padding: EdgeInsets.zero, + child: _widget(), + ), + ); + }, + ), ); } diff --git a/lib/activities/gesture_based/side_bar.dart b/lib/activities/gesture_based/side_bar.dart index e2812e5..4df43a1 100644 --- a/lib/activities/gesture_based/side_bar.dart +++ b/lib/activities/gesture_based/side_bar.dart @@ -50,31 +50,6 @@ class SideBar extends StatefulWidget { } class _SideBarState extends State { - bool added = false; - - void _interpreterListener() { - if (!mounted) { - return; - } - if (context.read().visible) { - context.read().cross = - CatInterpreter().getLastState as Cross; - if (!added) { - CatInterpreter().addListener(_interpreterListener); - added = !added; - } - } else { - CatInterpreter().removeListener(_interpreterListener); - added = !added; - } - } - - @override - void initState() { - context.read().addListener(_interpreterListener); - super.initState(); - } - @override Widget build(BuildContext context) => Container( height: MediaQuery.of(context).size.height * 0.90, @@ -243,8 +218,8 @@ class _SideBarState extends State { }, ), CrossWidgetSimple( + reference: true, displayLetters: context.read().state > 0, - resultValueNotifier: context.watch(), ), Column( children: [ @@ -255,10 +230,9 @@ class _SideBarState extends State { height: 55, ), CrossWidgetSimple( + reference: false, displayLetters: - context.read().state > 0 && - context.read().visible, - resultValueNotifier: context.watch(), + context.read().state > 0, ), ], ), @@ -302,10 +276,4 @@ class _SideBarState extends State { description: CatLoggingLevel.commandsReset, ); } - - @override - void dispose() { - CatInterpreter().removeListener(_interpreterListener); - super.dispose(); - } }