Created
October 23, 2024 02:59
-
-
Save hongsw/c490a28c146798e03bb57368dec6a64f to your computer and use it in GitHub Desktop.
w8-페이지 이동 with gpt https://chatgpt.com/c/67186524-23bc-8013-a178-c7ce9fece1e8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 필수 Material Design 위젯을 포함하는 패키지 임포트 | |
import 'package:flutter/material.dart'; | |
// 앱의 시작점 | |
void main() { | |
runApp(const MyApp()); | |
} | |
/// 앱의 루트 위젯 | |
/// MaterialApp을 반환하여 머티리얼 디자인 테마 적용 | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Form Demo', // 앱의 제목 | |
theme: ThemeData( | |
primarySwatch: Colors.blue, // 앱의 주 색상 테마 | |
), | |
home: const HomePage(), // 앱의 시작 페이지 | |
); | |
} | |
} | |
/// 홈 페이지 위젯 | |
/// StatelessWidget을 사용하여 정적 UI 구현 | |
class HomePage extends StatelessWidget { | |
const HomePage({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('홈')), | |
body: Center( | |
child: ElevatedButton( | |
child: const Text('검색 폼으로'), | |
onPressed: () { | |
// MaterialPageRoute를 사용하여 새 페이지로 이동 | |
Navigator.push( | |
context, | |
MaterialPageRoute(builder: (context) => SearchScreen()), | |
); | |
}, | |
), | |
), | |
); | |
} | |
} | |
// https://chatgpt.com/c/67186524-23bc-8013-a178-c7ce9fece1e8 | |
class SearchScreen extends StatefulWidget { | |
@override | |
_SearchScreenState createState() => _SearchScreenState(); | |
} | |
class _SearchScreenState extends State<SearchScreen> { | |
TextEditingController _searchController = TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Search UI'), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Column( | |
children: [ | |
TextField( | |
controller: _searchController, | |
decoration: InputDecoration( | |
labelText: 'Search', | |
hintText: 'Enter search term', | |
prefixIcon: Icon(Icons.search), | |
border: OutlineInputBorder( | |
borderRadius: BorderRadius.all(Radius.circular(8.0)), | |
), | |
), | |
onChanged: (value) { | |
print("Search input: $value"); // 검색 입력 값을 처리할 수 있는 부분 | |
}, | |
), | |
SizedBox(height: 20), | |
// 검색 결과를 보여주거나 다른 위젯을 추가할 수 있는 부분 | |
Expanded( | |
child: Center( | |
child: ElevatedButton( | |
child: const Text('입력 폼으로'), | |
onPressed: () { | |
// MaterialPageRoute를 사용하여 새 페이지로 이동 | |
Navigator.push( | |
context, | |
MaterialPageRoute(builder: (context) => InputFormPage()), | |
); | |
}, | |
), | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
@override | |
void dispose() { | |
_searchController.dispose(); | |
super.dispose(); | |
} | |
} | |
/// 입력 폼 페이지 | |
/// StatefulWidget을 사용하여 동적 상태 관리 | |
class InputFormPage extends StatefulWidget { | |
const InputFormPage({Key? key}) : super(key: key); | |
@override | |
InputFormPageState createState() => InputFormPageState(); | |
} | |
/// 입력 폼 페이지의 상태 관리 클래스 | |
class InputFormPageState extends State<InputFormPage> { | |
// 폼의 상태를 관리하기 위한 글로벌 키 | |
final _formKey = GlobalKey<FormState>(); | |
// 폼 입력 데이터를 저장할 변수 | |
String _name = ''; | |
String _email = ''; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('입력 폼')), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), // 전체 여백 설정 | |
child: Form( | |
key: _formKey, // 폼 검증을 위한 키 설정 | |
child: Column( | |
children: [ | |
// 이름 입력 필드 | |
TextFormField( | |
decoration: const InputDecoration( | |
labelText: '이름', // 라벨 텍스트 | |
border: OutlineInputBorder(), // 테두리 스타일 | |
), | |
// 입력값 검증 | |
validator: (value) { | |
if (value == null || value.isEmpty) { | |
return '이름을 입력하세요'; | |
} | |
return null; | |
}, | |
// 검증 통과 시 값 저장 | |
onSaved: (value) { | |
_name = value ?? ''; // null 체크 후 값 저장 | |
}, | |
), | |
const SizedBox(height: 16), // 수직 간격 추가 | |
// 이메일 입력 필드 | |
TextFormField( | |
decoration: const InputDecoration( | |
labelText: '이메일', | |
border: OutlineInputBorder(), | |
), | |
// 이메일 형식 검증 | |
validator: (value) { | |
if (value == null || value.isEmpty) { | |
return '이메일을 입력하세요'; | |
} | |
if (!value.contains('@')) { | |
return '올바른 이메일 형식이 아닙니다'; | |
} | |
return null; | |
}, | |
onSaved: (value) { | |
_email = value ?? ''; | |
}, | |
), | |
const SizedBox(height: 16), | |
// 저장 버튼 | |
ElevatedButton( | |
onPressed: () { | |
// 폼 검증 실행 | |
if (_formKey.currentState!.validate()) { | |
_formKey.currentState!.save(); // 검증 통과 시 데이터 저장 | |
// 이전 페이지로 데이터와 함께 이동 | |
Navigator.pop(context, { | |
'name': _name, | |
'email': _email, | |
}); | |
} | |
}, | |
child: const Text('저장'), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment