MIF_E31231313/Android/flutter_app/lib/screens/edit_profile_screen.dart

280 lines
10 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:lucide_icons/lucide_icons.dart';
import '../app_design.dart';
import '../app_state.dart';
import '../services/api_service.dart';
class EditProfileScreen extends StatefulWidget {
const EditProfileScreen({
super.key,
required this.initialName,
required this.initialPhone,
this.initialEmail = '',
});
final String initialName;
final String initialPhone;
final String initialEmail;
@override
State<EditProfileScreen> createState() => _EditProfileScreenState();
}
class _EditProfileScreenState extends State<EditProfileScreen> {
final _api = ApiService();
late final TextEditingController _nameController;
late final TextEditingController _phoneController;
late final TextEditingController _emailController;
final _formKey = GlobalKey<FormState>();
bool _saving = false;
@override
void initState() {
super.initState();
_nameController = TextEditingController(text: widget.initialName);
_phoneController = TextEditingController(text: widget.initialPhone);
_emailController = TextEditingController(text: widget.initialEmail);
}
@override
void dispose() {
_nameController.dispose();
_phoneController.dispose();
_emailController.dispose();
super.dispose();
}
Future<void> _save() async {
if (!_formKey.currentState!.validate()) return;
setState(() => _saving = true);
Map<String, dynamic> updatedProfile;
try {
updatedProfile = await _api.updateProfile(
name: _nameController.text.trim(),
phone: _phoneController.text.trim(),
email: _emailController.text.trim(),
);
} catch (e) {
if (!mounted) return;
setState(() => _saving = false);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Gagal menyimpan: $e')),
);
return;
}
if (!mounted) return;
setState(() => _saving = false);
SgiziAppState.instance.setProfileData(updatedProfile);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Perubahan profil berhasil disimpan.')),
);
Navigator.of(context).pop(updatedProfile);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF5F7F6),
appBar: AppBar(
title: const Text('Edit Profile'),
backgroundColor: const Color(0xFFF5F7F6),
),
body: SafeArea(
child: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
padding: const EdgeInsets.fromLTRB(20, 14, 20, 24),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
HealthCard(
child: Center(
child: Column(
children: [
Stack(
clipBehavior: Clip.none,
children: [
Container(
width: 98,
height: 98,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: const Color(0xFF0B7A86), width: 2),
),
child: Padding(
padding: const EdgeInsets.all(10),
child: ClipOval(
child: Image.asset(
'assets/image/onboarding_consultation.png',
fit: BoxFit.cover,
),
),
),
),
Positioned(
right: -2,
bottom: -2,
child: InkWell(
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Upload foto akan tersedia di versi berikutnya.')),
);
},
child: Container(
width: 34,
height: 34,
decoration: BoxDecoration(
color: const Color(0xFF0B7A86),
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.12),
blurRadius: 12,
offset: const Offset(0, 6),
),
],
),
child: const Icon(LucideIcons.camera, color: Colors.white, size: 16),
),
),
),
],
).animate(onPlay: (c) => c.repeat(reverse: true)).scale(
begin: const Offset(0.99, 0.99),
end: const Offset(1.02, 1.02),
duration: 1700.ms,
),
const SizedBox(height: 10),
Text(
'Ubah Foto Profile',
style: AppTypography.caption.copyWith(
color: const Color(0xFF0B7A86),
fontWeight: FontWeight.w700,
),
),
],
),
),
),
const SizedBox(height: 14),
_InputField(
controller: _nameController,
label: 'Nama Lengkap',
icon: LucideIcons.user,
validator: (v) => (v == null || v.trim().isEmpty) ? 'Nama wajib diisi.' : null,
),
const SizedBox(height: 12),
_InputField(
controller: _phoneController,
label: 'Nomor Telepon',
icon: LucideIcons.messageCircle,
validator: (v) => (v == null || v.trim().length < 8) ? 'Nomor telepon tidak valid.' : null,
),
const SizedBox(height: 12),
_InputField(
controller: _emailController,
label: 'Email',
icon: LucideIcons.info,
validator: (v) {
final value = (v ?? '').trim();
if (value.isEmpty) return null;
return value.contains('@') ? null : 'Format email tidak valid.';
},
),
const SizedBox(height: 18),
Container(
width: double.infinity,
height: 56,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Color(0xFF0B7A86), Color(0xFF1597A4)],
),
borderRadius: BorderRadius.circular(18),
boxShadow: [
BoxShadow(
color: const Color(0xFF0B7A86).withValues(alpha: 0.30),
blurRadius: 18,
offset: const Offset(0, 10),
),
],
),
child: FilledButton(
onPressed: _saving ? null : _save,
style: FilledButton.styleFrom(
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)),
),
child: _saving
? const SizedBox(
width: 22,
height: 22,
child: CircularProgressIndicator(strokeWidth: 2.2, color: Colors.white),
)
: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Simpan Perubahan',
style: AppTypography.h3.copyWith(color: Colors.white),
),
const SizedBox(width: 8),
const Icon(LucideIcons.arrowRight, color: Colors.white, size: 18),
],
),
),
),
],
),
),
),
),
);
}
}
class _InputField extends StatelessWidget {
const _InputField({
required this.controller,
required this.label,
required this.icon,
this.validator,
});
final TextEditingController controller;
final String label;
final IconData icon;
final String? Function(String?)? validator;
@override
Widget build(BuildContext context) {
return TextFormField(
controller: controller,
validator: validator,
decoration: InputDecoration(
labelText: label,
prefixIcon: Icon(icon, color: const Color(0xFF0B7A86)),
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: const BorderSide(color: Color(0xFFE3EAE8)),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: const BorderSide(color: Color(0xFFE3EAE8)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: const BorderSide(color: Color(0xFF0B7A86), width: 1.6),
),
),
);
}
}