Skip to content

Commit

Permalink
fixing colors and adding more buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
abdelaziz-mahdy committed Nov 24, 2023
1 parent e4c6d77 commit 18862b1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 231 deletions.
11 changes: 7 additions & 4 deletions lib/controllers/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class GridController {
void updateBlockState(int row, int column) {
if (isChangingWallStateAllowed) {
final currentBlockState = matrix[row][column].value;
BlockState newBlockState;
BlockState newBlockState = currentBlockState;

switch (cursorType) {
case CursorType.start:
Expand All @@ -94,10 +94,13 @@ class GridController {
resetEndPoint(); // Reset any existing end point
break;
case CursorType.wall:
newBlockState = BlockState.wall;
case CursorType.eraser:
if (currentBlockState == BlockState.wall) {
newBlockState = BlockState.none;
}
case CursorType.none:
default:
newBlockState = currentBlockState == BlockState.wall
? BlockState.none
: BlockState.wall;
break;
}

Expand Down
156 changes: 111 additions & 45 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:path_finding/algorithm/a_star.dart';
import 'package:path_finding/controllers/controller.dart';
import 'package:path_finding/widgets/action_button.dart';
import 'package:path_finding/widgets/expandable_fab.dart';
import 'package:path_finding/widgets/grid.dart';
import 'package:path_finding/models/models.dart';

Expand All @@ -24,20 +20,35 @@ class MyApp extends StatelessWidget {
return MaterialApp(
title: 'Path Finding Visualizer',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blueAccent),
useMaterial3: true,
),
home: const MyHomePage(title: 'Path Finding Visualizer'),
);
}
}

class MyHomePage extends StatelessWidget {
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
String _selectedAction = 'none';

void _handleAction(String action) {
setState(() {
_selectedAction = _selectedAction == action ? 'none' : action;
});
final controller = GridController();
if (_selectedAction == 'none') {
controller.cursorType = CursorType.none;
return;
}
switch (action) {
case 'start':
controller.cursorType = CursorType.start;
Expand All @@ -48,6 +59,8 @@ class MyHomePage extends StatelessWidget {
case 'wall':
controller.cursorType = CursorType.wall;
break;
case 'eraser':
controller.cursorType = CursorType.eraser;
case 'reset':
controller.resetMatrix();
break;
Expand All @@ -62,51 +75,65 @@ class MyHomePage extends StatelessWidget {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(title),
title: Text(widget.title),
),
body: Center(
child: Grid(
horizontalBlockCount: GridController().rows,
verticalBlockCount: GridController().columns,
),
),
floatingActionButton: ExpandableFab(
distance: max(100, MediaQuery.sizeOf(context).width * 0.15),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ActionButton(
onPressed: () => _handleAction('start'),
icon: const Text(
"Start",
style: TextStyle(color: Colors.white),
),
ActionButtonWidget(
action: 'start',
label: 'Start',
selectedAction: _selectedAction,
handleAction: _handleAction,
isCursorType: true,
),
const SizedBox(height: 10),
ActionButtonWidget(
action: 'end',
label: 'End',
selectedAction: _selectedAction,
handleAction: _handleAction,
isCursorType: true,
),
ActionButton(
onPressed: () => _handleAction('end'),
icon: const Text(
"End",
style: TextStyle(color: Colors.white),
),
const SizedBox(height: 10),
ActionButtonWidget(
action: 'wall',
label: 'Wall',
selectedAction: _selectedAction,
handleAction: _handleAction,
isCursorType: true,
),
ActionButton(
onPressed: () => _handleAction('wall'),
icon: const Text(
"Wall",
style: TextStyle(color: Colors.white),
),
const SizedBox(height: 10),
ActionButtonWidget(
action: 'eraser',
label: 'Eraser',
selectedAction: _selectedAction,
handleAction: _handleAction,
isCursorType: true,
),
ActionButton(
onPressed: () => _handleAction('reset'),
icon: const Text(
"Reset",
style: TextStyle(color: Colors.white),
),
const SizedBox(height: 25),
ActionButtonWidget(
action: 'reset',
label: 'Reset',
icon: Icons.refresh,
selectedAction: _selectedAction,
handleAction: _handleAction,
isCursorType: false,
),
ActionButton(
onPressed: () => _handleAction('startAlgorithm'),
icon: const Text(
"Run",
style: TextStyle(color: Colors.white),
),
const SizedBox(height: 10),
ActionButtonWidget(
action: 'startAlgorithm',
label: 'Start',
icon: Icons.play_arrow,
selectedAction: _selectedAction,
handleAction: _handleAction,
isCursorType: false,
),
],
),
Expand All @@ -115,14 +142,53 @@ class MyHomePage extends StatelessWidget {

void _startAlgorithm() {
final controller = GridController();
// Perform the algorithm execution using the controller's matrix
AlgorithmResult result =
AStarAlgorithm().execute(controller.getMatrixValues());
print("ended");
print("ended ${result.changes}");
print("ended ${result.path}");

// You can access the changes and path from the result object and perform necessary actions
controller.applyAlgorithmResult(result);
}
}

class ActionButtonWidget extends StatelessWidget {
final String action;
final String label;
final IconData? icon;
final String selectedAction;
final Function(String) handleAction;
final bool isCursorType;

const ActionButtonWidget({
Key? key,
this.icon,
required this.action,
required this.label,
required this.selectedAction,
required this.handleAction,
required this.isCursorType,
}) : super(key: key);

@override
Widget build(BuildContext context) {
bool isSelected = selectedAction == action;
TextStyle textStyle = TextStyle(
color: isSelected
? Theme.of(context).colorScheme.onPrimary
: Theme.of(context).colorScheme.onSecondary);
Color backgroundColor = isSelected
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.secondary;

if (!isCursorType) {
textStyle =
TextStyle(color: Theme.of(context).colorScheme.onPrimaryContainer);
backgroundColor = Theme.of(context).colorScheme.primaryContainer;
}

return FloatingActionButton(
onPressed: () => handleAction(action),
child: icon != null
? Icon(icon, color: textStyle.color)
: Text(label, style: textStyle),
backgroundColor: backgroundColor,
);
}
}
2 changes: 2 additions & 0 deletions lib/models/cursor_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ enum CursorType {
start,
end,
wall,
none,
eraser
}
Loading

0 comments on commit 18862b1

Please sign in to comment.