[英]How to set A Button or Text Widget in Flutter At the bottom of the screen
I'm trying to make a LoginPage in Flutter.我正在尝试在 Flutter 中创建一个 LoginPage。 I Just want to place "Terms of Service. Privacy Policy."
我只想放置“服务条款。隐私政策”。 Flatbutton At the end of the screen.
Flatbutton 在屏幕的末尾。 I tried many things but nothing seems to work!
我尝试了很多东西,但似乎没有任何效果!
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Expense Tracker',
home: LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
// String titleInput;
// String amountInput;
@override
_LoginPage createState() => _LoginPage();
}
class _LoginPage extends State<LoginPage> {
final userName = TextEditingController();
final password = TextEditingController();
String textDisplay = "";
void onSubmit() {
setState(() {
if (userName == null) {
textDisplay = "Invalid Username";
} else if (password == null && userName != null) {
textDisplay = "Invalid Password";
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('LOGIN'),
),
body: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: EdgeInsets.all(10),
child: Image.asset("assets/images/nike.png"),
),
Container(
padding: EdgeInsets.all(10),
child: Column(
children: [
TextField(
controller: userName,
decoration: InputDecoration(
labelText: "Username",
labelStyle: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
keyboardType: TextInputType.emailAddress,
onSubmitted: (_) => onSubmit(),
),
TextField(
controller: password,
decoration: InputDecoration(
labelText: "Password",
labelStyle: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
keyboardType: TextInputType.visiblePassword,
onSubmitted: (_) => onSubmit(),
),
Text(
textDisplay,
style: TextStyle(
color: Colors.redAccent[700],
),
),
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
FlatButton(
child: Text(
"Not a User. Sign Up Here.",
style: TextStyle(
color: Colors.black,
fontSize: 15,
),
),
onPressed: () {},
),
FlatButton(
child: Text(
"Terms of Service. Privacy Policy.",
style: TextStyle(
color: Colors.grey[700],
fontSize: 13,
),
),
onPressed: () {},
),
],
)
],
),
)
],
),
),
);
}
}
SetState(() {})
Also doen't work. SetState(() {})
也不行。 Please explain the answer too as i'm still learning Flutter.请解释答案,因为我还在学习 Flutter。
If there are any good changes in my code to make my app better please suggest them.如果我的代码中有任何好的更改以使我的应用程序更好,请提出建议。
If you separate the contents of the column into two child elements and use mainAxisAlignment: MainAxisAlignment.spaceBetween
, you will get the desired result, for the setState part TextEditingController()
has a property of text.如果将列的内容分成两个子元素并使用
mainAxisAlignment: MainAxisAlignment.spaceBetween
,您将获得所需的结果,因为 setState 部分TextEditingController()
具有文本属性。 Also you need to exchange SingleChildScrollView()
with CustomScrollView, as per this answer: https://stackoverflow.com/a/62097942/13315601您还需要根据以下答案将
SingleChildScrollView()
与 CustomScrollView 交换: https://stackoverflow.com/a/62097942/13315601
See the reworked code below:请参阅下面的重新编写的代码:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Expense Tracker',
home: LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
// String titleInput;
// String amountInput;
@override
_LoginPage createState() => _LoginPage();
}
class _LoginPage extends State<LoginPage> {
final userName = TextEditingController();
final password = TextEditingController();
String textDisplay = "";
void onSubmit() {
setState(() {
print(userName.text);
if (userName.text.length == 0) {
textDisplay = "Invalid Username";
} else if (password.text.length == 0 && userName.text.length > 0) {
textDisplay = "Invalid Password";
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('LOGIN'),
),
body: CustomScrollView(slivers: [
SliverFillRemaining(
hasScrollBody: false,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: EdgeInsets.all(10),
child: Column(
children: [
Image.asset("assets/images/nike.png"),
TextField(
controller: userName,
decoration: InputDecoration(
labelText: "Username",
labelStyle: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
keyboardType: TextInputType.emailAddress,
onSubmitted: (_) => onSubmit(),
),
TextField(
controller: password,
decoration: InputDecoration(
labelText: "Password",
labelStyle: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
keyboardType: TextInputType.visiblePassword,
onSubmitted: (_) => onSubmit(),
),
Text(
textDisplay,
style: TextStyle(
color: Colors.redAccent[700],
),
),
],
),
),
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FlatButton(
child: Text(
"Not a User. Sign Up Here.",
style: TextStyle(
color: Colors.black,
fontSize: 15,
),
),
onPressed: () {},
),
FlatButton(
child: Text(
"Terms of Service. Privacy Policy.",
style: TextStyle(
color: Colors.grey[700],
fontSize: 13,
),
),
onPressed: () {},
),
],
)
],
),
)
]),
);
}
}
If you want to properly validate the username and password you should use form validation, like: https://flutter.dev/docs/cookbook/forms/validation如果您想正确验证用户名和密码,您应该使用表单验证,例如: https://flutter.dev/docs/cookbook/forms/validation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.