Skip to content

Commit 5f1fd8b

Browse files
fix: undo redo buttons greyed out when stack is empty
1 parent dae62cf commit 5f1fd8b

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

lib/providers/draw_badge_provider.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ class DrawBadgeProvider extends ChangeNotifier {
1010

1111
List<List<bool>> _drawViewGrid =
1212
List.generate(11, (_) => List.generate(44, (_) => false));
13-
1413
final List<List<bool>> _previewGrid =
1514
List.generate(11, (_) => List.generate(44, (_) => false));
16-
1715
final List<List<List<bool>>> _undoStack = [];
1816
final List<List<List<bool>>> _redoStack = [];
1917

@@ -35,12 +33,10 @@ class DrawBadgeProvider extends ChangeNotifier {
3533

3634
DrawShape get selectedShape => _selectedShape;
3735
bool getIsDrawing() => isDrawing;
38-
3936
bool get canUndo => _undoStack.isNotEmpty;
4037
bool get canRedo => _redoStack.isNotEmpty;
4138

4239
// ========== STATE SETTERS ==========
43-
4440
void toggleIsDrawing(bool drawing) {
4541
isDrawing = drawing;
4642
notifyListeners();
@@ -108,7 +104,6 @@ class DrawBadgeProvider extends ChangeNotifier {
108104
}
109105

110106
// ========== UNDO / REDO ==========
111-
112107
void _pushToUndoStack() {
113108
_undoStack.add(_copyGrid(_drawViewGrid));
114109
_redoStack.clear(); // Invalidate redo stack on new action
@@ -142,5 +137,6 @@ class DrawBadgeProvider extends ChangeNotifier {
142137
class GridPosition {
143138
final int x;
144139
final int y;
140+
145141
GridPosition(this.x, this.y);
146142
}

lib/view/draw_badge_screen.dart

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class _DrawBadgeState extends State<DrawBadge> {
9696
_buildSaveButton(fileHelper),
9797
_buildShapesToggleButton(),
9898

99-
// ✅ New Undo & Redo buttons
99+
// ✅ Updated Undo & Redo buttons with greyed out state
100100
_buildUndoButton(),
101101
_buildRedoButton(),
102102
],
@@ -232,32 +232,44 @@ class _DrawBadgeState extends State<DrawBadge> {
232232
}
233233

234234
Widget _buildUndoButton() {
235+
// Check if undo is available using the getter
236+
final bool canUndo = drawToggle.canUndo;
237+
final Color buttonColor = canUndo ? Colors.black : Colors.grey;
238+
235239
return TextButton(
236-
onPressed: () {
237-
setState(() {
238-
drawToggle.undo();
239-
});
240-
},
241-
child: const Column(
240+
onPressed: canUndo
241+
? () {
242+
setState(() {
243+
drawToggle.undo();
244+
});
245+
}
246+
: null, // Disable button when can't undo
247+
child: Column(
242248
children: [
243-
Icon(Icons.undo, color: Colors.black),
244-
Text('Undo', style: TextStyle(color: Colors.black)),
249+
Icon(Icons.undo, color: buttonColor),
250+
Text('Undo', style: TextStyle(color: buttonColor)),
245251
],
246252
),
247253
);
248254
}
249255

250256
Widget _buildRedoButton() {
257+
// Check if redo is available using the getter
258+
final bool canRedo = drawToggle.canRedo;
259+
final Color buttonColor = canRedo ? Colors.black : Colors.grey;
260+
251261
return TextButton(
252-
onPressed: () {
253-
setState(() {
254-
drawToggle.redo();
255-
});
256-
},
257-
child: const Column(
262+
onPressed: canRedo
263+
? () {
264+
setState(() {
265+
drawToggle.redo();
266+
});
267+
}
268+
: null, // Disable button when can't redo
269+
child: Column(
258270
children: [
259-
Icon(Icons.redo, color: Colors.black),
260-
Text('Redo', style: TextStyle(color: Colors.black)),
271+
Icon(Icons.redo, color: buttonColor),
272+
Text('Redo', style: TextStyle(color: buttonColor)),
261273
],
262274
),
263275
);

0 commit comments

Comments
 (0)