티스토리 뷰

플러터(Flutter)

플러터 로그인 처리

xemaker 2024. 10. 25. 09:44

login_screen.dart

import 'package:flutter/material.dart';

class LoginScreen extends StatefulWidget {
  const LoginScreen({super.key});

  @override
  State<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  final _nameController = TextEditingController();
  final _emailController = TextEditingController();
  final _formKey = GlobalKey<FormState>();

  String _name = '로그인 해주세요';

  void _onLogin() {
    final form = _formKey.currentState;
    if (form?.validate() ?? false) {
      print('login 성공');
      setState(() {
        _name = '${_nameController.text} 님 로그인 하셨습니다.';
      });
    } else {
      print('login 실패');
    }
  }

  @override
  void dispose() {
    _nameController.dispose();
    _emailController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Please Login'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Container(
          decoration: const BoxDecoration(
            border: Border(
              bottom: BorderSide(
                color: Colors.red, // 테두리 색상
                width: 3, // 테두리 두께
              ),
              top: BorderSide(
                color: Colors.red, // 테두리 색상
                width: 3, // 테두리 두께
              ),
              left: BorderSide(
                color: Colors.red, // 테두리 색상
                width: 3, // 테두리 두께
              ),
              right: BorderSide(
                color: Colors.red, // 테두리 색상
                width: 3, // 테두리 두께
              ),
            ),
          ),
          child: Form(
            key: _formKey,
            child: Column(
              children: [
                TextFormField(
                  controller: _nameController,
                  decoration: const InputDecoration(labelText: '이름'),
                  validator: (text) => text!.isEmpty ? '이름을 입력해 주세요' : null,
                ),
                TextFormField(
                  controller: _emailController,
                  decoration: const InputDecoration(labelText: '이메일'),
                  validator: (text) {
                    if (text!.isEmpty) {
                      return '이메일을 입력해 주세요';
                    }
                    String pattern =
                        r'^[a-zA-Z0-9.a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$';
                    RegExp regex = RegExp(pattern);
                    if (!regex.hasMatch(text)) {
                      return '올바른 이메일을 입력해 주세요';
                    }
                    return null;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                ElevatedButton(
                  onPressed: _onLogin,
                  child: const Text('로그인'),
                ),
                const SizedBox(
                  height: 20,
                ),
                Text(
                  _name,
                  style: Theme.of(context).textTheme.headlineMedium,
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

main.dart

import 'package:flutter/material.dart';

import 'views/login_screen.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
       
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: LoginScreen(),
    );
  }
}

 

 

'플러터(Flutter)' 카테고리의 다른 글

ai 코딩 참고 사이트  (0) 2024.10.25
플러터 로티파일 사용  (0) 2024.10.25
플러터 로그인 구현  (0) 2024.10.24
플러터 이미지 주사위  (0) 2024.10.24
플러터 레이아웃 리팩토링  (0) 2024.10.23
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
글 보관함