Flutter登录,注册示例Demo

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Flutter登录注册界面的示例代码,您可以根据自己的需求进行修改和扩展。 ```dart import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Login Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: LoginPage(), ); } } class LoginPage extends StatefulWidget { @override _LoginPageState createState() => _LoginPageState(); } class _LoginPageState extends State<LoginPage> { final _formKey = GlobalKey<FormState>(); String email; String password; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Login Page'), ), body: Padding( padding: EdgeInsets.all(16.0), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ TextFormField( validator: (value) { if (value.isEmpty) { return 'Please enter your email'; } return null; }, decoration: InputDecoration( labelText: 'Email', ), onSaved: (value) { email = value; }, ), TextFormField( obscureText: true, validator: (value) { if (value.isEmpty) { return 'Please enter your password'; } return null; }, decoration: InputDecoration( labelText: 'Password', ), onSaved: (value) { password = value; }, ), SizedBox( height: 16.0, ), Center( child: RaisedButton( onPressed: () { if (_formKey.currentState.validate()) { _formKey.currentState.save(); // Do something with email and password print('Email: $email'); print('Password: $password'); } }, child: Text('Login'), ), ), SizedBox( height: 16.0, ), Center( child: GestureDetector( onTap: () { // Navigate to registration page Navigator.push( context, MaterialPageRoute( builder: (context) => RegistrationPage(), ), ); }, child: Text( 'Don\'t have an account? Register now!', style: TextStyle( color: Colors.blue, ), ), ), ), ], ), ), ), ); } } class RegistrationPage extends StatefulWidget { @override _RegistrationPageState createState() => _RegistrationPageState(); } class _RegistrationPageState extends State<RegistrationPage> { final _formKey = GlobalKey<FormState>(); String name; String email; String password; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Registration Page'), ), body: Padding( padding: EdgeInsets.all(16.0), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ TextFormField( validator: (value) { if (value.isEmpty) { return 'Please enter your name'; } return null; }, decoration: InputDecoration( labelText: 'Name', ), onSaved: (value) { name = value; }, ), TextFormField( validator: (value) { if (value.isEmpty) { return 'Please enter your email'; } return null; }, decoration: InputDecoration( labelText: 'Email', ), onSaved: (value) { email = value; }, ), TextFormField( obscureText: true, validator: (value) { if (value.isEmpty) { return 'Please enter your password'; } return null; }, decoration: InputDecoration( labelText: 'Password', ), onSaved: (value) { password = value; }, ), SizedBox( height: 16.0, ), Center( child: RaisedButton( onPressed: () { if (_formKey.currentState.validate()) { _formKey.currentState.save(); // Do something with name, email and password print('Name: $name'); print('Email: $email'); print('Password: $password'); } }, child: Text('Register'), ), ), ], ), ), ), ); } } ``` 在上面的代码中,我们创建了两个页面:`LoginPage`和`RegistrationPage`。在`LoginPage`中,我们使用了`TextFormField`小部件来获取用户输入的电子邮件和密码,以及一个`RaisedButton`在提交表单时触发的登录操作。如果用户没有帐户,他们可以点击“没有帐户?现在注册!”链接,然后导航到`RegistrationPage`。 在`RegistrationPage`中,我们使用了三个`TextFormField`小部件来获取用户输入的名称、电子邮件和密码,并使用一个`RaisedButton`在提交表单时触发注册操作。 您可以在这个示例代码的基础上进行修改和扩展,以满足您的具体需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值