Files
school-attendance/lib/screens/change_password_screen.dart
T
sihoo 29e319110c lib/ 코드를 UI(lib/ui/)와 기능(lib/function/)으로 분리
- 화면마다 위젯/스타일만 담당하는 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
  경로만 갱신하고 리팩터링은 보류
2026-08-05 15:04:24 +09:00

110 lines
3.8 KiB
Dart

// 🔑 비밀번호 변경 화면. (참고: 현재 앱 어디서도 이 화면으로 이동하는 곳이 없는 미사용 화면)
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../config.dart';
import '../ui/login_screen.dart';
// 💡 main.dart 파일의 최하단(다른 클래스 중괄호 밖)에 붙여넣으세요.
class ChangePasswordScreen extends StatefulWidget {
final String studentId;
const ChangePasswordScreen({super.key, required this.studentId});
@override
State<ChangePasswordScreen> createState() => _ChangePasswordScreenState();
}
class _ChangePasswordScreenState extends State<ChangePasswordScreen> {
final _pwController = TextEditingController();
bool _isLoading = false;
Future<void> _updatePassword() async {
String newPw = _pwController.text.trim();
if (newPw.isEmpty || newPw == "1234") {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('⚠️ 초기 비밀번호와 다른 안전한 비밀번호를 입력하세요.')),
);
return;
}
setState(() => _isLoading = true);
try {
final response = await http.post(
Uri.parse('$baseUrl/api/users/change-password'),
headers: {"Content-Type": "application/json"},
body: jsonEncode({"studentId": widget.studentId, "newPassword": newPw}),
);
final res = jsonDecode(response.body);
if (response.statusCode == 200 && res['status'] == 'success') {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('🔒 비밀번호 변경 완료! 다시 로그인해 주세요.')),
);
// 비밀번호를 바꿨으니 다시 로그인 화면으로 강제 이동
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const LoginScreen()),
);
}
} catch (e) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('❌ 통신 실패')));
} finally {
setState(() => _isLoading = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(32.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'🔒 보안을 위해\n비밀번호를 변경해 주세요',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
height: 1.4,
),
),
const SizedBox(height: 8),
const Text(
'처음 로그인 시 초기 비밀번호(1234)를 반드시 변경해야 이용이 가능합니다.',
style: TextStyle(color: Colors.grey),
),
const SizedBox(height: 32),
TextField(
controller: _pwController,
obscureText: true,
decoration: const InputDecoration(
labelText: '새로운 비밀번호 입력',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
height: 50,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.indigo,
foregroundColor: Colors.white,
),
onPressed: _isLoading ? null : _updatePassword,
child: _isLoading
? const CircularProgressIndicator(color: Colors.white)
: const Text('변경 및 적용하기'),
),
),
],
),
),
);
}
}