프로젝트 최초 커밋
This commit is contained in:
293
lib/screens/user/user_form.dart
Normal file
293
lib/screens/user/user_form.dart
Normal file
@@ -0,0 +1,293 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:superport/models/company_model.dart';
|
||||
import 'package:superport/models/user_model.dart';
|
||||
import 'package:superport/screens/common/theme_tailwind.dart';
|
||||
import 'package:superport/screens/common/custom_widgets.dart';
|
||||
import 'package:superport/services/mock_data_service.dart';
|
||||
import 'package:superport/utils/constants.dart';
|
||||
import 'package:superport/utils/validators.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:superport/screens/user/controllers/user_form_controller.dart';
|
||||
import 'package:superport/models/user_phone_field.dart';
|
||||
import 'package:superport/screens/common/widgets/company_branch_dropdown.dart';
|
||||
|
||||
// 사용자 등록/수정 화면 (UI만 담당, 상태/로직 분리)
|
||||
class UserFormScreen extends StatefulWidget {
|
||||
final int? userId;
|
||||
const UserFormScreen({super.key, this.userId});
|
||||
|
||||
@override
|
||||
State<UserFormScreen> createState() => _UserFormScreenState();
|
||||
}
|
||||
|
||||
class _UserFormScreenState extends State<UserFormScreen> {
|
||||
late final UserFormController _controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = UserFormController(
|
||||
dataService: MockDataService(),
|
||||
userId: widget.userId,
|
||||
);
|
||||
_controller.isEditMode = widget.userId != null;
|
||||
_controller.loadCompanies();
|
||||
if (_controller.isEditMode) {
|
||||
_controller.loadUser();
|
||||
} else if (_controller.phoneFields.isEmpty) {
|
||||
_controller.addPhoneField();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(_controller.isEditMode ? '사용자 수정' : '사용자 등록')),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _controller.formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 이름
|
||||
_buildTextField(
|
||||
label: '이름',
|
||||
initialValue: _controller.name,
|
||||
hintText: '사용자 이름을 입력하세요',
|
||||
validator: (value) => validateRequired(value, '이름'),
|
||||
onSaved: (value) => _controller.name = value!,
|
||||
),
|
||||
// 직급
|
||||
_buildTextField(
|
||||
label: '직급',
|
||||
initialValue: _controller.position,
|
||||
hintText: '직급을 입력하세요',
|
||||
onSaved: (value) => _controller.position = value ?? '',
|
||||
),
|
||||
// 소속 회사/지점
|
||||
CompanyBranchDropdown(
|
||||
companies: _controller.companies,
|
||||
selectedCompanyId: _controller.companyId,
|
||||
selectedBranchId: _controller.branchId,
|
||||
branches: _controller.branches,
|
||||
onCompanyChanged: (value) {
|
||||
setState(() {
|
||||
_controller.companyId = value;
|
||||
_controller.branchId = null;
|
||||
if (value != null) {
|
||||
_controller.loadBranches(value);
|
||||
} else {
|
||||
_controller.branches = [];
|
||||
}
|
||||
});
|
||||
},
|
||||
onBranchChanged: (value) {
|
||||
setState(() {
|
||||
_controller.branchId = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
// 이메일
|
||||
_buildTextField(
|
||||
label: '이메일',
|
||||
initialValue: _controller.email,
|
||||
hintText: '이메일을 입력하세요',
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) return null;
|
||||
return validateEmail(value);
|
||||
},
|
||||
onSaved: (value) => _controller.email = value ?? '',
|
||||
),
|
||||
// 전화번호
|
||||
_buildPhoneFieldsSection(),
|
||||
// 권한
|
||||
_buildRoleRadio(),
|
||||
const SizedBox(height: 24),
|
||||
// 저장 버튼
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: _onSaveUser,
|
||||
style: AppThemeTailwind.primaryButtonStyle,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Text(
|
||||
_controller.isEditMode ? '수정하기' : '등록하기',
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 이름/직급/이메일 등 공통 텍스트 필드 위젯
|
||||
Widget _buildTextField({
|
||||
required String label,
|
||||
required String initialValue,
|
||||
required String hintText,
|
||||
TextInputType? keyboardType,
|
||||
List<TextInputFormatter>? inputFormatters,
|
||||
String? Function(String?)? validator,
|
||||
void Function(String?)? onSaved,
|
||||
}) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(label, style: const TextStyle(fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 4),
|
||||
TextFormField(
|
||||
initialValue: initialValue,
|
||||
decoration: InputDecoration(hintText: hintText),
|
||||
keyboardType: keyboardType,
|
||||
inputFormatters: inputFormatters,
|
||||
validator: validator,
|
||||
onSaved: onSaved,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 전화번호 입력 필드 섹션 위젯 (UserPhoneField 기반)
|
||||
Widget _buildPhoneFieldsSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('전화번호', style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 4),
|
||||
..._controller.phoneFields.asMap().entries.map((entry) {
|
||||
final i = entry.key;
|
||||
final phoneField = entry.value;
|
||||
return Row(
|
||||
children: [
|
||||
// 종류 드롭다운
|
||||
DropdownButton<String>(
|
||||
value: phoneField.type,
|
||||
items:
|
||||
_controller.phoneTypes
|
||||
.map(
|
||||
(type) =>
|
||||
DropdownMenuItem(value: type, child: Text(type)),
|
||||
)
|
||||
.toList(),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
phoneField.type = value!;
|
||||
});
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
// 번호 입력
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: phoneField.controller,
|
||||
keyboardType: TextInputType.phone,
|
||||
decoration: const InputDecoration(hintText: '전화번호'),
|
||||
onSaved: (value) {}, // 값은 controller에서 직접 추출
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.remove_circle, color: Colors.red),
|
||||
onPressed:
|
||||
_controller.phoneFields.length > 1
|
||||
? () {
|
||||
setState(() {
|
||||
_controller.removePhoneField(i);
|
||||
});
|
||||
}
|
||||
: null,
|
||||
),
|
||||
],
|
||||
);
|
||||
}),
|
||||
// 추가 버튼
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: TextButton.icon(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_controller.addPhoneField();
|
||||
});
|
||||
},
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('전화번호 추가'),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// 권한(관리등급) 라디오 위젯
|
||||
Widget _buildRoleRadio() {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('권한', style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 4),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: RadioListTile<String>(
|
||||
title: const Text('관리자'),
|
||||
value: UserRoles.admin,
|
||||
groupValue: _controller.role,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_controller.role = value!;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: RadioListTile<String>(
|
||||
title: const Text('일반 사용자'),
|
||||
value: UserRoles.member,
|
||||
groupValue: _controller.role,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_controller.role = value!;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 저장 버튼 클릭 시 사용자 저장
|
||||
void _onSaveUser() {
|
||||
setState(() {
|
||||
_controller.saveUser((error) {
|
||||
if (error != null) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text(error)));
|
||||
} else {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user