class - 随机数学应用程序颤动-有更好的方法吗?
问题描述
我已经开始创建一个随机数学应用程序,用户首先可以选择算术。之后将显示该算术的随机计数,用户将使用键盘填写答案。
现在我不确定将每个算术放在一个自己的类中(并在那里获取随机数)是最好的方法,还是有更好的方法?
我试图将所有算术选择添加到一个类中,但我无法让它工作。我将不得不以某种方式将每个选择传递给同一班级中的正确计算。
我在想一个计算器,但你先把算术放在一个单独的页面上。这甚至会起作用吗?
是否可以将每个班级的输出收集到一个班级,这样我只需要创建一个键盘(即使我使用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"),
);
}
}
非常感谢这里的任何帮助或建议。谢谢你。
解决方案
推荐阅读
- html - 使图像适合父级并在其上添加铭牌
- html - 如何用css添加第二个背景?
- sql - postgres 仅在不存在时创建用户
- great-expectations - 对流数据使用很高的期望
- python - Django未检测模型中的日期时间字段
- visual-studio - 将变量从 Fortran DLL 传递到主程序的问题
- node.js - Dockerfile 中 nats-server 的 CMD 命令是什么
- java - 有没有办法从另一个 Android 应用程序读取来自 Facebook Messenger 的消息?
- r - x轴名称和颜色变化
- javascript - 如何使用 javascript/Jquery 在通知托盘中显示我的音乐播放器网站的媒体控件?