因此,我的程序的想法是使用运算符生成随机数,然后对该操作生成非用户结果。它可以正常工作,但我的应用程序每次执行任何操作时]都会刷新数据,例如打开TextField或提交结果以将用户输入与实际答案进行比较。我想问题是由于国家管理不善造成的,但我自己想办法如何解决。(我有自己的数学课程,带有程序功能,但无需担心)
代码是什么必需
:class _GameplayPageState extends State<GameplayPage> { @override Widget build(BuildContext context) { var mathManager = Mathematics(); var firstNum = mathManager.getNumber(); var secondNum = mathManager.getNumber(); var mathOperator = mathManager.getOperator(); var calResult = mathManager.calculate(firstNum, mathOperator, secondNum); Positioned( top: 120, child: Container( width: screenWidth, height: 230, // decoration: BoxDecoration(color: Colors.orange), child: Align( alignment: Alignment.center, child: Container( child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[ SizedBox( width: 10, ), // >>>> LOOK HERE Text( firstNum.toString(), style: TextStyle( fontFamily: 'Montserrat', fontSize: 100, color: Color(0xff52de97), letterSpacing: 0.5), ), Text( mathOperator, style: TextStyle( fontFamily: 'Montserrat', fontSize: 70, color: Color(0xff52de97), ), ), Text( secondNum.toString(), style: TextStyle( fontFamily: 'Montserrat', fontSize: 100, color: Color(0xff52de97), letterSpacing: 0.5, ), ), SizedBox( width: 10, ), ], ), ), ), ), ), child: TextField( onSubmitted: (String value) async { await showDialog( context: context, builder: (BuildContext context) { return AlertDialog( content: (value == calResult ? Text('Right') : Text('Wrong!')), ); }); }, FlatButton( onPressed: () { setState(() { }); },
对不起,格式不好;(
因此,我的程序的想法是使用运算符生成随机数,然后对该操作生成非用户结果。它可以工作,但是我的应用程序每次执行任何操作时都会刷新数据...
您可以尝试创建一个单独的stateful
我不确定您要实现的目标,但是您可以尝试以下简单代码,在检查输入是否正确之后,该代码不会生成新数字,并且还具有新的生成数字按钮
因此,我通过将数学对象分解成几个函数,然后在Widget build
之前做了最艰巨的工作,解决了这个问题。代码: