帮我用flutter写个登陆
时间: 2023-07-26 17:03:28 浏览: 43
好的,下面是一个简单的Flutter登录页面的代码示例:
```dart
import 'package:flutter/material.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _formKey = GlobalKey<FormState>();
String _email;
String _password;
void _submitForm() {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
// TODO: Perform login operation
print("Email: $_email");
print("Password: $_password");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Login"),
),
body: Padding(
padding: EdgeInsets.all(20.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: "Email"),
validator: (value) {
if (value.isEmpty) {
return "Please enter your email";
}
return null;
},
onSaved: (value) {
_email = value;
},
),
TextFormField(
decoration: InputDecoration(labelText: "Password"),
obscureText: true,
validator: (value) {
if (value.isEmpty) {
return "Please enter your password";
}
return null;
},
onSaved: (value) {
_password = value;
},
),
SizedBox(height: 20.0),
RaisedButton(
onPressed: _submitForm,
child: Text("Login"),
)
],
),
),
),
);
}
}
```
在这个示例中,我们使用了一个带有表单验证和保存的Flutter StatefulWidget来创建登录页面。在表单提交时,我们可以执行实际登录操作。你可以根据自己的需要修改代码。