From c6ea7ee5bb6e24736c508a788cd78e6c652e6aa7 Mon Sep 17 00:00:00 2001 From: cha-imag <100341870+tkdals802@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:05:44 +0900 Subject: [PATCH] =?UTF-8?q?M3-168=20Feat=20:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8?= =?UTF-8?q?=20=ED=99=94=EB=A9=B4=EC=97=90=20=EC=A0=95=EA=B7=9C=EC=8B=9D=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/controllers/sign_up_controller.dart | 51 +++++++++++++++++++---- lib/controllers/user_info_controller.dart | 3 +- lib/screens/sign_up_screen.dart | 30 +++++++++---- lib/screens/user_info_update_screen.dart | 11 +++-- 4 files changed, 71 insertions(+), 24 deletions(-) diff --git a/lib/controllers/sign_up_controller.dart b/lib/controllers/sign_up_controller.dart index 11bdbabd..7ff3673b 100644 --- a/lib/controllers/sign_up_controller.dart +++ b/lib/controllers/sign_up_controller.dart @@ -1,3 +1,5 @@ +import 'package:dio/dio.dart'; +import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:image_picker/image_picker.dart'; @@ -11,6 +13,8 @@ class SignUpController extends GetxController { var profileImage = Rxn(); + final RegExp regExp = RegExp(r'^[A-Za-z가-힣0-9]{3,10}$'); + late RxList toggleSelection; bool isMale = true; bool isFemale = false; @@ -18,6 +22,7 @@ class SignUpController extends GetxController { RxString nickname = ''.obs; RxInt birthYear = 2000.obs; RxString gender = 'MALE'.obs; + RxString nicknameValidation = "".obs; RxBool isNicknameTyped = false.obs; @override @@ -59,16 +64,44 @@ class SignUpController extends GetxController { } } - void completeRegistration() async { - int? statusCode = await userService.putUserInfo( - gender: gender.value, - birthYear: birthYear.value, - nickname: nickname.value, - profileImagePath: profileImage.value?.path, + void showErrorDialog(String message) { + Get.dialog( + AlertDialog( + title: Text(message), + actions: [ + TextButton( + child: Text('확인'), + onPressed: () { + Get.back(); + }, + ), + ], + ), ); - if (statusCode == 200) { - await secureStorage.writeSignupStatus("true"); - Get.offAllNamed('/main'); + } + + void completeRegistration() async { + try { + int? statusCode = await userService.putUserInfo( + gender: gender.value, + birthYear: birthYear.value, + nickname: nickname.value, + profileImagePath: profileImage.value?.path, + ); + if (statusCode == 200) { + await secureStorage.writeSignupStatus("true"); + Get.offAllNamed('/main'); + } + } catch (e) { + if (!regExp.hasMatch(nickname.value)) { + showErrorDialog('닉네임 형식이 맞지 않습니다!'); + } + if (e is DioException) { + final response = e.response; + if (response != null && response.statusCode == 400) { + showErrorDialog('중복된 닉네임입니다!'); + } + } } } } diff --git a/lib/controllers/user_info_controller.dart b/lib/controllers/user_info_controller.dart index 5e34b92b..67e6e91f 100644 --- a/lib/controllers/user_info_controller.dart +++ b/lib/controllers/user_info_controller.dart @@ -87,7 +87,7 @@ class UserInfoController extends GetxController { void showErrorDialog(String message) { Get.dialog( AlertDialog( - title: Text('$message'), + title: Text(message), actions: [ TextButton( child: Text('확인'), @@ -108,7 +108,6 @@ class UserInfoController extends GetxController { nickname: nickname.value, profileImagePath: profileImage.value?.path, ); - if (statusCode == 200) { Get.offAllNamed('/main'); } diff --git a/lib/screens/sign_up_screen.dart b/lib/screens/sign_up_screen.dart index fe55bb26..ffc89324 100644 --- a/lib/screens/sign_up_screen.dart +++ b/lib/screens/sign_up_screen.dart @@ -12,11 +12,12 @@ class SignUpScreen extends StatelessWidget { @override Widget build(BuildContext context) { final SignUpController controller = Get.put(SignUpController()); + final RegExp regExp = RegExp(r'^[A-Za-z가-힣0-9]{3,10}$'); const int lowBoundYear = 1900; const int upperBoundYear = 2024; return GestureDetector( - onTap: (){ + onTap: () { FocusScope.of(context).unfocus(); }, child: Scaffold( @@ -86,13 +87,28 @@ class SignUpScreen extends StatelessWidget { Container( color: Color(0xffD9D9D9), child: TextField( - decoration: InputDecoration( + decoration: InputDecoration( border: InputBorder.none, - contentPadding: - EdgeInsets.symmetric(horizontal: 8.0, vertical: 12.0), + contentPadding: EdgeInsets.symmetric( + horizontal: 8.0, + vertical: 12.0, + ), ), style: TextStyle(fontSize: 16.0), - onChanged: controller.updateNickname, + onChanged: (value) { + controller.updateNickname(value); + if (!regExp.hasMatch(value)) { + controller.nicknameValidation.value = "형식에 맞지 않습니다!"; + } else { + controller.nicknameValidation.value = ""; + } + }, + ), + ), + Align( + alignment: Alignment.centerLeft, + child: Obx( + () => Text(controller.nicknameValidation.value), ), ), ], @@ -160,8 +176,8 @@ class SignUpScreen extends StatelessWidget { Obx( () => ToggleButtons( constraints: BoxConstraints.expand( - width: - (MediaQuery.of(context).size.width - 100) / 2,), // + width: (MediaQuery.of(context).size.width - 100) / 2, + ), // isSelected: controller.toggleSelection, onPressed: controller.updateSelectedGender, children: [ diff --git a/lib/screens/user_info_update_screen.dart b/lib/screens/user_info_update_screen.dart index 3b27365b..780488f4 100644 --- a/lib/screens/user_info_update_screen.dart +++ b/lib/screens/user_info_update_screen.dart @@ -2,7 +2,6 @@ import 'dart:io'; import 'package:dropdown_button2/dropdown_button2.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:get/get.dart'; import '../controllers/user_info_controller.dart'; @@ -108,9 +107,9 @@ class UserInfoUpdateScreen extends StatelessWidget { onChanged: (value) { controller.nickname.value = value; if (!regExp.hasMatch(value)) { - controller.nicknameValidation.value = "형식에 맞지 않습니다!"; - print("missmiss match! ${value}"); - }else{ + controller.nicknameValidation.value = + "형식에 맞지 않습니다!"; + } else { controller.nicknameValidation.value = ""; } }, @@ -119,9 +118,9 @@ class UserInfoUpdateScreen extends StatelessWidget { Align( alignment: Alignment.centerLeft, child: Obx( - () => Text('${controller.nicknameValidation.value}'), + () => Text(controller.nicknameValidation.value), ), - ) + ), ], ), SizedBox(height: 16),