Files
rog_app/lib/pages/register/user_detail_page.dart
2024-08-01 20:08:12 +09:00

183 lines
6.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:rogapp/model/user.dart';
import 'package:rogapp/routes/app_pages.dart';
import 'package:rogapp/services/api_service.dart';
import 'package:rogapp/pages/index/index_controller.dart';
class UserDetailsEditPage extends StatefulWidget {
@override
_UserDetailsEditPageState createState() => _UserDetailsEditPageState();
}
class _UserDetailsEditPageState extends State<UserDetailsEditPage> {
final _formKey = GlobalKey<FormState>();
final IndexController indexController = Get.find<IndexController>();
late User _user;
final TextEditingController _firstnameController = TextEditingController();
final TextEditingController _lastnameController = TextEditingController();
final TextEditingController _dateOfBirthController = TextEditingController();
late bool _female;
@override
void initState() {
super.initState();
_user = User.fromJson(indexController.currentUser[0]['user']);
_firstnameController.text = _user.firstname;
_lastnameController.text = _user.lastname;
_dateOfBirthController.text = _user.dateOfBirth != null
? '${_user.dateOfBirth!.year}/${_user.dateOfBirth!.month.toString().padLeft(2, '0')}/${_user.dateOfBirth!.day.toString().padLeft(2, '0')}'
: '';
_female = _user.female;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('個人情報の修正'),
automaticallyImplyLeading: false,
),
body: Form(
key: _formKey,
child: ListView(
padding: EdgeInsets.all(16.0),
children: [
TextFormField(
controller: _lastnameController,
decoration: InputDecoration(
labelText: '',
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return '姓を入力してください';
}
return null;
},
),
SizedBox(height: 16),
TextFormField(
controller: _firstnameController,
decoration: InputDecoration(
labelText: '',
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return '名を入力してください';
}
return null;
},
),
SizedBox(height: 16),
TextFormField(
controller: _dateOfBirthController,
decoration: InputDecoration(
labelText: '生年月日 (YYYY/MM/DD)',
border: OutlineInputBorder(),
hintText: 'YYYY/MM/DD',
),
keyboardType: TextInputType.datetime,
validator: (value) {
if (value == null || value.isEmpty) {
return '生年月日を入力してください';
}
if (!RegExp(r'^\d{4}/\d{2}/\d{2}$').hasMatch(value)) {
return '正しい形式で入力してください (YYYY/MM/DD)';
}
final date = DateTime.tryParse(value.replaceAll('/', '-'));
if (date == null) {
return '有効な日付を入力してください';
}
if (date.isAfter(DateTime.now())) {
return '未来の日付は入力できません';
}
return null;
},
),
SizedBox(height: 16),
SwitchListTile(
title: Text('性別'),
subtitle: Text(_female ? '女性' : '男性'),
value: _female,
onChanged: (bool value) {
setState(() {
_female = value;
});
},
),
SizedBox(height: 16),
TextFormField(
initialValue: _user.email,
decoration: InputDecoration(
labelText: 'メールアドレス',
border: OutlineInputBorder(),
),
enabled: false,
),
SizedBox(height: 16),
SwitchListTile(
title: Text('アクティブ状態'),
value: _user.isActive,
onChanged: null,
),
SizedBox(height: 32),
ElevatedButton(
child: Text('更新'),
onPressed: _updateUserDetails,
),
],
),
),
);
}
void _updateUserDetails() async {
if (_formKey.currentState!.validate()) {
final dateOfBirth = DateTime.tryParse(_dateOfBirthController.text.replaceAll('/', '-'));
if (dateOfBirth == null || dateOfBirth.isAfter(DateTime.now())) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('生年月日が無効です', style: TextStyle(color: Colors.red))),
);
return;
}
User updatedUser = User(
id: _user.id,
email: _user.email,
firstname: _firstnameController.text,
lastname: _lastnameController.text,
dateOfBirth: dateOfBirth,
female: _female,
isActive: _user.isActive,
);
try {
bool success = await ApiService.updateUserDetail(updatedUser, indexController.currentUser[0]['token']);
if (success) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('個人情報が更新されました')),
);
indexController.updateCurrentUser(updatedUser);
Get.offAllNamed(AppPages.INDEX);
//Get.back();
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('更新に失敗しました', style: TextStyle(color: Colors.red))),
);
}
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('エラーが発生しました: $e', style: TextStyle(color: Colors.red))),
);
}
}
}
}