Reputation: 37
I want when I press the button, the text will immediately turn into OK
A hint appears setState "The declaration 'setState' isn't referenced"
class mainApp extends StatefulWidget {
@override
_State createState() => _State();
}
class _State extends State<mainApp> {
@override
Widget build(BuildContext context) {
. return Scaffold(
. . ...
. . body: pageview(),
. );
}
}
Widget pageview() {
bool BOOL = false;
return Row( children: [
FlatButton( onPressed: () { setState(){}; BOOL= true; } ,child: Text('OK') ),
Text( textFun(BOOL), ),
]);
}
String(bool BOOL) {
if(BOOL)
return 'OK';
return 'not OK';
}
Upvotes: 1
Views: 1354
Reputation: 37
I found the solution as follows: Add a new function to State class, and call it with VoidCallback
bool BOOL = false;
class mainApp extends StatefulWidget {
@override
_State createState() => _State();
}
class _State extends State<mainApp> {
@override
Widget build(BuildContext context) {
. return Scaffold(
. . ...
. . body: pageview(fun),
. );
}
void fun(){
setState(() {
BOOL = true;
});
}
}
Widget pageview(VoidCallback fun) {
return Row( children: [
FlatButton( onPressed: fun ,child: Text('OK') ),
Text( textFun(BOOL), ),
]);
}
String(bool BOOL) {
if(BOOL)
return 'OK';
return 'not OK';
}
Upvotes: 2
Reputation: 3136
What you are doing is setting a state without changes and then you change your BOOL
variable.
First make changes, then after that you call setState
, or change your BOOL inside setState
Upvotes: 0