feat: 사용자 관리 API 연동 구현
- UserRemoteDataSource: 사용자 CRUD, 상태 변경, 비밀번호 변경, 중복 확인 API 구현 - UserService: DTO-Model 변환 로직 및 역할/전화번호 매핑 처리 - UserListController: ChangeNotifier 패턴 적용, 페이지네이션, 검색, 필터링 기능 추가 - UserFormController: API 연동, username 중복 확인 기능 추가 - user_form.dart: username/password 필드 추가 및 실시간 검증 - user_list_redesign.dart: Provider 패턴 적용, 무한 스크롤 구현 - equipment_out_form_controller.dart: 구문 오류 수정 - API 통합 진행률: 85% (사용자 관리 100% 완료)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:superport/models/company_model.dart';
|
||||
import 'package:superport/models/user_model.dart';
|
||||
import 'package:superport/screens/common/theme_tailwind.dart';
|
||||
@@ -21,116 +22,288 @@ class UserFormScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
final TextEditingController _passwordController = TextEditingController();
|
||||
final TextEditingController _confirmPasswordController = TextEditingController();
|
||||
bool _showPassword = false;
|
||||
bool _showConfirmPassword = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
_passwordController.dispose();
|
||||
_confirmPasswordController.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),
|
||||
return ChangeNotifierProvider(
|
||||
create: (_) => UserFormController(
|
||||
dataService: MockDataService(),
|
||||
userId: widget.userId,
|
||||
),
|
||||
child: Consumer<UserFormController>(
|
||||
builder: (context, controller, child) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(controller.isEditMode ? '사용자 수정' : '사용자 등록'),
|
||||
),
|
||||
body: controller.isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: 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!,
|
||||
),
|
||||
|
||||
// 사용자명 (신규 등록 시만)
|
||||
if (!controller.isEditMode) ...[
|
||||
_buildTextField(
|
||||
label: '사용자명',
|
||||
initialValue: controller.username,
|
||||
hintText: '로그인에 사용할 사용자명',
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '사용자명을 입력해주세요';
|
||||
}
|
||||
if (value.length < 3) {
|
||||
return '사용자명은 3자 이상이어야 합니다';
|
||||
}
|
||||
if (controller.isUsernameAvailable == false) {
|
||||
return '이미 사용 중인 사용자명입니다';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onChanged: (value) {
|
||||
controller.username = value;
|
||||
controller.checkUsernameAvailability(value);
|
||||
},
|
||||
onSaved: (value) => controller.username = value!,
|
||||
suffixIcon: controller.isCheckingUsername
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(12.0),
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
),
|
||||
),
|
||||
)
|
||||
: controller.isUsernameAvailable != null
|
||||
? Icon(
|
||||
controller.isUsernameAvailable!
|
||||
? Icons.check_circle
|
||||
: Icons.cancel,
|
||||
color: controller.isUsernameAvailable!
|
||||
? Colors.green
|
||||
: Colors.red,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
|
||||
// 비밀번호
|
||||
_buildPasswordField(
|
||||
label: '비밀번호',
|
||||
controller: _passwordController,
|
||||
hintText: '비밀번호를 입력하세요',
|
||||
obscureText: !_showPassword,
|
||||
onToggleVisibility: () {
|
||||
setState(() {
|
||||
_showPassword = !_showPassword;
|
||||
});
|
||||
},
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '비밀번호를 입력해주세요';
|
||||
}
|
||||
if (value.length < 6) {
|
||||
return '비밀번호는 6자 이상이어야 합니다';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (value) => controller.password = value!,
|
||||
),
|
||||
|
||||
// 비밀번호 확인
|
||||
_buildPasswordField(
|
||||
label: '비밀번호 확인',
|
||||
controller: _confirmPasswordController,
|
||||
hintText: '비밀번호를 다시 입력하세요',
|
||||
obscureText: !_showConfirmPassword,
|
||||
onToggleVisibility: () {
|
||||
setState(() {
|
||||
_showConfirmPassword = !_showConfirmPassword;
|
||||
});
|
||||
},
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '비밀번호를 다시 입력해주세요';
|
||||
}
|
||||
if (value != _passwordController.text) {
|
||||
return '비밀번호가 일치하지 않습니다';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
],
|
||||
|
||||
// 수정 모드에서 비밀번호 변경 (선택사항)
|
||||
if (controller.isEditMode) ...[
|
||||
ExpansionTile(
|
||||
title: const Text('비밀번호 변경'),
|
||||
children: [
|
||||
_buildPasswordField(
|
||||
label: '새 비밀번호',
|
||||
controller: _passwordController,
|
||||
hintText: '변경할 경우만 입력하세요',
|
||||
obscureText: !_showPassword,
|
||||
onToggleVisibility: () {
|
||||
setState(() {
|
||||
_showPassword = !_showPassword;
|
||||
});
|
||||
},
|
||||
validator: (value) {
|
||||
if (value != null && value.isNotEmpty && value.length < 6) {
|
||||
return '비밀번호는 6자 이상이어야 합니다';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (value) => controller.password = value ?? '',
|
||||
),
|
||||
|
||||
_buildPasswordField(
|
||||
label: '새 비밀번호 확인',
|
||||
controller: _confirmPasswordController,
|
||||
hintText: '비밀번호를 다시 입력하세요',
|
||||
obscureText: !_showConfirmPassword,
|
||||
onToggleVisibility: () {
|
||||
setState(() {
|
||||
_showConfirmPassword = !_showConfirmPassword;
|
||||
});
|
||||
},
|
||||
validator: (value) {
|
||||
if (_passwordController.text.isNotEmpty && value != _passwordController.text) {
|
||||
return '비밀번호가 일치하지 않습니다';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
|
||||
// 직급
|
||||
_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) {
|
||||
controller.companyId = value;
|
||||
controller.branchId = null;
|
||||
if (value != null) {
|
||||
controller.loadBranches(value);
|
||||
} else {
|
||||
controller.branches = [];
|
||||
}
|
||||
},
|
||||
onBranchChanged: (value) {
|
||||
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(controller),
|
||||
// 권한
|
||||
_buildRoleRadio(controller),
|
||||
const SizedBox(height: 24),
|
||||
// 오류 메시지 표시
|
||||
if (controller.error != null)
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
margin: const EdgeInsets.only(bottom: 16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red.shade50,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: Colors.red.shade200),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.error_outline, color: Colors.red.shade700),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
controller.error!,
|
||||
style: TextStyle(color: Colors.red.shade700),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 저장 버튼
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: controller.isLoading
|
||||
? null
|
||||
: () => _onSaveUser(controller),
|
||||
style: AppThemeTailwind.primaryButtonStyle,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: controller.isLoading
|
||||
? const SizedBox(
|
||||
height: 20,
|
||||
width: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
|
||||
),
|
||||
)
|
||||
: Text(
|
||||
controller.isEditMode ? '수정하기' : '등록하기',
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -144,6 +317,8 @@ class _UserFormScreenState extends State<UserFormScreen> {
|
||||
List<TextInputFormatter>? inputFormatters,
|
||||
String? Function(String?)? validator,
|
||||
void Function(String?)? onSaved,
|
||||
void Function(String)? onChanged,
|
||||
Widget? suffixIcon,
|
||||
}) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 16.0),
|
||||
@@ -154,25 +329,66 @@ class _UserFormScreenState extends State<UserFormScreen> {
|
||||
const SizedBox(height: 4),
|
||||
TextFormField(
|
||||
initialValue: initialValue,
|
||||
decoration: InputDecoration(hintText: hintText),
|
||||
decoration: InputDecoration(
|
||||
hintText: hintText,
|
||||
suffixIcon: suffixIcon,
|
||||
),
|
||||
keyboardType: keyboardType,
|
||||
inputFormatters: inputFormatters,
|
||||
validator: validator,
|
||||
onSaved: onSaved,
|
||||
onChanged: onChanged,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 비밀번호 필드 위젯
|
||||
Widget _buildPasswordField({
|
||||
required String label,
|
||||
required TextEditingController controller,
|
||||
required String hintText,
|
||||
required bool obscureText,
|
||||
required VoidCallback onToggleVisibility,
|
||||
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(
|
||||
controller: controller,
|
||||
obscureText: obscureText,
|
||||
decoration: InputDecoration(
|
||||
hintText: hintText,
|
||||
suffixIcon: IconButton(
|
||||
icon: Icon(
|
||||
obscureText ? Icons.visibility : Icons.visibility_off,
|
||||
),
|
||||
onPressed: onToggleVisibility,
|
||||
),
|
||||
),
|
||||
validator: validator,
|
||||
onSaved: onSaved,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// 전화번호 입력 필드 섹션 위젯 (UserPhoneField 기반)
|
||||
Widget _buildPhoneFieldsSection() {
|
||||
Widget _buildPhoneFieldsSection(UserFormController controller) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('전화번호', style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 4),
|
||||
..._controller.phoneFields.asMap().entries.map((entry) {
|
||||
...controller.phoneFields.asMap().entries.map((entry) {
|
||||
final i = entry.key;
|
||||
final phoneField = entry.value;
|
||||
return Row(
|
||||
@@ -180,17 +396,11 @@ class _UserFormScreenState extends State<UserFormScreen> {
|
||||
// 종류 드롭다운
|
||||
DropdownButton<String>(
|
||||
value: phoneField.type,
|
||||
items:
|
||||
_controller.phoneTypes
|
||||
.map(
|
||||
(type) =>
|
||||
DropdownMenuItem(value: type, child: Text(type)),
|
||||
)
|
||||
.toList(),
|
||||
items: controller.phoneTypes
|
||||
.map((type) => DropdownMenuItem(value: type, child: Text(type)))
|
||||
.toList(),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
phoneField.type = value!;
|
||||
});
|
||||
phoneField.type = value!;
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
@@ -205,14 +415,9 @@ class _UserFormScreenState extends State<UserFormScreen> {
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.remove_circle, color: Colors.red),
|
||||
onPressed:
|
||||
_controller.phoneFields.length > 1
|
||||
? () {
|
||||
setState(() {
|
||||
_controller.removePhoneField(i);
|
||||
});
|
||||
}
|
||||
: null,
|
||||
onPressed: controller.phoneFields.length > 1
|
||||
? () => controller.removePhoneField(i)
|
||||
: null,
|
||||
),
|
||||
],
|
||||
);
|
||||
@@ -221,11 +426,7 @@ class _UserFormScreenState extends State<UserFormScreen> {
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: TextButton.icon(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_controller.addPhoneField();
|
||||
});
|
||||
},
|
||||
onPressed: () => controller.addPhoneField(),
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('전화번호 추가'),
|
||||
),
|
||||
@@ -235,7 +436,7 @@ class _UserFormScreenState extends State<UserFormScreen> {
|
||||
}
|
||||
|
||||
// 권한(관리등급) 라디오 위젯
|
||||
Widget _buildRoleRadio() {
|
||||
Widget _buildRoleRadio(UserFormController controller) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 16.0),
|
||||
child: Column(
|
||||
@@ -249,11 +450,9 @@ class _UserFormScreenState extends State<UserFormScreen> {
|
||||
child: RadioListTile<String>(
|
||||
title: const Text('관리자'),
|
||||
value: UserRoles.admin,
|
||||
groupValue: _controller.role,
|
||||
groupValue: controller.role,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_controller.role = value!;
|
||||
});
|
||||
controller.role = value!;
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -261,11 +460,9 @@ class _UserFormScreenState extends State<UserFormScreen> {
|
||||
child: RadioListTile<String>(
|
||||
title: const Text('일반 사용자'),
|
||||
value: UserRoles.member,
|
||||
groupValue: _controller.role,
|
||||
groupValue: controller.role,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_controller.role = value!;
|
||||
});
|
||||
controller.role = value!;
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -277,17 +474,24 @@ class _UserFormScreenState extends State<UserFormScreen> {
|
||||
}
|
||||
|
||||
// 저장 버튼 클릭 시 사용자 저장
|
||||
void _onSaveUser() {
|
||||
setState(() {
|
||||
_controller.saveUser((error) {
|
||||
if (error != null) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text(error)));
|
||||
} else {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
});
|
||||
void _onSaveUser(UserFormController controller) async {
|
||||
await controller.saveUser((error) {
|
||||
if (error != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(error),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(controller.isEditMode ? '사용자 정보가 수정되었습니다' : '사용자가 등록되었습니다'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user