Flutter/Dart: Button onPressed() called but screen not redrawing?
I am trying to have a button change its text when it is pressed. I have done so by using setState() to change the value of the string that is passed to the Text child of the button. The button I am trying to change the text of is forgotLabel - found in the code below.
I have tried using different button types such as FlatButton and RaisedButton and all of them resulted in the same issue: The button gets pressed, setState gets called and changes the value of the string that represents the text in the button (called forgotText in the code) but the button does not appear to visually change. Here is the code:
import 'package:flutter/material.dart';
import 'package:mobileapp/home_page.dart';
class LoginPage extends StatefulWidget {
static String tag = 'login-page';
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
final bgGrey = Color(0xFF1c232c);
//Company logo at top
final logo = Hero(
tag: 'hero',
child: CircleAvatar(
backgroundColor: Colors.transparent,
radius:68.0,
child: Image.asset('assets/logo_transparent.png'),
),
);
//Email field
final email = TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
initialValue: 'jonDoe@domain.com', //remove later
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
hintText: 'Email',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(color: Colors.white)
)
),
);
//Password field
final password = TextFormField(
autofocus: false,
initialValue: 'verysafePassword', //remove later
obscureText: true,
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
hintText: 'Password',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(color: Colors.white)
)
),
);
// Login button
final loginButton = Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: Material(
borderRadius: BorderRadius.circular(30.0),
color: Colors.deepOrangeAccent,
shadowColor: Colors.deepOrange[300],
elevation: 5.0,
child: MaterialButton(
minWidth: 200.0,
height: 30.0,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage()),
);
},
child: Text('Login', style: TextStyle(color: Colors.white70),),
),
)
);
String forgotText = "Forgot Password";
void changeText() {
setState(() =>forgotText = "Please use the web app to reset your password.");
}
// Forgot password button
final forgotLabel = MaterialButton(
child: Text(
forgotText,
style: TextStyle(color: Colors.white70),
),
onPressed: changeText,
);
//Layout settings for each widget made
//Contains: logo, email, password, loginButton and forgotLabel
return Scaffold(
backgroundColor: bgGrey, //defined under method declaration
body: Center(
child: ListView(
shrinkWrap: true,
padding: EdgeInsets.only(left: 24.0, right: 24.0),
children: <Widget>[
logo,
SizedBox(height: 48.0),
email,
SizedBox(height: 8.0),
password,
SizedBox(height: 25.0),
loginButton,
forgotLabel
],
),
),
);
}
}
add a comment |
I am trying to have a button change its text when it is pressed. I have done so by using setState() to change the value of the string that is passed to the Text child of the button. The button I am trying to change the text of is forgotLabel - found in the code below.
I have tried using different button types such as FlatButton and RaisedButton and all of them resulted in the same issue: The button gets pressed, setState gets called and changes the value of the string that represents the text in the button (called forgotText in the code) but the button does not appear to visually change. Here is the code:
import 'package:flutter/material.dart';
import 'package:mobileapp/home_page.dart';
class LoginPage extends StatefulWidget {
static String tag = 'login-page';
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
final bgGrey = Color(0xFF1c232c);
//Company logo at top
final logo = Hero(
tag: 'hero',
child: CircleAvatar(
backgroundColor: Colors.transparent,
radius:68.0,
child: Image.asset('assets/logo_transparent.png'),
),
);
//Email field
final email = TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
initialValue: 'jonDoe@domain.com', //remove later
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
hintText: 'Email',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(color: Colors.white)
)
),
);
//Password field
final password = TextFormField(
autofocus: false,
initialValue: 'verysafePassword', //remove later
obscureText: true,
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
hintText: 'Password',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(color: Colors.white)
)
),
);
// Login button
final loginButton = Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: Material(
borderRadius: BorderRadius.circular(30.0),
color: Colors.deepOrangeAccent,
shadowColor: Colors.deepOrange[300],
elevation: 5.0,
child: MaterialButton(
minWidth: 200.0,
height: 30.0,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage()),
);
},
child: Text('Login', style: TextStyle(color: Colors.white70),),
),
)
);
String forgotText = "Forgot Password";
void changeText() {
setState(() =>forgotText = "Please use the web app to reset your password.");
}
// Forgot password button
final forgotLabel = MaterialButton(
child: Text(
forgotText,
style: TextStyle(color: Colors.white70),
),
onPressed: changeText,
);
//Layout settings for each widget made
//Contains: logo, email, password, loginButton and forgotLabel
return Scaffold(
backgroundColor: bgGrey, //defined under method declaration
body: Center(
child: ListView(
shrinkWrap: true,
padding: EdgeInsets.only(left: 24.0, right: 24.0),
children: <Widget>[
logo,
SizedBox(height: 48.0),
email,
SizedBox(height: 8.0),
password,
SizedBox(height: 25.0),
loginButton,
forgotLabel
],
),
),
);
}
}
1
define - String forgotText = "Forgot Password"; outside the Build method -
– anmol.majhail
Nov 17 '18 at 16:49
add a comment |
I am trying to have a button change its text when it is pressed. I have done so by using setState() to change the value of the string that is passed to the Text child of the button. The button I am trying to change the text of is forgotLabel - found in the code below.
I have tried using different button types such as FlatButton and RaisedButton and all of them resulted in the same issue: The button gets pressed, setState gets called and changes the value of the string that represents the text in the button (called forgotText in the code) but the button does not appear to visually change. Here is the code:
import 'package:flutter/material.dart';
import 'package:mobileapp/home_page.dart';
class LoginPage extends StatefulWidget {
static String tag = 'login-page';
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
final bgGrey = Color(0xFF1c232c);
//Company logo at top
final logo = Hero(
tag: 'hero',
child: CircleAvatar(
backgroundColor: Colors.transparent,
radius:68.0,
child: Image.asset('assets/logo_transparent.png'),
),
);
//Email field
final email = TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
initialValue: 'jonDoe@domain.com', //remove later
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
hintText: 'Email',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(color: Colors.white)
)
),
);
//Password field
final password = TextFormField(
autofocus: false,
initialValue: 'verysafePassword', //remove later
obscureText: true,
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
hintText: 'Password',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(color: Colors.white)
)
),
);
// Login button
final loginButton = Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: Material(
borderRadius: BorderRadius.circular(30.0),
color: Colors.deepOrangeAccent,
shadowColor: Colors.deepOrange[300],
elevation: 5.0,
child: MaterialButton(
minWidth: 200.0,
height: 30.0,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage()),
);
},
child: Text('Login', style: TextStyle(color: Colors.white70),),
),
)
);
String forgotText = "Forgot Password";
void changeText() {
setState(() =>forgotText = "Please use the web app to reset your password.");
}
// Forgot password button
final forgotLabel = MaterialButton(
child: Text(
forgotText,
style: TextStyle(color: Colors.white70),
),
onPressed: changeText,
);
//Layout settings for each widget made
//Contains: logo, email, password, loginButton and forgotLabel
return Scaffold(
backgroundColor: bgGrey, //defined under method declaration
body: Center(
child: ListView(
shrinkWrap: true,
padding: EdgeInsets.only(left: 24.0, right: 24.0),
children: <Widget>[
logo,
SizedBox(height: 48.0),
email,
SizedBox(height: 8.0),
password,
SizedBox(height: 25.0),
loginButton,
forgotLabel
],
),
),
);
}
}
I am trying to have a button change its text when it is pressed. I have done so by using setState() to change the value of the string that is passed to the Text child of the button. The button I am trying to change the text of is forgotLabel - found in the code below.
I have tried using different button types such as FlatButton and RaisedButton and all of them resulted in the same issue: The button gets pressed, setState gets called and changes the value of the string that represents the text in the button (called forgotText in the code) but the button does not appear to visually change. Here is the code:
import 'package:flutter/material.dart';
import 'package:mobileapp/home_page.dart';
class LoginPage extends StatefulWidget {
static String tag = 'login-page';
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
final bgGrey = Color(0xFF1c232c);
//Company logo at top
final logo = Hero(
tag: 'hero',
child: CircleAvatar(
backgroundColor: Colors.transparent,
radius:68.0,
child: Image.asset('assets/logo_transparent.png'),
),
);
//Email field
final email = TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
initialValue: 'jonDoe@domain.com', //remove later
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
hintText: 'Email',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(color: Colors.white)
)
),
);
//Password field
final password = TextFormField(
autofocus: false,
initialValue: 'verysafePassword', //remove later
obscureText: true,
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
hintText: 'Password',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(color: Colors.white)
)
),
);
// Login button
final loginButton = Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: Material(
borderRadius: BorderRadius.circular(30.0),
color: Colors.deepOrangeAccent,
shadowColor: Colors.deepOrange[300],
elevation: 5.0,
child: MaterialButton(
minWidth: 200.0,
height: 30.0,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage()),
);
},
child: Text('Login', style: TextStyle(color: Colors.white70),),
),
)
);
String forgotText = "Forgot Password";
void changeText() {
setState(() =>forgotText = "Please use the web app to reset your password.");
}
// Forgot password button
final forgotLabel = MaterialButton(
child: Text(
forgotText,
style: TextStyle(color: Colors.white70),
),
onPressed: changeText,
);
//Layout settings for each widget made
//Contains: logo, email, password, loginButton and forgotLabel
return Scaffold(
backgroundColor: bgGrey, //defined under method declaration
body: Center(
child: ListView(
shrinkWrap: true,
padding: EdgeInsets.only(left: 24.0, right: 24.0),
children: <Widget>[
logo,
SizedBox(height: 48.0),
email,
SizedBox(height: 8.0),
password,
SizedBox(height: 25.0),
loginButton,
forgotLabel
],
),
),
);
}
}
asked Nov 17 '18 at 16:41
ibai joe
31
31
1
define - String forgotText = "Forgot Password"; outside the Build method -
– anmol.majhail
Nov 17 '18 at 16:49
add a comment |
1
define - String forgotText = "Forgot Password"; outside the Build method -
– anmol.majhail
Nov 17 '18 at 16:49
1
1
define - String forgotText = "Forgot Password"; outside the Build method -
– anmol.majhail
Nov 17 '18 at 16:49
define - String forgotText = "Forgot Password"; outside the Build method -
– anmol.majhail
Nov 17 '18 at 16:49
add a comment |
1 Answer
1
active
oldest
votes
The problem is forgotText was changed when calling setState but because it's defined as a local variable inside build(), it gets initialised to Forgot Password again. Make it an instance variable of _LoginPageState.
Thank you. This worked.
– ibai joe
Nov 17 '18 at 17:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53353282%2fflutter-dart-button-onpressed-called-but-screen-not-redrawing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is forgotText was changed when calling setState but because it's defined as a local variable inside build(), it gets initialised to Forgot Password again. Make it an instance variable of _LoginPageState.
Thank you. This worked.
– ibai joe
Nov 17 '18 at 17:11
add a comment |
The problem is forgotText was changed when calling setState but because it's defined as a local variable inside build(), it gets initialised to Forgot Password again. Make it an instance variable of _LoginPageState.
Thank you. This worked.
– ibai joe
Nov 17 '18 at 17:11
add a comment |
The problem is forgotText was changed when calling setState but because it's defined as a local variable inside build(), it gets initialised to Forgot Password again. Make it an instance variable of _LoginPageState.
The problem is forgotText was changed when calling setState but because it's defined as a local variable inside build(), it gets initialised to Forgot Password again. Make it an instance variable of _LoginPageState.
answered Nov 17 '18 at 17:00
stt106
1,2191023
1,2191023
Thank you. This worked.
– ibai joe
Nov 17 '18 at 17:11
add a comment |
Thank you. This worked.
– ibai joe
Nov 17 '18 at 17:11
Thank you. This worked.
– ibai joe
Nov 17 '18 at 17:11
Thank you. This worked.
– ibai joe
Nov 17 '18 at 17:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53353282%2fflutter-dart-button-onpressed-called-but-screen-not-redrawing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
define - String forgotText = "Forgot Password"; outside the Build method -
– anmol.majhail
Nov 17 '18 at 16:49