简体   繁体   English

Flutter 登录注册认证

[英]Flutter Login and Sign up Authentication

I'm new to flutter and i need to authenticate the login and sign up pages.我是 flutter 的新手,我需要验证登录和注册页面。 I have the UI ready but i don't know how to authenticate the pages.我已经准备好用户界面,但我不知道如何对页面进行身份验证。 I have a database ready in my phpAdmin我的 phpAdmin 中准备了一个数据库

So you mean your form is not working?所以你的意思是你的表格不起作用? Please check the code which works for me with Firebase. Maybe it would be helpful请使用 Firebase 检查对我有用的代码。也许会有帮助

import 'package:flutter/material.dart';

class Login extends StatefulWidget {
  const Login({Key? key}) : super(key: key);

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  final _formKey = GlobalKey<FormState>();

  TextEditingController clientEmail = TextEditingController();
  TextEditingController clientPassword = TextEditingController();

  bool _obscureText = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('LoginView'),
          centerTitle: true,
        ),
        body: SingleChildScrollView(
          child: Form(
            key: _formKey,
            child: Column(
              children: [
                SizedBox(
                  height: 50,
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(8, 0, 8, 8),
                  child: Card(
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15.0),
                    ),
                    elevation: 3,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: TextFormField(
                        decoration: InputDecoration(
                          prefixIcon: Icon(Icons.mail),
                          border: InputBorder.none,
                          labelText: 'E-Mail',
                          labelStyle: TextStyle(
                              fontSize: 12, fontWeight: FontWeight.bold),
                        ),
                        onSaved: (value) {
                          clientEmail = value as TextEditingController;
                        },
                        controller: clientEmail,
                        textInputAction: TextInputAction.next,
                        validator: ((value) {
                          if (value!.isEmpty || !value.contains('@')) {
                            return 'Please enter E-Mail Address ';
                          }
                          return null;
                        }),
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(8, 0, 8, 8),
                  child: Card(
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15.0),
                    ),
                    elevation: 3,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: TextFormField(
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          prefixIcon: Icon(Icons.lock),
                          suffixIcon: GestureDetector(
                            onTap: () {
                              setState(() {
                                _obscureText = !_obscureText;
                              });
                            },
                            child: Icon(_obscureText
                                ? Icons.visibility
                                : Icons.visibility_off),
                          ),
                          labelText: 'Password',
                          labelStyle: TextStyle(
                              fontSize: 12, fontWeight: FontWeight.bold),
                        ),
                        onSaved: (value) {
                          clientPassword = value as TextEditingController;
                        },
                        obscureText: _obscureText,
                        controller: clientPassword,
                        textInputAction: TextInputAction.next,
                        validator: ((value) {
                          if (value!.isEmpty || value.length < 7) {
                            return 'Please enter your Password, min 7 digits';
                          }
                          return null;
                        }),
                      ),
                    ),
                  ),
                ),
                ElevatedButton(
                    onPressed: () {
                      if (_formKey.currentState!.validate()) {
                        // here would be your function which saves the data to yout database
                        //for example
                        // addUser();
                      }
                    },
                    child: Text('Login')),
              ],
            ),
          ),
        ));
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM