状态数据与界面更新 -1

This commit is contained in:
toly
2023-04-18 09:41:37 +08:00
parent d0d3f9476b
commit 6c72e7fb8f

View File

@@ -18,10 +18,13 @@ class _GuessPageState extends State<GuessPage> {
int _value = 0; int _value = 0;
Random _random = Random(); Random _random = Random();
bool _guessing = false;
void _generateRandomValue() { void _generateRandomValue() {
setState(() { setState(() {
_guessing = true;
_value = _random.nextInt(100); _value = _random.nextInt(100);
print(_value);
}); });
} }
@@ -31,22 +34,21 @@ class _GuessPageState extends State<GuessPage> {
appBar: GuessAppBar(), appBar: GuessAppBar(),
body: Stack( body: Stack(
children: [ children: [
Column( // Column(
children: [ // children: [
ResultNotice(color:Colors.redAccent,info:'大了'), // ResultNotice(color:Colors.redAccent,info:'大了'),
ResultNotice(color:Colors.blueAccent,info:'小了'), // ResultNotice(color:Colors.blueAccent,info:'小了'),
], // ],
), // ),
Center( Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
const Text( if(!_guessing)
'点击生成随机数值', const Text('点击生成随机数值',),
),
Text( Text(
'$_value', _guessing ? '**' : '$_value',
style: Theme.of(context).textTheme.headlineMedium, style: const TextStyle(fontSize: 68, fontWeight: FontWeight.bold),
), ),
], ],
), ),
@@ -54,7 +56,8 @@ class _GuessPageState extends State<GuessPage> {
], ],
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: _generateRandomValue, onPressed: _guessing?null:_generateRandomValue,
backgroundColor: _guessing ? Colors.grey : Colors.blue,
tooltip: 'Increment', tooltip: 'Increment',
child: const Icon(Icons.generating_tokens_outlined), child: const Icon(Icons.generating_tokens_outlined),
), ),