Page MenuHomec4science

Ac7.Dart
No OneTemporary

File Metadata

Created
Fri, May 10, 08:03

Ac7.Dart

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:student/widgets/onlineRobotMap.Dart';
import 'dart:async';
import 'dart:convert';
import 'package:student/Database.dart';
import 'package:student/model/Group.dart';
import 'package:student/model/Cellulo.dart';
import 'package:student/widgets/showAlertDialog.Dart';
import 'package:student/widgets/membersBar.Dart';
import 'package:student/widgets/inactivityDetector.Dart';
import 'package:flutter_appavailability/flutter_appavailability.dart';
class Ac7 extends StatefulWidget {
Ac7({Key key}) : super(key: key);
@override
_Ac7State createState() => _Ac7State();
}
class _Ac7State extends State<Ac7>
with SingleTickerProviderStateMixin, WidgetsBindingObserver {
Timer timerCelluloPosition;
Timer timerslopedetection;
Timer timercleartracking;
Timer timerprevCelluloPosition;
Timer timerInactivityDetector;
Timer timerprevCelluloPositiontoserver;
final acID = 7;
bool celluloxSwitch1 = true;
bool celluloySwitch1 = false;
bool celluloxSwitch2 = true;
bool celluloySwitch2 = false;
var onStop = false;
bool mode = false;
int wrongcounts = 0;
double _progress = 0.0;
final celluloSize = 60;
final ScrollController _scrollController = ScrollController();
final functionFormulas = ['X ⟼ Y', 'X+1 ⟼ Y', 'X+2 ⟼ Y', '2*X ⟼ Y', '-X ⟼ Y'];
Offset beginpath = Offset(0, 0);
Offset endpath = Offset(10, 100);
AnimationController controller;
Animation<double> animation;
TextEditingController controllerinitialX = TextEditingController();
TextEditingController controllerinitialY = TextEditingController();
TextEditingController controllerXslope = TextEditingController();
TextEditingController controllerYslope = TextEditingController();
var counterfunctionImage = 0;
var imagespath = [
'assets/images/Ac7_function1.svg',
'assets/images/Ac7_function2.svg',
'assets/images/Ac7_function3.svg',
'assets/images/GridOnlyPositive.svg',
];
var onTap = [false, false, false, false, false];
// var onTap=[false,false,false,false,false];
final double mapSizeWidth = 860;
final double mapSizeHeight = 860;
var currentTurn = 1;
var currentTap = 0;
var tapCounter = 0;
List<int> progress = [0, -2, -2];
// zero: undergoing, -1: not accomplished, 1: accomplished
bool waitforanimation = false;
List<int> mistakesSlope = [0, 0, 0];
List<int> mistakesIntrepet = [0, 0, 0];
List<int> mistakesInitialPosition = [0, 0, 0];
List<int> progressElpasedTime = [0, 0, 0];
var inactivity = 0;
var prevcelluloxPositionSlope = 0.0;
var prevcelluloyPositionSlope = 0.0;
// robot related
var elapseTimer = new Stopwatch();
bool setRun = false;
var celluloxPosition = [0.0, 0.0];
var celluloyPosition = [0.0, 0.0];
List<double> celluloxPositiontopaint = [0.0, 0.0];
List<double> celluloyPositiontopaint = [0.0, 0.0];
var prevcelluloxPosition = [0.0, 0.0];
var prevcelluloyPosition = [0.0, 0.0];
var initialPositionCellulox = [0.0, 0.0];
var initialPositionCelluloy = [0.0, 0.0];
// screen-related
var originCoordinates = [303.08, 563.08];
var avgActivation = 0;
int prevtotalTaps = 0;
var correctAnswer = [
{"initialPointX": 0, "initialPointY": 0, "slope": 1},
{"initialPointX": 0, "initialPointY": 1, "slope": 1},
{"initialPointX": 0, "initialPointY": 0, "slope": 2}
];
Future<void> getApps() async {
List<Map<String, String>> _installedApps;
_installedApps = await AppAvailability.getInstalledApps();
print(await AppAvailability.checkAvailability("com.android.chrome"));
// Returns: Map<String, String>{app_name: Chrome, package_name: com.android.chrome, versionCode: null, version_name: 55.0.2883.91}
print(await AppAvailability.isAppEnabled("com.android.chrome"));
// Returns: true
}
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
dbRef.child('groups').child(group.id).child('tabletStatus').set("YES");
dbRef
.child('groups')
.child(group.id)
.child('currentActivity')
.set("Activity 7");
elapseTimer.start();
onDataSend();
// getApps();
super.initState();
// group.numCurrentActivity = 7;
cellulox.resetrobot();
celluloy.resetrobot();
timerCelluloPosition =
new Timer.periodic(new Duration(milliseconds: 500), (time) {
cellulox.getrobotx().then((val) => setState(() {
celluloxPosition[0] = val;
if (setRun == true) celluloxPositiontopaint.add(val * 500 / 860);
// print(setRun.toString());
}));
cellulox.getroboty().then((val) => setState(() {
celluloxPosition[1] = val;
if (setRun == true) prevcelluloxPosition[1] = celluloxPosition[1];
}));
celluloy.getrobotx().then((val) => setState(() {
celluloyPosition[0] = val;
if (setRun == true) prevcelluloyPosition[0] = celluloyPosition[0];
}));
celluloy.getroboty().then((val) => setState(() {
celluloyPosition[1] = val;
if (setRun == true) celluloyPositiontopaint.add(val * 500 / 860);
}));
if (celluloxPosition[0] > 800 || celluloyPosition[1] > 800) {
cellulox.resetrobot();
celluloy.resetrobot();
}
/*
dbRef
.child(group.id)
.child("activity 8")
.child("inactivity")
.push()
.set(inactivity);
dbRef
.child(group.id)
.child("cellulox")
.child("x")
.push()
.set(celluloxPosition);
dbRef
.child(group.id)
.child("cellulox")
.child("y")
.push()
.set(celluloxPosition);
dbRef
.child(group.id)
.child("celluloy")
.child("x")
.push()
.set(celluloyPosition);
dbRef
.child(group.id)
.child("celluloy")
.child("y")
.push()
.set(celluloyPosition);
*/
});
timerprevCelluloPositiontoserver =
new Timer.periodic(new Duration(milliseconds: 400), (time) {
if (setRun == true) {
dbRef.child("celluloPosition").push().set(json.encode({
"x": celluloxPosition[0],
"y": celluloyPosition[1],
"acID": "Ac7",
"turn": currentTurn,
"groupID": group.id,
}));
}
});
timerprevCelluloPosition =
new Timer.periodic(new Duration(seconds: 40), (time) {
setState(() {
if (setRun == true) {
prevcelluloxPosition[0] = celluloxPosition[0];
prevcelluloxPosition[1] = celluloxPosition[1];
prevcelluloyPosition[0] = celluloyPosition[0];
prevcelluloyPosition[1] = celluloyPosition[1];
}
});
/*
dbRef
.child(group.id)
.child("activity 8")
.child("inactivity")
.push()
.set(inactivity);
dbRef
.child(group.id)
.child("cellulox")
.child("x")
.push()
.set(celluloxPosition);
dbRef
.child(group.id)
.child("cellulox")
.child("y")
.push()
.set(celluloxPosition);
dbRef
.child(group.id)
.child("celluloy")
.child("x")
.push()
.set(celluloyPosition);
dbRef
.child(group.id)
.child("celluloy")
.child("y")
.push()
.set(celluloyPosition);
*/
});
timerInactivityDetector =
new Timer.periodic(new Duration(seconds: 60), (time) {
onInactivityTimer();
});
controller = AnimationController(
duration: Duration(milliseconds: 10000), vsync: this);
///
/// Ask to be notified when messages related to the game
/// are sent by the server
///
}
AppLifecycleState _notification;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
print(state);
if (state == AppLifecycleState.resumed) {
dbRef.child('groups').child(group.id).child('tabletStatus').set("YES");
} else {
dbRef.child('groups').child(group.id).child('tabletStatus').set("NO");
}
setState(() {
_notification = state;
});
}
void onDataSend() {
dbRef.child('attempts').push().set(json.encode({
"numAttempts": tapCounter,
"groupID": group.id,
"acID": "Ac7",
"elpasedTime": elapseTimer.elapsedMilliseconds,
"progress": {
"turn1": progress[0],
"turn2": progress[1],
"turn3": progress[2]
},
"progressElpasedTime": {
"turn1": progressElpasedTime[0],
"turn2": progressElpasedTime[1],
"turn3": progressElpasedTime[2],
},
"currentTurn": currentTurn,
"inactivity": inactivity,
"mistakes": {
"turn1": {
"slope": mistakesSlope[0],
"initialPoint": mistakesInitialPosition[0]
},
"turn2": {
"slope": mistakesSlope[1],
"initialPoint": mistakesInitialPosition[1]
},
"turn3": {
"slope": mistakesSlope[2],
"initialPoint": mistakesInitialPosition[2]
},
}
}));
}
void slopeDtectionTimer() {
timerslopedetection = new Timer.periodic(new Duration(seconds: 1), (time) {
print("prev" +
(((celluloxPosition[0] - prevcelluloxPositionSlope).roundToDouble() >
0)
.toString()));
// print("currwnt" + celluloxPosition[0].toString());
controllerXslope.text = 1.0.toString();
if ((celluloxPosition[0] - prevcelluloxPositionSlope).abs() < 10) {
controllerYslope.text = "You sould move the RED robot faster";
} else {
controllerYslope.text =
(((prevcelluloyPositionSlope - celluloyPosition[1])) /
(celluloxPosition[0] - prevcelluloxPositionSlope))
.roundToDouble()
.toString();
}
prevcelluloxPositionSlope = celluloxPosition[0];
prevcelluloyPositionSlope = celluloyPosition[1];
// print(int.parse(controllerXslope.text) > 0);
print("cur" +
(((celluloxPosition[0] - prevcelluloxPositionSlope).roundToDouble() >
0)
.toString()));
if ((celluloxPosition[0] - prevcelluloxPositionSlope).roundToDouble() >
1) {
setState(() {
celluloxSwitch2 = true;
print(celluloxSwitch2);
});
}
if (((celluloxPosition[0] - prevcelluloxPositionSlope) / 100)
.roundToDouble() <
-1) {
setState(() {
celluloxSwitch2 = false;
print(celluloxSwitch2);
});
}
if (celluloyPosition[1] - prevcelluloyPositionSlope > 0) {
setState(() {
celluloySwitch2 = true;
print(celluloySwitch2);
});
}
if (celluloyPosition[1] - prevcelluloyPositionSlope < 1.0) {
setState(() {
celluloySwitch2 = false;
print(celluloySwitch2);
});
}
});
}
void clearTrackingTimer() {
timercleartracking = new Timer.periodic(new Duration(seconds: 5), (time) {
cellulox.resetrobot();
celluloy.resetrobot();
setState(() {
setRun = true;
});
timercleartracking.cancel();
});
}
void onInactivityTimer() {
group.inactivity = group.inactivity + 1;
dbRef.child('activation').push().set(json.encode({
"groupID": group.id,
"acID": "Ac7",
"elpasedTime": elapseTimer.elapsedMilliseconds,
"inactivity": tapCounter - prevtotalTaps,
}));
prevtotalTaps = tapCounter;
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
timer.cancel();
super.dispose();
}
void nextPLayer() {
setState(() {
currentTurn = currentTurn + 1;
// celluloyPositiontopaint = [0.0, 0.0];
// celluloxPositiontopaint = [0.0, 0.0];
});
}
void animationRunner(progress) {
print(progress);
if (progress < 0.4) {
cellulox.setGoalPosition(
originCoordinates[0] + 100 * int.parse(controllerinitialX.text), 600);
celluloy.setGoalPosition(
100, originCoordinates[1] - 100 * int.parse(controllerinitialY.text));
}
setState(() {
setRun = true;
});
if (progress > 0.6) {
cellulox.setGoalPosition(
originCoordinates[0] + 100 * int.parse(controllerinitialX.text) + 500,
600);
celluloy.setGoalPosition(
100,
originCoordinates[1] -
100 * int.parse(controllerinitialY.text) -
500);
}
if (celluloxPosition[0] > 800 || celluloyPosition[1] > 800) {
cellulox.resetrobot();
celluloy.resetrobot();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Activity 7: Semi-Mathematical Control'),
),
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Column(children: <Widget>[
Card(
child: ListTile(
title: Text(
'Choose the two points that your robots start from there and also how much the blue robot should move. '),
),
),
Container(
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent),
borderRadius: new BorderRadius.circular(25.0),
),
height: 230.0,
child: Column(children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
width: 250,
child: new TextFormField(
controller: controllerinitialX,
decoration: new InputDecoration(
labelText: "RED starts at this position:",
fillColor: Colors.red,
border: new OutlineInputBorder(
borderRadius:
new BorderRadius.circular(25.0),
borderSide: new BorderSide(),
),
//fillColor: Colors.green
),
validator: (val) {
if (val.length == 0) {
return "Email cannot be empty";
} else {
return null;
}
},
keyboardType: TextInputType.text,
style: new TextStyle(
fontFamily: "Poppins",
),
),
),
SizedBox(height: 10),
Row(children: <Widget>[
Container(
width: 250,
child: new TextFormField(
enabled: false,
// controller: controllerXslope,
decoration: new InputDecoration(
labelText: "When RED moves by 1",
fillColor: Colors.red,
border: new OutlineInputBorder(
borderRadius:
new BorderRadius.circular(25.0),
borderSide: new BorderSide(),
),
//fillColor: Colors.green
),
validator: (val) {
if (val.length == 0) {
return "Email cannot be empty";
} else {
return null;
}
},
keyboardType: TextInputType.text,
style: new TextStyle(
fontFamily: "Poppins",
),
),
),
Column(children: <Widget>[
Container(
child: Switch(
value: celluloxSwitch1,
onChanged: (value) {
setState(() {
// celluloxSwitch1 = value;
});
},
activeTrackColor: Colors.lightGreenAccent,
activeColor: Colors.green,
inactiveTrackColor: Colors.lightGreenAccent,
inactiveThumbColor: Colors.green,
)),
Text(celluloxSwitch1 ? 'Right' : 'Left')
]),
]),
]),
SizedBox(
width: 8,
),
Column(
// mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 250,
child: new TextFormField(
controller: controllerinitialY,
decoration: new InputDecoration(
labelText: "BLUE starts at this position:",
fillColor: Colors.blue,
border: new OutlineInputBorder(
borderRadius:
new BorderRadius.circular(25.0),
borderSide: new BorderSide(),
),
//fillColor: Colors.green
),
validator: (val) {
if (val.length == 0) {
return "Email cannot be empty";
} else {
return null;
}
},
keyboardType: TextInputType.text,
style: new TextStyle(
fontFamily: "Poppins",
),
),
),
SizedBox(height: 10),
Row(children: <Widget>[
Container(
width: 250,
child: new TextFormField(
controller: controllerYslope,
decoration: new InputDecoration(
labelText: "Then Blue should move by:",
fillColor: Colors.blue,
border: new OutlineInputBorder(
borderRadius:
new BorderRadius.circular(25.0),
borderSide: new BorderSide(),
),
//fillColor: Colors.green
),
validator: (val) {
if (val.length == 0) {
return "Email cannot be empty";
} else {
return null;
}
},
keyboardType: TextInputType.text,
style: new TextStyle(
fontFamily: "Poppins",
),
)),
Column(children: <Widget>[
RotatedBox(
quarterTurns: 1,
child: Switch(
value: celluloySwitch1,
onChanged: (value) {
setState(() {
celluloySwitch1 = value;
});
},
activeTrackColor: Colors.lightGreenAccent,
activeColor: Colors.green,
inactiveTrackColor:
Colors.lightGreenAccent,
inactiveThumbColor: Colors.green,
)),
Text(celluloySwitch1 ? 'Down' : 'Up')
]),
])
]),
]),
SizedBox(
height: 5,
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: <
Widget>[
FloatingActionButton(
heroTag: "btn2",
tooltip: 'Run the Robots',
onPressed: () => {
tapCounter = tapCounter + 1,
// avgActivation=
if (controllerinitialX.text !=
correctAnswer[currentTurn - 1]['initialPointX']
.toString() ||
controllerinitialY.text !=
correctAnswer[currentTurn - 1]['initialPointY']
.toString())
{
mistakesInitialPosition[currentTurn - 1] =
mistakesInitialPosition[currentTurn - 1] + 1,
},
if (controllerYslope.text !=
correctAnswer[currentTurn - 1]['slope'].toString())
{
mistakesSlope[currentTurn - 1] =
mistakesSlope[currentTurn - 1] + 1,
},
if (controllerinitialX.text ==
correctAnswer[currentTurn - 1]
['initialPointX']
.toString() &&
controllerinitialY.text ==
correctAnswer[currentTurn - 1]['initialPointY']
.toString() &&
controllerYslope.text ==
correctAnswer[currentTurn - 1]['slope']
.toString())
{
progress[currentTurn - 1] = 1,
},
onDataSend(),
controller.reset(),
animation = Tween(begin: 0.0, end: 1.0).animate(
controller)
..addListener(() {
_progress = animation.value;
if (_progress < 0.4) {
cellulox.setGoalPosition(
originCoordinates[0] +
100 * int.parse(controllerinitialX.text),
600);
celluloy.setGoalPosition(
100,
originCoordinates[1] -
100 * int.parse(controllerinitialY.text));
}
if (_progress > 0.6) {
setState(() {
setRun = true;
});
cellulox.setGoalPosition(
originCoordinates[0] +
100 * int.parse(controllerinitialX.text) +
300,
600);
celluloy.setGoalPosition(
100,
originCoordinates[1] -
100 * int.parse(controllerinitialY.text) -
150);
}
if (celluloxPosition[0] > 800 ||
celluloyPosition[1] > 800) {
cellulox.resetrobot();
celluloy.resetrobot();
}
}),
controller.forward(),
},
child: Icon(Icons.play_circle_outline),
),
SizedBox(width: 20),
FloatingActionButton(
heroTag: "btn1",
tooltip: 'Next Turn',
onPressed: () => {
setState(() {
currentTurn = currentTurn + 1;
if (currentTurn == 2) {
progressElpasedTime[0] =
elapseTimer.elapsedMilliseconds;
} else {
progressElpasedTime[currentTurn - 2] =
elapseTimer.elapsedMilliseconds -
progressElpasedTime[currentTurn - 3];
}
}),
if (progress[currentTurn - 2] == 0)
progress[currentTurn - 2] = -1,
if (currentTurn < 4)
{
celluloxPositiontopaint = [0, 0],
celluloyPositiontopaint = [0, 0],
controller.reset(),
setRun = false,
progress[currentTurn - 1] = 0,
onDataSend(),
}
// _ackAlert,
// showAlertDialog(context, '', 'Now its next player turn'),
},
child: Icon(Icons.navigate_next),
),
]),
])),
SizedBox(
height: 5,
),
celluloMap(context),
SizedBox(
height: 15,
),
MembersBar(),
])));
}
Widget celluloMap(BuildContext context) {
final double mapSizeScreenWidth = 500;
final double mapSizeScreenHeight = 500;
final double scaleWidth = mapSizeScreenWidth / mapSizeWidth;
final double scaleHeight = mapSizeScreenHeight / mapSizeHeight;
return Container(
height: mapSizeScreenWidth,
width: mapSizeScreenHeight,
decoration: BoxDecoration(border: Border.all(color: Colors.blueAccent)),
child: Stack(children: <Widget>[
Container(
height: mapSizeScreenWidth,
width: mapSizeScreenHeight,
child: SvgPicture.asset(imagespath[currentTurn - 1])),
Container(
height: mapSizeScreenWidth,
width: mapSizeScreenHeight,
child: SvgPicture.asset("assets/images/GridOnlyPositive.svg")),
Align(
alignment: Alignment(
2 * (celluloxPosition[0] * scaleWidth / mapSizeScreenWidth) - 1,
1),
child: Card(
child: SvgPicture.asset("assets/images/cellulox.svg",
height: 60, width: 60),
),
),
Align(
alignment: Alignment(
-1,
2 * (celluloyPosition[1] * scaleHeight / mapSizeScreenHeight) -
1),
child: Card(
child: SvgPicture.asset("assets/images/celluloy.svg",
height: 60, width: 60),
),
),
CustomPaint(
size: Size(mapSizeScreenWidth, mapSizeScreenHeight),
painter:
LinePainter2(celluloxPositiontopaint, celluloyPositiontopaint),
),
]));
}
}

Event Timeline