You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
7.0 KiB
Dart

3 years ago
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'num_format.dart';
import 'dart:math';
import 'dart:async';
void main() {
runApp(const MaterialApp(
home: Home(),
debugShowCheckedModeBanner: false,
));
}
double truncateToDecimalPlaces(num value, int fractionalDigits) => (value * pow(10,
fractionalDigits)).truncate() / pow(10, fractionalDigits);
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
int _num = 94;
String _status = "Not yet started";
int _timer = 60;
double _txt = 0.0;
double rng1 = 0.0, rng2 = 0.0, res1 = 0.0, res2 = 0.0;
bool match = false;
var r1 = Random();
var r2 = Random();
Timer? t, t2, t3;
void cancelTimers() {
setState(() {
t?.cancel();
t2?.cancel();
t3?.cancel();
});
}
void btnPress() async {
cancelTimers();
setState(() {
_timer = 60;
rng1 = 0.0;
res1 = 0.0;
rng2 = 0.0;
res2 = 0.0;
});
setState(() {
process();
t = Timer.periodic(const Duration(seconds: 1), (timer) {
setState(() {
_status = "Processing";
});
setState(() {
res2 = _txt + _num;
});
if (match) {
setState(() {
_status = "Match found";
rng1 = res1;
cancelTimers();
});
}
if (_timer <= 0) {
if (!match) {
setState(() {
_status = "No match";
});
}
cancelTimers();
}
setState(() {
_timer -= 1;
});
});
});
}
void process() async {
t2 = Timer.periodic(const Duration(milliseconds: 100), (timer) {
setState(() {
rng1 = truncateToDecimalPlaces(r1.nextDouble(), 2);
setState(() {
res1 = rng1 + rng2;
});
if (res1 == res2) {
setState(() {
match = true;
cancelTimers();
});
}
});
});
if (_num == 94) {
setState(() {
rng2 = 80 + r2.nextInt((20)).toDouble();
});
} else {
setState(() {
rng2 = 100 + r2.nextInt((20)).toDouble();
});
}
t3 = Timer.periodic(const Duration(seconds: 3), (timer) {
if (_num == 94) {
setState(() {
rng2 = 80 + r2.nextInt((20)).toDouble();
});
} else {
setState(() {
rng2 = 100 + r2.nextInt((20)).toDouble();
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ArcVal'),
centerTitle: true,
),
body: Container(
padding: const EdgeInsets.symmetric(horizontal: 30.0, vertical: 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Center(child:
Text(
'Status: $_status',
style: const TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
)
),
const SizedBox(height: 30),
const Text(
'Choose a number:',
style: TextStyle(
fontSize: 18.0,
),
),
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
child: RadioListTile<int>(
title: const Text('94'),
value: 94,
groupValue: _num,
onChanged: (v) {
setState(() {
_num = v!;
});
},
),
),
Expanded(
child: RadioListTile<int>(
title: const Text('114'),
value: 114,
groupValue: _num,
onChanged: (v) {
setState(() {
_num = v!;
});
},
),
),
],
),
const SizedBox(height: 20),
const Text(
'Enter a number between -5.0 and 5.0:',
style: TextStyle(
fontSize: 18.0,
),
),
const SizedBox(height: 10),
TextFormField(
initialValue: '0.0',
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter your number'
),
keyboardType: TextInputType.number,
onChanged: (String v) {
setState(() {
_txt = double.parse(v);
});
},
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^-?\d{0,1}\.?\d{0,1}')),
NumFormat(),
],
),
const SizedBox(height: 30),
const Center(
child: Text(
'Result Display',
style: TextStyle(
fontSize: 18.0,
),
),
),
const SizedBox(height: 30),
Row(
children: <Widget>[
Expanded(
child: Column(
children: <Widget>[
const Text('Result 1'),
Text(
truncateToDecimalPlaces(res1, 2).toString(),
style: const TextStyle(
fontSize: 18.0,
),
),
],
),
),
Expanded(
child: Column(
children: <Widget>[
const Text('Result 2'),
Text(
res2.toString(),
style: const TextStyle(
fontSize: 18.0,
),
),
],
),
),
],
),
const SizedBox(height: 30),
Center(
child: Text(
'Timer: $_timer',
style: const TextStyle(
fontSize: 18.0,
),
),
),
const SizedBox(height: 30),
OutlinedButton(
onPressed: btnPress,
child: const Text('Start'),
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
backgroundColor: MaterialStateProperty.all<Color>(Colors.blueAccent),
),
)
],
),
),
);
}
}