Skip to content

Commit

Permalink
components rearranged
Browse files Browse the repository at this point in the history
  • Loading branch information
MG-LSJ committed Jun 7, 2024
1 parent cbc0ccb commit 0d9f574
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 98 deletions.
79 changes: 3 additions & 76 deletions lib/components/edge_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:visual_graphs/components/graph_game.dart';
import 'package:visual_graphs/models/graph.dart';
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:visual_graphs/widgets/edge_info_box.dart';

class EdgeComponent extends ShapeComponent with HasGameRef<GraphGame> {
Edge edge;
Expand Down Expand Up @@ -52,6 +52,8 @@ class EdgeComponent extends ShapeComponent with HasGameRef<GraphGame> {
void showInfo(BuildContext context) {
showAdaptiveDialog(
context: context,
barrierDismissible: true,
useSafeArea: true,
builder: (BuildContext context) {
return EdgeInfoBox(edgeComponent: this);
},
Expand Down Expand Up @@ -278,78 +280,3 @@ class EdgeComponent extends ShapeComponent with HasGameRef<GraphGame> {
);
}
}

class EdgeInfoBox extends StatefulWidget {
final EdgeComponent edgeComponent;
const EdgeInfoBox({super.key, required this.edgeComponent});

@override
State<EdgeInfoBox> createState() => _EdgeInfoBoxState();
}

class _EdgeInfoBoxState extends State<EdgeInfoBox> {
@override
Widget build(BuildContext context) {
return AlertDialog.adaptive(
title: Text(
"Edge: Node ${widget.edgeComponent.edge.from.label} ${widget.edgeComponent.edge.isDirected ? "to" : "&"} Node ${widget.edgeComponent.edge.to.label}"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("ID: ${widget.edgeComponent.edge.id}"),
TextField(
decoration: const InputDecoration(
labelText: "Weight",
),
keyboardType: TextInputType.number,
controller: TextEditingController(
text: widget.edgeComponent.edge.weight.toString()),
focusNode: FocusNode()..requestFocus(),
onChanged: (value) {
if (value.isEmpty) {
widget.edgeComponent.edge.weight = 0;
}
try {
widget.edgeComponent.edge.weight = int.parse(value);
} catch (e) {
return;
}
},
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'[0-9\-]')),
],
),
CheckboxListTile(
title: const Text("Directed"),
value: widget.edgeComponent.edge.isDirected,
onChanged: (value) => setState(() {
widget.edgeComponent.gameRef.graph
.removeEdge(widget.edgeComponent.edge);
widget.edgeComponent.edge.isDirected = value!;
widget.edgeComponent.gameRef.graph
.addEdge(widget.edgeComponent.edge);
}),
),
if (widget.edgeComponent.edge.isDirected)
ElevatedButton.icon(
onPressed: () {
widget.edgeComponent.gameRef.graph
.removeEdge(widget.edgeComponent.edge);

// swap the vertices
var temp = widget.edgeComponent.edge.from;
widget.edgeComponent.edge.from = widget.edgeComponent.edge.to;
widget.edgeComponent.edge.to = temp;

widget.edgeComponent.gameRef.graph
.addEdge(widget.edgeComponent.edge);
setState(() {});
},
label: const Text("Reverse"),
icon: const Icon(Icons.swap_horiz_outlined),
),
],
),
);
}
}
23 changes: 4 additions & 19 deletions lib/components/vertex_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/events.dart';
import 'package:flutter/material.dart';
import 'package:visual_graphs/widgets/vertex_info_box.dart';

class VertexComponent extends ShapeComponent
with
Expand Down Expand Up @@ -73,26 +74,10 @@ class VertexComponent extends ShapeComponent
void showInfo(BuildContext context) {
showAdaptiveDialog(
context: context,
barrierDismissible: true,
useSafeArea: true,
builder: (BuildContext context) {
return AlertDialog.adaptive(
title: Text("Node ${vertex.label}"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("ID: ${vertex.id}"),
TextField(
decoration: const InputDecoration(
labelText: "Label",
),
controller: TextEditingController(text: vertex.label),
focusNode: FocusNode()..requestFocus(),
onChanged: (value) {
vertex.label = value;
},
),
],
),
);
return VertexInfoBox(vertexComponent: this);
},
);
}
Expand Down
91 changes: 91 additions & 0 deletions lib/widgets/edge_info_box.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:visual_graphs/components/edge_component.dart';

class EdgeInfoBox extends StatefulWidget {
final EdgeComponent edgeComponent;
const EdgeInfoBox({super.key, required this.edgeComponent});

@override
State<EdgeInfoBox> createState() => _EdgeInfoBoxState();
}

class _EdgeInfoBoxState extends State<EdgeInfoBox> {
@override
Widget build(BuildContext context) {
return AlertDialog.adaptive(
title: Text(
"Edge: Node ${widget.edgeComponent.edge.from.label} ${widget.edgeComponent.edge.isDirected ? "to" : "&"} Node ${widget.edgeComponent.edge.to.label}"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("ID: ${widget.edgeComponent.edge.id}"),
TextField(
decoration: const InputDecoration(
labelText: "Weight",
),
keyboardType: TextInputType.number,
controller: TextEditingController(
text: widget.edgeComponent.edge.weight.toString()),
focusNode: FocusNode()..requestFocus(),
onChanged: (value) {
if (value.isEmpty) {
widget.edgeComponent.edge.weight = 0;
}
try {
widget.edgeComponent.edge.weight = int.parse(value);
} catch (e) {
return;
}
},
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'[0-9\-]')),
],
),
CheckboxListTile(
title: const Text("Directed"),
value: widget.edgeComponent.edge.isDirected,
onChanged: (value) => setState(() {
widget.edgeComponent.gameRef.graph
.removeEdge(widget.edgeComponent.edge);
widget.edgeComponent.edge.isDirected = value!;
widget.edgeComponent.gameRef.graph
.addEdge(widget.edgeComponent.edge);
}),
),
if (widget.edgeComponent.edge.isDirected)
ElevatedButton.icon(
onPressed: () {
widget.edgeComponent.gameRef.graph
.removeEdge(widget.edgeComponent.edge);

// swap the vertices
var temp = widget.edgeComponent.edge.from;
widget.edgeComponent.edge.from = widget.edgeComponent.edge.to;
widget.edgeComponent.edge.to = temp;

widget.edgeComponent.gameRef.graph
.addEdge(widget.edgeComponent.edge);
setState(() {});
},
label: const Text("Reverse"),
icon: const Icon(Icons.swap_horiz_outlined),
),
const SizedBox(height: 10),
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: [
FilledButton(
child: const Text("Done"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
)
],
),
);
}
}
49 changes: 49 additions & 0 deletions lib/widgets/vertex_info_box.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:flutter/material.dart';
import 'package:visual_graphs/components/vertex_component.dart';

class VertexInfoBox extends StatefulWidget {
final VertexComponent vertexComponent;
const VertexInfoBox({super.key, required this.vertexComponent});

@override
State<VertexInfoBox> createState() => _VertexInfoBoxState();
}

class _VertexInfoBoxState extends State<VertexInfoBox> {
@override
Widget build(BuildContext context) {
return AlertDialog.adaptive(
title: Text("Node ${widget.vertexComponent.vertex.label}"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("ID: ${widget.vertexComponent.vertex.id}"),
TextField(
decoration: const InputDecoration(
labelText: "Label",
),
controller: TextEditingController(
text: widget.vertexComponent.vertex.label),
focusNode: FocusNode()..requestFocus(),
onChanged: (value) {
widget.vertexComponent.vertex.label = value;
},
),
const SizedBox(height: 10),
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: [
FilledButton(
child: const Text("Done"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
)
],
),
);
}
}
6 changes: 3 additions & 3 deletions web/site.webmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"short_name": "VisualGraphs",
"start_url": ".",
"display": "standalone",
"background_color": "#211F30",
"theme_color": "#211F30",
"background_color": "#211f30",
"theme_color": "#211f30",
"description": "A Graph Algorithms Visualizer that helps you visualize and understand the working of various graph algorithms.",
"orientation": "landscape-primary",
"orientation": "landscape",
"prefer_related_applications": false,
"icons": [
{
Expand Down

0 comments on commit 0d9f574

Please sign in to comment.