可用于在 Android 应用程序中登录和注册的示例 Flutter 布局:
代码实现:
import 'package:flutter/material.dart';
class LoginRegisterScreen extends StatefulWidget {
_LoginRegisterScreenState createState() => _LoginRegisterScreenState();
}
class _LoginRegisterScreenState extends State<LoginRegisterScreen> {
final _formKey = GlobalKey<FormState>();
bool _isLoginForm = true;
String _email;
String _password;
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_isLoginForm ? 'Login' : 'Register'),
),
body: Container(
padding: EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(labelText: 'Email'),
validator: (value) =>
value.isEmpty ? 'Email can\'t be empty' : null,
onSaved: (value) => _email = value,
),
SizedBox(height: 16),
TextFormField(
obscureText: true,
decoration: InputDecoration(labelText: 'Password'),
validator: (value) =>
value.isEmpty ? 'Password can\'t be empty' : null,
onSaved: (value) => _password = value,
),
SizedBox(height: 16),
RaisedButton(
onPressed: _submitForm,
child: Text(_isLoginForm ? 'Login' : 'Create Account'),
),
FlatButton(
onPressed: _toggleFormMode,
child: Text(_isLoginForm
? 'Don\'t have an account? Create one'
: 'Already have an account? Sign in'),
),
],
),
),
),
);
}
void _submitForm() {
final form = _formKey.currentState;
if (form.validate()) {
form.save();
// Perform login or registration here
}
}
void _toggleFormMode() {
setState(() {
_isLoginForm = !_isLoginForm;
});
}
}
此布局使用 Form 小部件来显示用于电子邮件和密码输入的文本字段,并使用 RaisedButton 和 FlatButton 小部件来触发登录和注册操作。
_isLoginForm 变量用于在登录和注册表单之间切换,
调用 _submitForm 函数来验证和保存用户的输入。
这只是一个示例,您需要对其进行自定义以满足大家的特定要求