Graphs_dart/flutter/lib/pages/drawing_page.dart

199 lines
6.2 KiB
Dart
Raw Normal View History

2021-11-05 00:35:51 +03:00
import 'dart:io';
import 'package:graphs/curve_painter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:graphs/src/graph.dart';
Graphs getGraph() {
List<Dot> d = <Dot>[];
d.add(Dot.fromTwoLists("1", [2, 3], [1, 1]));
d.add(Dot.fromTwoLists("2", [1, 3], [1, 1]));
d.add(Dot.fromTwoLists("3", [1, 2], [1, 1]));
d.add(Dot.fromTwoLists("Name1", [], []));
d.add(Dot.fromTwoLists("Name2", [], []));
return Graphs.fromList("1", d, false, false);
}
2021-11-05 00:35:51 +03:00
class DrawingPage extends StatefulWidget {
const DrawingPage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _DrawingPageState();
}
class _DrawingPageState extends State<DrawingPage> {
2021-11-05 01:40:16 +03:00
double screenSize = 0;
Graphs data = getGraph();
2021-11-05 00:35:51 +03:00
final _textNameController = TextEditingController();
final _textInptController = TextEditingController();
final _textDistController = TextEditingController();
final _textLnthController = TextEditingController();
void clearTextControllers() {
_textDistController.clear();
_textInptController.clear();
_textLnthController.clear();
_textNameController.clear();
}
2021-11-05 01:40:16 +03:00
2021-11-05 00:35:51 +03:00
@override
Widget build(BuildContext context) {
2021-11-05 01:40:16 +03:00
screenSize = MediaQuery.of(context).size.width;
2021-11-05 00:35:51 +03:00
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("Graph",
style: TextStyle(fontSize: 35, color: Colors.white)),
toolbarHeight: 110, // Set this height
flexibleSpace: Container(
color: Colors.blue,
child: Column(children: <Widget>[
const SizedBox(height: 5),
Row(children: [
addSpaceW(screenSize / 8 - 20),
createButton("\nAdd dot\n", addDotPushed),
createInputBox("Dot name", screenSize / 4 - 25, Icons.label,
_textNameController),
addSpaceW(20),
createButton("\nDel dot \n", delDotPushed),
createInputBox("Dot number", screenSize / 4 - 25,
Icons.fiber_manual_record, _textInptController),
]),
addSpaceH(3),
2021-11-05 01:40:16 +03:00
Row(children: [
addSpaceW(screenSize / 4 - 20),
createInputBox("Destination number", screenSize / 4 - 5,
Icons.fiber_manual_record, _textDistController),
2021-11-05 01:40:16 +03:00
//addSpaceW(4),
createInputBox("Input length", screenSize / 4 - 5,
Icons.arrow_right_alt_outlined, _textLnthController),
2021-11-05 01:40:16 +03:00
]),
//createInputBox("Input path length", screenSize / 3 + 30, Icons.arrow_right_alt_outlined, _textLenController),
2021-11-05 00:35:51 +03:00
]),
),
actions: [
/*Column(
2021-11-05 00:35:51 +03:00
children: [
2021-11-05 01:40:16 +03:00
addSpaceH(10),
createButton("\nAdd dot\n", addDotPushed),
addSpaceH(3),
createButton("\nDel dot \n", addLenPushed),
2021-11-05 00:35:51 +03:00
],
),*/
2021-11-05 00:35:51 +03:00
IconButton(
onPressed: () => flushData(),
icon: const Icon(Icons.delete_sweep),
iconSize: 60,
2021-11-05 00:35:51 +03:00
),
]),
body: CustomPaint(
painter: CurvePainter(gr: data),
2021-11-05 00:35:51 +03:00
child: const Center(),
),
));
}
2021-11-05 01:40:16 +03:00
// ignore: avoid_types_as_parameter_names, non_constant_identifier_names, use_function_type_syntax_for_parameters
ElevatedButton createButton(String txt, void onPressing()) {
2021-11-05 00:35:51 +03:00
return ElevatedButton(
2021-11-05 01:40:16 +03:00
/*style: TextButton.styleFrom(
2021-11-05 00:35:51 +03:00
primary: Colors.blue,
2021-11-05 01:40:16 +03:00
),*/
onPressed: onPressing,
2021-11-05 00:35:51 +03:00
child: Text(txt,
style: const TextStyle(
fontSize: 15,
color: Colors.white,
height: 1,
)),
);
}
2021-11-05 01:40:16 +03:00
Container createInputBox(String text, double wid, IconData icon,
TextEditingController controller) {
return Container(
width: wid,
height: 40,
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
child: TextField(
controller: controller,
textAlign: TextAlign.center,
decoration: InputDecoration(
contentPadding:
const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
filled: true,
fillColor: Colors.white,
prefixIcon: Icon(icon, color: Colors.black),
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(40))),
hintStyle: const TextStyle(color: Colors.black38),
hintText: text),
));
}
SizedBox addSpaceH(double h) {
2021-11-05 00:35:51 +03:00
return SizedBox(height: h);
}
2021-11-05 01:40:16 +03:00
SizedBox addSpaceW(double w) {
return SizedBox(width: w);
}
void addDotPushed() {
//showPopUp("Test", "Test message");
//var inp = int.tryParse(_textNameController.text);
if (_textNameController.text == "") {
showPopUp("Error", "No name in \"Dot name\" box");
} else {
String? res = data.addIsolated(_textNameController.text);
if (res != null) {
showPopUp("Error", res);
}
}
//print(_textNameController.text);
clearTextControllers();
}
void delDotPushed() {
if (_textNameController.text == "") {
showPopUp("Error", "No name in \"Dot name\" box");
} else {
String? res = data.addIsolated(_textNameController.text);
if (res != null) {
showPopUp("Error", res);
}
}
clearTextControllers();
2021-11-05 01:40:16 +03:00
}
void addLenPushed() {
//data.delDot()
2021-11-05 01:40:16 +03:00
print(
"${_textInptController.text} -> ${_textDistController.text} = ${_textLnthController.text}");
clearTextControllers();
2021-11-05 01:40:16 +03:00
}
void showPopUp(String alertTitle, String err) => showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text(alertTitle),
content: Text(err),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'OK'),
child: const Text('OK'),
),
],
),
);
void flushData() {
/*if (Platform.isWindows) {
2021-11-05 00:35:51 +03:00
exit(0);
}
SystemChannels.platform.invokeMethod('SystemNavigator.pop');*/
data.flushData();
2021-11-05 00:35:51 +03:00
}
}