From 40f502eb591eb1cd28f0dd0b46eaa46f5c90f394 Mon Sep 17 00:00:00 2001 From: toly <1981462002@qq.com> Date: Tue, 18 Apr 2023 13:04:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E7=BB=93=E6=9E=9C=E4=B8=8E?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/guess/guess_page.dart | 47 +++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/guess/guess_page.dart b/lib/guess/guess_page.dart index 7acb2fa..4e50669 100644 --- a/lib/guess/guess_page.dart +++ b/lib/guess/guess_page.dart @@ -19,7 +19,7 @@ class _GuessPageState extends State { Random _random = Random(); bool _guessing = false; - + bool? _isBig; @override void dispose() { @@ -37,8 +37,24 @@ class _GuessPageState extends State { TextEditingController _guessCtrl = TextEditingController(); - void _onCheck(){ + void _onCheck() { print("=====Check:目标数值:$_value=====${_guessCtrl.text}============"); + int? guessValue = int.tryParse(_guessCtrl.text); + // 游戏未开始,或者输入非整数,无视 + if (!_guessing || guessValue == null) return; + + //猜对了 + if (guessValue == _value) { + setState(() { + _isBig = null; + _guessing = false; + }); + return; + } + // 猜错了 + setState(() { + _isBig = guessValue > _value; + }); } @override @@ -50,21 +66,28 @@ class _GuessPageState extends State { ), body: Stack( children: [ - // Column( - // children: [ - // ResultNotice(color:Colors.redAccent,info:'大了'), - // ResultNotice(color:Colors.blueAccent,info:'小了'), - // ], - // ), + if(_isBig!=null) + Column( + children: [ + if(_isBig!) + ResultNotice(color:Colors.redAccent,info:'大了'), + Spacer(), + if(!_isBig!) + ResultNotice(color:Colors.blueAccent,info:'小了'), + ], + ), Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - if(!_guessing) - const Text('点击生成随机数值',), + if (!_guessing) + const Text( + '点击生成随机数值', + ), Text( _guessing ? '**' : '$_value', - style: const TextStyle(fontSize: 68, fontWeight: FontWeight.bold), + style: const TextStyle( + fontSize: 68, fontWeight: FontWeight.bold), ), ], ), @@ -72,7 +95,7 @@ class _GuessPageState extends State { ], ), floatingActionButton: FloatingActionButton( - onPressed: _guessing?null:_generateRandomValue, + onPressed: _guessing ? null : _generateRandomValue, backgroundColor: _guessing ? Colors.grey : Colors.blue, tooltip: 'Increment', child: const Icon(Icons.generating_tokens_outlined),