- 화면마다 위젯/스타일만 담당하는 UI 파일과, 서버통신/상태/파생로직만 담당하는 ChangeNotifier 컨트롤러 파일로 1:1 분리 (lib/screens/ -> lib/ui/ + lib/function/) - UI는 ListenableBuilder로 컨트롤러를 구독해서 재렌더링, 버튼은 컨트롤러 메서드만 호출 - 다이얼로그/스낵바 등 위젯 코드는 전부 UI 파일에 남기고, 컨트롤러는 결과값(성공여부+메시지) 또는 콜백으로만 UI와 통신 (BuildContext/Widget 의존성 없음) - 디자인/레이아웃은 기존과 완전히 동일하게 유지 (순수 코드 재배치) - change_password_screen.dart, debug_pocket_main.dart(미사용 파일)는 깨지지 않게 import 경로만 갱신하고 리팩터링은 보류
308 lines
11 KiB
Dart
308 lines
11 KiB
Dart
// 🔐 (마스터 계정용) 학생 계정 및 기기 관리 화면 (UI 전용). 기기 UUID 초기화/계정 삭제/생성 다이얼로그를 그린다.
|
|
// 서버 통신/상태는 lib/function/student_management_controller.dart가 담당한다.
|
|
import 'package:flutter/material.dart';
|
|
import '../function/student_management_controller.dart';
|
|
|
|
// ==========================================
|
|
// 🔐 6. 학생 계정 및 기기 관리 화면 (기기 리셋 + 계정 삭제 완본)
|
|
// ==========================================
|
|
class StudentManagementScreen extends StatefulWidget {
|
|
const StudentManagementScreen({super.key});
|
|
|
|
@override
|
|
State<StudentManagementScreen> createState() =>
|
|
_StudentManagementScreenState();
|
|
}
|
|
|
|
class _StudentManagementScreenState extends State<StudentManagementScreen> {
|
|
final StudentManagementController _controller =
|
|
StudentManagementController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_controller.init();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _fetchStudents() async {
|
|
final error = await _controller.fetchStudents();
|
|
if (error != null && mounted) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(error)));
|
|
}
|
|
}
|
|
|
|
// 🌐 서버로 기기 초기화(리셋) 명령 보내기
|
|
void _resetDevice(String studentId, String studentName) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text(
|
|
'⚠️ 기기 잠금 해제',
|
|
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.purple),
|
|
),
|
|
content: Text('$studentName 학생의 스마트폰 기기 등록을 초기화하시겠습니까?'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text('취소', style: TextStyle(color: Colors.grey)),
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.purple,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
onPressed: () async {
|
|
Navigator.pop(ctx);
|
|
final (_, message) = await _controller.resetDevice(
|
|
studentId,
|
|
studentName,
|
|
);
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(message)));
|
|
},
|
|
child: const Text('초기화 승인'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// 🌐 [새 기능] 서버로 계정 완전 삭제 명령 보내기
|
|
void _deleteUser(String studentId, String studentName) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text(
|
|
'🚨 계정 완전 삭제 경고',
|
|
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.red),
|
|
),
|
|
content: Text(
|
|
'정말로 $studentName ($studentId) 학생의 계정을 시스템에서 탈퇴(삭제)시키겠습니까?\n\n이 작업은 되돌릴 수 없으며, 해당 학생은 다시 회원가입을 진행해야 합니다.',
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text('취소', style: TextStyle(color: Colors.grey)),
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.red,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
onPressed: () async {
|
|
Navigator.pop(ctx);
|
|
final (_, message) = await _controller.deleteUser(
|
|
studentId,
|
|
studentName,
|
|
);
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(message)));
|
|
},
|
|
child: const Text('영구 삭제'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showCreateUserDialog() {
|
|
final idController = TextEditingController();
|
|
final nameController = TextEditingController();
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('👤 신규 학생 계정 추가'),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextField(
|
|
controller: idController,
|
|
decoration: const InputDecoration(labelText: '학번 입력 (예: 20101)'),
|
|
keyboardType: TextInputType.number,
|
|
),
|
|
const SizedBox(height: 8),
|
|
TextField(
|
|
controller: nameController,
|
|
decoration: const InputDecoration(labelText: '학생 이름 입력'),
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('취소'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () async {
|
|
String studentId = idController.text.trim();
|
|
String name = nameController.text.trim();
|
|
|
|
if (studentId.isEmpty || name.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('⚠️ 학번과 이름을 모두 입력하세요.')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
final (success, message) = await _controller.createUser(
|
|
studentId,
|
|
name,
|
|
);
|
|
if (!mounted) return;
|
|
if (success) {
|
|
Navigator.pop(context); // 팝업 닫기
|
|
}
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(message)));
|
|
},
|
|
child: const Text('생성'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListenableBuilder(
|
|
listenable: _controller,
|
|
builder: (context, _) {
|
|
final realStudents = _controller.students;
|
|
return Scaffold(
|
|
backgroundColor: Colors.grey[50],
|
|
appBar: AppBar(
|
|
title: const Text(
|
|
'학생 계정 및 기기 관리',
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
backgroundColor: Colors.purple,
|
|
foregroundColor: Colors.white,
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.person_add_alt_1_rounded),
|
|
onPressed: () => _showCreateUserDialog(),
|
|
),
|
|
IconButton(icon: const Icon(Icons.refresh), onPressed: _fetchStudents),
|
|
],
|
|
),
|
|
body: _controller.isLoading
|
|
? const Center(
|
|
child: CircularProgressIndicator(color: Colors.purple),
|
|
)
|
|
: realStudents.isEmpty
|
|
? const Center(
|
|
child: Text(
|
|
'가입된 학생 계정이 없습니다.',
|
|
style: TextStyle(color: Colors.grey, fontSize: 16),
|
|
),
|
|
)
|
|
: ListView.builder(
|
|
padding: const EdgeInsets.all(16),
|
|
itemCount: realStudents.length,
|
|
itemBuilder: (context, index) {
|
|
final student = realStudents[index];
|
|
final bool needsReset = student['device'] == "초기화 필요";
|
|
|
|
return Card(
|
|
elevation: 2,
|
|
margin: const EdgeInsets.only(bottom: 12),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: ListTile(
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
horizontal: 16,
|
|
vertical: 12,
|
|
),
|
|
leading: CircleAvatar(
|
|
backgroundColor: needsReset
|
|
? Colors.red[50]
|
|
: Colors.purple[50],
|
|
child: Icon(
|
|
needsReset ? Icons.lock_reset : Icons.person,
|
|
color: needsReset ? Colors.red : Colors.purple,
|
|
),
|
|
),
|
|
title: Text(
|
|
'${student['name']} (${student['id']})',
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
subtitle: Text(
|
|
needsReset ? '기기 초기화 승인 대기중' : '정상 등록 상태',
|
|
style: TextStyle(
|
|
color: needsReset ? Colors.red : Colors.grey,
|
|
fontWeight: needsReset
|
|
? FontWeight.bold
|
|
: FontWeight.normal,
|
|
),
|
|
),
|
|
// 🕹️ 오른쪽 끝에 [기기 리셋] 버튼과 [쓰레기통(삭제)] 버튼을 나란히 배치하는 Row 레이아웃 기획
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
if (!needsReset)
|
|
OutlinedButton(
|
|
style: OutlinedButton.styleFrom(
|
|
foregroundColor: Colors.purple,
|
|
side: BorderSide(
|
|
color: Colors.purple.withValues(
|
|
alpha: 0.5,
|
|
),
|
|
),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
),
|
|
onPressed: () => _resetDevice(
|
|
student['id'],
|
|
student['name'],
|
|
),
|
|
child: const Text('기기 리셋'),
|
|
)
|
|
else
|
|
const Icon(
|
|
Icons.check_circle,
|
|
color: Colors.green,
|
|
),
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
// 🔴 [새 버튼] 최고권한 마스터 전용 계정 영구 삭제 쓰레기통 버튼!
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.delete_forever_rounded,
|
|
color: Colors.redAccent,
|
|
),
|
|
tooltip: '계정 영구 삭제',
|
|
onPressed: () =>
|
|
_deleteUser(student['id'], student['name']),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|