我试图在2秒后将一个 bool(boolean) 值设置为false(showSpinner),但无法处理,主要思想是比显示解决方案显示加载微调器2秒钟,但旋转器永不停止且解决方案从不显示(旋转器正在加载微调器,文字=解决方案)
@override
void initState(){
super.initState();
showSpinner=true;
Timer(Duration(seconds: 2),(){
setState(() {
showSpinner=false;
});
});
}
Widget build(BuildContext context) {
Widget child;
if (showSpinner == true && isPressed4 == true) {
setState(() {
Timer(Duration(seconds: 2), () {
showSpinner == false;
});
});
child = spinkit;
}
if (showSpinner == false && isPressed4 == true) {
text = simpleInterest.accumulationFunction(
time, yearlySimpleInterestRate, principal);
child = Text(
text,
style: TextStyle(fontSize: 18),
textAlign: TextAlign.center,
);
}
有3个按钮(如果按钮true为true,则按钮1为isPressed1,按钮2为isPressed2,按钮3为isPressd3)
var floatingActionButton1 = FloatingActionButton(
onPressed: () {
setState(() {
isPressed1 = !isPressed1;
});
setState(() {
principal = double.parse(_principalController.text);
});
setState(() {
if (isPressed3 == true && isPressed2 == true && isPressed1 == true) {
isPressed4 = true;
}
});
},
elevation: 40,
backgroundColor: isPressed1 ? Colors.lightGreenAccent : null,
heroTag: "btn1",
child: Icon(Icons.check),
);
最佳答案
我不知道spinkit
和isPressed4
是什么,但是我会这样做:
bool showSpinner;
@override
void initState() {
super.initState();
showSpinner = true;
Timer(Duration(seconds: 2), () {
setState(() {
showSpinner = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: buildBody()
);
}
Widget buildBody(){
return showSpinner ?
CircularProgressIndicator() :
Text(
'The answer',
);
}
UPD:
最初我有2秒。然后:
showSpinner == false
和floatingActionButton1
,那么我会收到文本The time is up!
。 showSpinner == true
和floatingActionButton1
,那么我会收到文本Button 4 is not pressed yet
。 showSpinner == true
和floatingActionButton1
,那么我会得到CircularProgressIndicator()
(如问题中所述)。 showSpinner == false
和floatingActionButton1
,那么我得到带有Text
的The answer
(如问题中所示): bool showSpinner;
var floatingActionButton1;
bool isPressed1;
bool isPressed2;
bool isPressed3;
bool isPressed4;
@override
void initState() {
super.initState();
showSpinner = true;
isPressed1 = false;
isPressed2 = true;
isPressed3 = true;
isPressed4 = false;
floatingActionButton1 = FloatingActionButton(
onPressed: () {
setState(() {
isPressed1 = !isPressed1;
if (isPressed3 == true && isPressed2 == true && isPressed1 == true) {
isPressed4 = true;
}
});
},
backgroundColor: isPressed1 ? Colors.lightGreenAccent : null,
child: Icon(Icons.check),
);
Timer(Duration(seconds: 2), () {
setState(() {
showSpinner = false;
});
});
}
Widget build(BuildContext context) {
Widget child;
if(showSpinner == false && isPressed4 == false)
child = Text('The time is up!');
else if(showSpinner == true && isPressed4 == false)
child = Text('Button 4 is not pressed yet');
else if (showSpinner == true && isPressed4 == true) {
child = CircularProgressIndicator();
}
else if (showSpinner == false && isPressed4 == true) {
child = Text(
'The answer',
style: TextStyle(fontSize: 18),
textAlign: TextAlign.center,
);
}
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: child,
floatingActionButton: floatingActionButton1,
);
}