Skip to content

Commit

Permalink
Solved incorrect visualization of the cross when made visible
Browse files Browse the repository at this point in the history
  • Loading branch information
karpev98 committed May 28, 2023
1 parent 1141b6a commit a526e41
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 115 deletions.
125 changes: 95 additions & 30 deletions lib/activities/cross.dart
Original file line number Diff line number Diff line change
@@ -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<StatefulWidget> createState() => CrossWidgetSimpleState();
Expand Down Expand Up @@ -70,27 +73,51 @@ class CrossWidgetSimpleState extends State<CrossWidgetSimple> {
6,
(int i) => List<Widget>.filled(6, _buildDummy()),
);
if (widget.resultValueNotifier != null) {
for (int y = 0; y < buttons.length; y++) {
if (<int>[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 (<int>[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 (<int>[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 (<int>[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()),
);
}

Expand All @@ -117,17 +144,55 @@ class CrossWidgetSimpleState extends State<CrossWidgetSimple> {
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<ReferenceNotifier>(),
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<VisibilityNotifier>(),
builder: (_, __) => AnimatedBuilder(
animation: CatInterpreter(),
builder: (_, __) => Container(
width: containerDimension,
height: containerDimension,
decoration: BoxDecoration(
color: context.read<VisibilityNotifier>().visible
? colors[CatInterpreter().getLastState.getGrid[y][x]]
: colors[0],
borderRadius: const BorderRadius.all(Radius.circular(100)),
),
child: Center(
child: widget.displayLetters &&
context.read<VisibilityNotifier>().visible
? Text("${rows[y]!.toUpperCase()}${x + 1}")
: const Text(""),
),
),
),
);

Expand Down
108 changes: 58 additions & 50 deletions lib/activities/gesture_based/model/cross_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -152,60 +153,67 @@ class CrossButtonState extends State<CrossButton> {
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<VisibilityNotifier>(),
builder: (_, __) => AnimatedBuilder(
animation: CatInterpreter(),
builder: (_, __) {
if (!t.isActive) {
buttonColor = context.read<VisibilityNotifier>().visible
? colors[CatInterpreter()
.getLastState
.getGrid[widget.position.first][widget.position.second]]
: colors[0];
}

return;
} else if (widget.selectionMode.value ==
SelectionModes.multiple) {
if (context.read<SelectedColorsNotifier>().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<SelectedColorsNotifier>().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(),
),
);
},
),
);
}

Expand Down
38 changes: 3 additions & 35 deletions lib/activities/gesture_based/side_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,6 @@ class SideBar extends StatefulWidget {
}

class _SideBarState extends State<SideBar> {
bool added = false;

void _interpreterListener() {
if (!mounted) {
return;
}
if (context.read<VisibilityNotifier>().visible) {
context.read<ResultNotifier>().cross =
CatInterpreter().getLastState as Cross;
if (!added) {
CatInterpreter().addListener(_interpreterListener);
added = !added;
}
} else {
CatInterpreter().removeListener(_interpreterListener);
added = !added;
}
}

@override
void initState() {
context.read<VisibilityNotifier>().addListener(_interpreterListener);
super.initState();
}

@override
Widget build(BuildContext context) => Container(
height: MediaQuery.of(context).size.height * 0.90,
Expand Down Expand Up @@ -243,8 +218,8 @@ class _SideBarState extends State<SideBar> {
},
),
CrossWidgetSimple(
reference: true,
displayLetters: context.read<TypeUpdateNotifier>().state > 0,
resultValueNotifier: context.watch<ReferenceNotifier>(),
),
Column(
children: <Widget>[
Expand All @@ -255,10 +230,9 @@ class _SideBarState extends State<SideBar> {
height: 55,
),
CrossWidgetSimple(
reference: false,
displayLetters:
context.read<TypeUpdateNotifier>().state > 0 &&
context.read<VisibilityNotifier>().visible,
resultValueNotifier: context.watch<ResultNotifier>(),
context.read<TypeUpdateNotifier>().state > 0,
),
],
),
Expand Down Expand Up @@ -302,10 +276,4 @@ class _SideBarState extends State<SideBar> {
description: CatLoggingLevel.commandsReset,
);
}

@override
void dispose() {
CatInterpreter().removeListener(_interpreterListener);
super.dispose();
}
}

0 comments on commit a526e41

Please sign in to comment.