我已经开始创建一个随机的数学应用程序,用户首先可以选择算术。之后,将显示该算法的随机计数,用户将使用键盘填写答案。
现在,我不确定将每种算术放在自己的类中(然后在其中获取随机数)是最好的方法,还是有更好的方法?
我试图将所有算术选择添加到一个类中,但是我无法使它起作用。我将不得不以某种方式将每个选择传递给同一个类中的正确计算。
我当时的想法就像是使用计算器,但是您首先将算术放在另一页上。这甚至行得通吗?
是否可以将每个类的输出收集到一个类中,这样我就只需要创建一个键盘(即使我使用keyboard.numbers)?
如我所见(但我可能会犯错),具有不同类和不同键盘键的缺点是应用程序的大小。 另一方面,优点是可以为每个算术类设置不同的样式。
下面的代码尚未完成,但是可以正常工作,并且有希望使您对我正在尝试做的事情有所了解。(我已将图像用于算术-添加到pubspec.yaml中)
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:math';
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp]) //to make the app in portait mode
.then((_) {
//to let it load in portrait mode before launch
runApp(new MyApp());
});
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "SOME MATH",
theme: ThemeData(primarySwatch: Colors.red),
home: MyHomeScreen(),
);
}
}
class MyHomeScreen extends StatefulWidget {
@override
_MyHomeScreenState createState() => _MyHomeScreenState();
}
class _MyHomeScreenState extends State<MyHomeScreen> {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.red,
),
child: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
backgroundColor: Colors.transparent,
//To make the appBar invisible
elevation: 0.0,
leading: IconButton(icon: Icon(Icons.menu), onPressed: () {}),
//tapable without action
title: Text(
"MATH",
style: TextStyle(fontSize: 25.0),
),
),
body: ListView(
//would listView.builder be better?
children: <Widget>[
GestureDetector(
//could be changed to inkWell
child: _ArithmeticCard(
headImageAssetPath: 'assets/plus.png',
),
onTap: () {
//make it pass to stateful
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) => new MyPlus()));
},
),
GestureDetector(
child: _ArithmeticCard(
headImageAssetPath: 'assets/minus.png',
),
onTap: () {
//new taps to different classes
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) => new MyMinus()));
},
),
GestureDetector(
child: _ArithmeticCard(
headImageAssetPath: 'assets/times.png',
),
onTap: () {
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) => new MyTimes()));
},
),
GestureDetector(
child: _ArithmeticCard(
headImageAssetPath: 'assets/division.png',
),
onTap: () {
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) => new MyDivision()));
},
),
],
),
),
);
}
}
class _ArithmeticCard extends StatelessWidget {
final String headImageAssetPath;
_ArithmeticCard({this.headImageAssetPath});
//creating Card
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(left: 10.0, right: 10.0, bottom:
10.0),
child: Card(
elevation: 10.0,
child: Column(
children: <Widget>[
Image.asset(
headImageAssetPath,
width: double.infinity,
height: 150.0,
fit: BoxFit.cover,
),
],
),
),
);
}
}
class MyPlus extends StatefulWidget {
//class for adding
@override
_MyPlusState createState() => _MyPlusState();
}
class _MyPlusState extends State<MyPlus> {
final random = Random();
int a, b, sum;
String output;
void changeData() {
setState(() {
a = random.nextInt(10);
b = random.nextInt(10);
sum = a + b;
output = "$a + $b = ";
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(
"$output",
style: TextStyle(fontSize: 48.0),
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.amber, //just playing with colors
child: Icon(Icons.refresh, color: Colors.white),
onPressed: changeData,
),
);
}
}
class MyMinus extends StatefulWidget {
//class for subtraktion
@override
_MyMinusState createState() => _MyMinusState();
}
class _MyMinusState extends State<MyMinus> {
final random = Random();
int a, b, sum;
String output;
void changeData() {
setState(() {
a = random.nextInt(10);
b = random.nextInt(10);
if (a >= b) {
sum = a - b;
output = "$a - $b =";
} else {
//Don't want the sum to be negative
sum = b - a;
output = "$b - $a =";
}
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(
"$output",
style: TextStyle(fontSize: 48.0),
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.lime,
child: Icon(Icons.refresh, color: Colors.black),
onPressed: changeData,
),
);
}
}
class MyTimes extends StatefulWidget {
@override
_MyTimesState createState() => _MyTimesState();
}
class _MyTimesState extends State<MyTimes> {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(color: Colors.purpleAccent),
child: Text("This is my times"),
);
}
}
class MyDivision extends StatefulWidget {
@override
_MyDivisionState createState() => _MyDivisionState();
}
class _MyDivisionState extends State<MyDivision> {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.limeAccent,
),
child: Text("This is my division"),
);
}
}
在此提供任何帮助或建议都非常感谢。谢谢。