From a6d5d1107afa70a1484f553e926bef1f37af665f Mon Sep 17 00:00:00 2001 From: Rafael <1024481@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 17:38:47 +0200 Subject: [PATCH] some code refactoring --- lib/pages/user_data_page.dart | 14 +----- lib/pages/user_profile_page.dart | 79 ++++++++------------------------ lib/pages/user_vision_page.dart | 5 +- lib/utils/helper.dart | 15 ++++++ 4 files changed, 39 insertions(+), 74 deletions(-) diff --git a/lib/pages/user_data_page.dart b/lib/pages/user_data_page.dart index 2ac649d..67e6e33 100644 --- a/lib/pages/user_data_page.dart +++ b/lib/pages/user_data_page.dart @@ -13,6 +13,7 @@ import '../models/language.dart'; import '../models/language_setting.dart'; import '../models/location.dart'; import '../services/auth/auth_service.dart'; +import '../utils/helper.dart'; import '../utils/math.dart'; class UserDataPage extends StatefulWidget { @@ -590,17 +591,6 @@ class _UserDataPageState extends State { ); void _showSnackBar(String message) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text(message), - backgroundColor: Colors.red, - action: SnackBarAction( - label: 'Dismiss', - onPressed: () { - ScaffoldMessenger.of(context).hideCurrentSnackBar(); - }, - ), - ), - ); + showErrorSnackBar(context, message); } } diff --git a/lib/pages/user_profile_page.dart b/lib/pages/user_profile_page.dart index b717a4a..06adb62 100644 --- a/lib/pages/user_profile_page.dart +++ b/lib/pages/user_profile_page.dart @@ -276,14 +276,7 @@ class _UserProfilePageState extends State { if (isOwner) Padding( padding: const EdgeInsets.only(left: 8.0), - child: Align( - alignment: Alignment.topRight, - child: OutlinedButton.icon( - label: const Text('Edit'), - icon: const Icon(Icons.edit), - onPressed: editUserCommunicationInfo, - ), - ), + child: _editButton(context, editUserCommunicationInfo), ), ], ), @@ -344,14 +337,7 @@ class _UserProfilePageState extends State { if (isOwner) Padding( padding: const EdgeInsets.only(left: 8.0), - child: Align( - alignment: Alignment.topRight, - child: OutlinedButton.icon( - label: const Text('Edit'), - icon: const Icon(Icons.edit), - onPressed: editUserWorkCultureInfo, - ), - ), + child: _editButton(context, editUserWorkCultureInfo), ), ], ), @@ -412,15 +398,7 @@ class _UserProfilePageState extends State { ], ), ), - if (isOwner) - Align( - alignment: Alignment.topRight, - child: OutlinedButton.icon( - label: const Text('Edit'), - icon: const Icon(Icons.edit), - onPressed: editUserVisionInfo, - ), - ), + if (isOwner) _editButton(context, editUserVisionInfo), ], ), const SizedBox(height: 16), @@ -480,15 +458,7 @@ class _UserProfilePageState extends State { ], ), ), - if (isOwner) - Align( - alignment: Alignment.topRight, - child: OutlinedButton.icon( - label: const Text('Edit'), - icon: const Icon(Icons.edit), - onPressed: editUserSkillsInfo, - ), - ), + if (isOwner) _editButton(context, editUserSkillsInfo), ], ), const SizedBox(height: 16), @@ -513,15 +483,7 @@ class _UserProfilePageState extends State { ], ), ), - if (isOwner) - Align( - alignment: Alignment.topRight, - child: OutlinedButton.icon( - label: const Text('Edit'), - icon: const Icon(Icons.edit), - onPressed: editUserSkillsSoughtInfo, - ), - ), + if (isOwner) _editButton(context, editUserSkillsSoughtInfo), ], ), ], @@ -560,14 +522,8 @@ class _UserProfilePageState extends State { style: const TextStyle(fontSize: 16)), ], ), - Align( - alignment: Alignment.bottomRight, - child: OutlinedButton.icon( - label: const Text('Edit'), - icon: const Icon(Icons.edit), - onPressed: editUserDataInfo, - ), - ), + _editButton(context, editUserDataInfo, + alignment: Alignment.bottomRight), ], ), if (isOwner) const SizedBox(height: 16), @@ -632,14 +588,7 @@ class _UserProfilePageState extends State { return Column( children: [ if (isOwner) - Align( - alignment: Alignment.bottomRight, - child: OutlinedButton.icon( - label: const Text('Edit'), - icon: const Icon(Icons.edit), - onPressed: editNameInfo, - ), - ), + _editButton(context, editNameInfo, alignment: Alignment.bottomRight), CircleAvatar( radius: 80, backgroundImage: @@ -683,4 +632,16 @@ class _UserProfilePageState extends State { ], ); } + + Widget _editButton(BuildContext context, void Function()? onPressedFunction, + {Alignment alignment = Alignment.topRight}) { + return Align( + alignment: alignment, + child: OutlinedButton.icon( + label: const Text('Edit'), + icon: const Icon(Icons.edit), + onPressed: onPressedFunction, + ), + ); + } } diff --git a/lib/pages/user_vision_page.dart b/lib/pages/user_vision_page.dart index 063ee3f..1255147 100644 --- a/lib/pages/user_vision_page.dart +++ b/lib/pages/user_vision_page.dart @@ -5,6 +5,7 @@ import '../constants.dart'; import '../enumerations.dart'; import '../forms/corporate_culture_form.dart'; import '../services/auth/auth_service.dart'; +import '../utils/helper.dart'; class UserVisionPage extends StatefulWidget { const UserVisionPage( @@ -87,9 +88,7 @@ class UserVisionPageState extends State { } void _showSnackBar(String message) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text(message)), - ); + showErrorSnackBar(context, message); } bool isVisionSelected() { diff --git a/lib/utils/helper.dart b/lib/utils/helper.dart index c950e05..9d7d1a7 100644 --- a/lib/utils/helper.dart +++ b/lib/utils/helper.dart @@ -96,3 +96,18 @@ void showMsg(BuildContext context, String title, String content) { ), ); } + +void showErrorSnackBar(BuildContext context, String message) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(message), + backgroundColor: Colors.red, + action: SnackBarAction( + label: 'Dismiss', + onPressed: () { + ScaffoldMessenger.of(context).hideCurrentSnackBar(); + }, + ), + ), + ); +} \ No newline at end of file