[英]Widgets shifted down when error shows for TextFormField on button's press
I have created a login page in Flutter.我在 Flutter 中创建了一个登录页面。 I have used
SingleChildScrollView
to make this page scrollable.我使用
SingleChildScrollView
使此页面可滚动。 I have used the form validation to validate the username and password text form fields.我使用表单验证来验证用户名和密码文本表单字段。 The problem is when error messages displayed below
TextFormField
, the login button is shifted down.问题是当
TextFormField
下方显示错误消息时,登录按钮会向下移动。
I have also used ListView
to enable the scrolling but the behavior is the same as far as shifting is concerned.我还使用
ListView
来启用滚动,但就移动而言,行为是相同的。
I am new to Flutter and seeking help in this.我是 Flutter 的新手,并在这方面寻求帮助。 I am providing the code and screenshot below.
我在下面提供代码和屏幕截图。
Thank you.谢谢你。
Screenshot截屏
Login Page登录页面
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'login_form.dart';
class LoginPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(child: LoginForm()),
);
}
}
Login Form登录表单
import 'package:flutter/material.dart';
class LoginForm extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _LoginFormState();
}
}
class _LoginFormState extends State<LoginForm> {
TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0);
OutlineInputBorder textFieldBorder = OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(color: const Color(0xff01d277)));
final formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
/// Username field
final usernameField = TextFormField(
obscureText: false,
style: style,
validator: (value) {
if (value.isEmpty) return 'Please enter username';
return null;
},
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: 'Username',
border: textFieldBorder,
));
/// Password field
final passwordField = TextFormField(
obscureText: true,
style: style,
validator: (value) {
if (value.isEmpty) return 'Please enter password';
return null;
},
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: 'Password',
border: textFieldBorder,
));
/// Login button
final loginButton = Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Color(0xff01d277),
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: () {
if (formKey.currentState.validate())
Scaffold.of(context).showSnackBar(SnackBar(content: Text('Done!')));
},
child: Text("Login",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white, fontWeight: FontWeight.bold)),
),
);
return Padding(
padding: const EdgeInsets.all(36.0),
child: Form(
key: formKey,
child: Column(
children: <Widget>[
SizedBox(
height: 150,
child: Image.asset(
'lib/assets/images/logo_dark.png',
fit: BoxFit.contain,
),
),
SizedBox(height: 45.0),
usernameField,
SizedBox(
height: 45.0,
),
passwordField,
SizedBox(
height: 45.0,
),
loginButton,
],
)));
}
}
Use ConstrainedBox with fixed maxHeight and minHeight使用具有固定 maxHeight 和 minHeight 的 ConstrainedBox
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: 70.0,
minHeight: 70.0,
child: TextFormField(...),
),
);
Use 'Container' widget to set full height.使用“容器”小部件设置全高。
Container(
width: double.infinity,
height: 73, //(text field height + error label height)
child: TextFormField(<your Text Form Field>)
)
Try wrapping the usernameField and passwordField in an Expanded widget尝试将 usernameField 和 passwordField 包装在 Expanded 小部件中
Expanded(child: usernameField),
SizedBox(
height: 45.0,
),
Expanded(child: passwordField),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.