LoginApp - 만들기

송민경's avatar
Apr 12, 2024
LoginApp - 만들기

1. 구조 잡기

notion image
notion image
 

2. size.dart

  • 사이즈 지정해서 재사용하기
const double small_gap = 5.0; const double mwdium_gap = 10.0; const double large_gap = 20.0; const double xlarge_gap = 100.0;

3. HomePage.dart

  • 로그인 후 페이지
import 'package:flutter/material.dart'; class HomePage extends StatelessWidget { const HomePage({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: Container( color: Colors.red, ), ); } }

4. LoginPage.dart

import 'package:flutter/material.dart'; class LoginPage extends StatelessWidget { const LoginPage({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: Container( color: Colors.yellow, ), ); } }

5. main.dart

  • 루트 설정하기
  • login → LoginPage home → HomePage
import 'package:flutter/material.dart'; import 'package:login_app/pages/home_page.dart'; import 'package:login_app/pages/login_page.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( initialRoute: "/login", routes: { "/login": (context) => LoginPage(), "/home": (context) => HomePage(), }, ); } }
notion image
 

6. svg 라이브러리 다운받아 사용하기

notion image
notion image
notion image
notion image
notion image
  • 터미널 창에 명령어 입력하기
    • flutter pub add flutter_svg
notion image
  • pubspec.yml에 추가 된 것 확인
notion image
 

7. Logo.dart

import 'package:flutter/cupertino.dart'; import 'package:flutter_svg/flutter_svg.dart'; class Logo extends StatelessWidget { final String title; const Logo(this.title); @override Widget build(BuildContext context) { return Column( children: [ SvgPicture.asset( "assets/logo.svg", height: 70, width: 70, ), Text( title, style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold), ), ], ); } }
notion image
 

8. CustomTextFormField.dart

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:login_app/size.dart'; class CustomTextFormField extends StatelessWidget { final String text; const CustomTextFormField(this.text); @override Widget build(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(text), SizedBox(height: small_gap), TextFormField( validator: (value) => value!.isEmpty ? "Please enter some text" : null, obscureText: text == "Password" ? true : false, decoration: InputDecoration( hintText: "Enter $text", enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(20), ), errorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(20), ), focusedErrorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(20), ), ), ) ], ); } }
 

9. CustomForm.dart

import 'package:flutter/cupertino.dart'; import 'package:login_app/components/custom_text_filed.dart'; import 'package:login_app/size.dart'; class CustomForm extends StatelessWidget { final _formKey = GlobalKey<FormState>(); @override Widget build(BuildContext context) { return Form( key: _formKey, child: Column( children: [ CustomTextFormField("Email"), SizedBox(height: medium_gap), CustomTextFormField("Password"), SizedBox(height: large_gap), ], ), ); } }
 

10. LoginPage.dart에 Form 추가하기

import 'package:flutter/material.dart'; import 'package:login_app/components/coustom_form.dart'; import 'package:login_app/components/logo.dart'; import 'package:login_app/size.dart'; class LoginPage extends StatelessWidget { const LoginPage({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: Padding( padding: const EdgeInsets.all(16.0), child: ListView( children: [ SizedBox(height: xlarge_gap), Logo("Login"), SizedBox(height: large_gap), CustomForm(), ], ), ), ); } }
notion image
 

11. main.dart에 버튼 만들기

import 'package:flutter/material.dart'; import 'package:login_app/pages/home_page.dart'; import 'package:login_app/pages/login_page.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( theme: ThemeData( textButtonTheme: TextButtonThemeData( style: TextButton.styleFrom( foregroundColor: Colors.white, backgroundColor: Colors.black, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(30), ), minimumSize: Size(400, 60), ), ), ), initialRoute: "/login", routes: { "/login": (context) => LoginPage(), "/home": (context) => HomePage(), }, ); } }
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:login_app/components/custom_text_filed.dart'; import 'package:login_app/size.dart'; class CustomForm extends StatelessWidget { final _formKey = GlobalKey<FormState>(); @override Widget build(BuildContext context) { return Form( key: _formKey, child: Column( children: [ CustomTextFormField("Email"), SizedBox(height: medium_gap), CustomTextFormField("Password"), SizedBox(height: large_gap), TextButton(onPressed: () {}, child: Text("Login")), ], ), ); } }
notion image
 

12. CustomForm에 login 버튼 누르면 home으로 이동하기

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:login_app/components/custom_text_filed.dart'; import 'package:login_app/size.dart'; class CustomForm extends StatelessWidget { final _formKey = GlobalKey<FormState>(); @override Widget build(BuildContext context) { return Form( key: _formKey, child: Column( children: [ CustomTextFormField("Email"), SizedBox(height: medium_gap), CustomTextFormField("Password"), SizedBox(height: large_gap), TextButton(onPressed: () { Navigator.pushNamed(context, "/home"); }, child: Text("Login")), ], ), ); } }
notion image
notion image
 

13. CustomForm에 유효성 검사 걸기

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:login_app/components/custom_text_filed.dart'; import 'package:login_app/size.dart'; class CustomForm extends StatelessWidget { final _formKey = GlobalKey<FormState>(); @override Widget build(BuildContext context) { return Form( key: _formKey, child: Column( children: [ CustomTextFormField("Email"), SizedBox(height: medium_gap), CustomTextFormField("Password"), SizedBox(height: large_gap), TextButton( onPressed: () { if (_formKey.currentState!.validate()) { Navigator.pushNamed(context, "/home"); } }, child: Text("Login"), ), ], ), ); } }
notion image
 

14. HomePage에 버튼 추가하기

import 'package:flutter/material.dart'; import 'package:login_app/components/logo.dart'; class HomePage extends StatelessWidget { const HomePage({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ SizedBox(height: 200), Logo("Care Soft"), SizedBox(height: 50), TextButton(onPressed: (){ Navigator.pop(context); }, child: Text("Get Started"), ), ], ), ), ); } }
notion image
Share article

vosw1