【问题标题】:Flutter/Dart: Button onPressed() called but screen not redrawing?Flutter/Dart:调用了按钮 onPressed() 但屏幕不重绘?
【发布时间】:2019-04-20 13:15:46
【问题描述】:

我试图让按钮在按下时更改其文本。我通过使用 setState() 更改传递给按钮的 Text 子项的字符串的值来做到这一点。我试图更改其文本的按钮是 forgotLabel - 在下面的代码中找到。

我尝试使用不同的按钮类型,例如 FlatButton 和 RaisedButton,所有这些都导致了相同的问题:按钮被按下,setState 被调用并更改表示按钮中文本的字符串的值(称为 forgotText在代码中),但按钮似乎没有在视觉上发生变化。这是代码:

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
      ],
    ),
  ),
);
}
}

【问题讨论】:

  • define - String forgotText = "忘记密码";在 Build 方法之外 -

标签: android button dart flutter


【解决方案1】:

问题是 forgotText 在调用 setState 时被更改了,但是因为它被定义为 build() 中的局部变量,它再次被初始化为 Forgot Password。使其成为_LoginPageState 的实例变量。

【讨论】:

    猜你喜欢
    • 2020-04-02
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2020-11-06
    • 2017-03-27
    • 1970-01-01
    • 2019-01-05
    相关资源
    最近更新 更多