반응형

To create a login page in Flutter that allows the user to enter their email and password, you can follow these steps:

  1. First, create a new Flutter project.
  2. Create a new file called login_page.dart and define a LoginPage widget. This widget will contain the login form.
  3. In the build method of the LoginPage widget, use a Scaffold widget as the root of your widget tree. This will provide you with a default app bar.
  4. Add a TextField widget for the user to enter their email address and a TextField widget for the user to enter their password.
  5. Add a login button below the text fields. When the user clicks the login button, you can validate the email and password and proceed with the login process.

Here is some example code to get you started:

 

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;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Login'),
      ),
      body: Form(
        key: _formKey,
        child: Padding(
          padding: EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              TextFormField(
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Please enter your email';
                  }
                  return null;
                },
                onSaved: (value) => _email = value,
                decoration: InputDecoration(
                  hintText: 'Enter your email',
                ),
              ),
              SizedBox(
                height: 8.0,
              ),
              TextFormField(
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Please enter your password';
                  }
                  return null;
                },
                onSaved: (value) => _password = value,
                obscureText: true,
                decoration: InputDecoration(
                  hintText: 'Enter your password',
                ),
              ),
              SizedBox(
                height: 24.0,
              ),
              ButtonTheme(
                height: 40.0,
                child: RaisedButton(
                  onPressed: () {
                    final form = _formKey.currentState;
                    if (form.validate()) {
                      form.save();
                      // Perform login
                    }
                  },
                  child: Text('Log In'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
반응형

'[====== Development ======] > Flutter' 카테고리의 다른 글

Visual Studio Code Extensions  (0) 2023.01.26
rounded card list in Flutter  (0) 2023.01.24
Flutter - UI Clone Coding  (0) 2023.01.08
Flutter - Login with firebase  (0) 2023.01.08
chatting ui by flutter  (0) 2023.01.08

+ Recent posts