From 0f37f378a3d22f5498334775a073929ca3c96a1b Mon Sep 17 00:00:00 2001 From: maxxkim Date: Tue, 5 Mar 2024 12:46:01 +0300 Subject: [PATCH] refactor(diary): Refactoring flutter analyze finished. Started refactoring Run Code Metrics --- .../language_select_screen.dart | 60 ++++---- lib/widgets/app_bar/appbar_leading_image.dart | 8 +- lib/widgets/app_bar/appbar_subtitle.dart | 8 +- .../app_bar/appbar_title_circleimage.dart | 8 +- lib/widgets/app_bar/appbar_title_image.dart | 8 +- lib/widgets/app_bar/exit_button.dart | 8 +- lib/widgets/custom_bottom_bar.dart | 129 ++++++++---------- lib/widgets/custom_text_form_field.dart | 118 +++++++++------- lib/widgets/next_step_button.dart | 78 ++++++----- 9 files changed, 225 insertions(+), 200 deletions(-) diff --git a/lib/presentation/registration/language_select_screen/language_select_screen.dart b/lib/presentation/registration/language_select_screen/language_select_screen.dart index cd8b9b4b..56e644a0 100644 --- a/lib/presentation/registration/language_select_screen/language_select_screen.dart +++ b/lib/presentation/registration/language_select_screen/language_select_screen.dart @@ -64,23 +64,22 @@ class LanguageSelectScreen extends StatelessWidget { languageSelectModelObj, ) { return CustomDropDown( - icon: Container( - margin: - EdgeInsets.fromLTRB(30.h, 11.v, 17.h, 11.v), - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(1.h)), - child: CustomImageView( - imagePath: ImageConstant.imgFavorite, - height: 12.adaptSize, - width: 12.adaptSize)), - hintText: "Не выбрано".tr, - items: - languageSelectModelObj?.dropdownItemList ?? [], - onChanged: (value) { - context - .read() - .add(ChangeDropDownEvent(value: value)); - }); + icon: Container( + margin: EdgeInsets.fromLTRB(30.h, 11.v, 17.h, 11.v), + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(1.h), + ), + child: CustomImageView( + imagePath: ImageConstant.imgFavorite, + height: 12.adaptSize, + width: 12.adaptSize, + ), + ), + hintText: "Не выбрано".tr, + items: languageSelectModelObj?.dropdownItemList ?? [], + onChanged: (value) => + _onDropDownChanged(context, value), + ); }), Spacer(flex: 63), ])), @@ -92,15 +91,24 @@ class LanguageSelectScreen extends StatelessWidget { margin: EdgeInsets.only(left: 16.h, right: 16.h, bottom: 50.v), buttonStyle: CustomButtonStyles.outlinePrimaryTL15, - onPressed: () { - UserRegistrationSheet sheet = - state.languageSelectModelObj!.userRegistrationSheet; - Navigator.pushNamed( - context, - AppRoutes.credentialsScreen, - arguments: sheet, - ); - }); + onPressed: _saveAndPush(state, context)); }))); } + + void Function()? _saveAndPush( + LanguageSelectState state, BuildContext context) { + return () { + UserRegistrationSheet sheet = + state.languageSelectModelObj!.userRegistrationSheet; + Navigator.pushNamed( + context, + AppRoutes.credentialsScreen, + arguments: sheet, + ); + }; + } + + void _onDropDownChanged(BuildContext context, value) { + context.read().add(ChangeDropDownEvent(value: value)); + } } diff --git a/lib/widgets/app_bar/appbar_leading_image.dart b/lib/widgets/app_bar/appbar_leading_image.dart index 5c9ff8e4..003d9f03 100644 --- a/lib/widgets/app_bar/appbar_leading_image.dart +++ b/lib/widgets/app_bar/appbar_leading_image.dart @@ -21,9 +21,7 @@ class AppbarLeadingImage extends StatelessWidget { @override Widget build(BuildContext context) { return InkWell( - onTap: () { - onTap!.call(); - }, + onTap: onTapCall, child: Padding( padding: margin ?? EdgeInsets.zero, child: CustomImageView( @@ -35,4 +33,8 @@ class AppbarLeadingImage extends StatelessWidget { ), ); } + + void onTapCall() { + onTap!.call(); + } } diff --git a/lib/widgets/app_bar/appbar_subtitle.dart b/lib/widgets/app_bar/appbar_subtitle.dart index 69c1b639..58779324 100644 --- a/lib/widgets/app_bar/appbar_subtitle.dart +++ b/lib/widgets/app_bar/appbar_subtitle.dart @@ -21,9 +21,7 @@ class AppbarSubtitle extends StatelessWidget { @override Widget build(BuildContext context) { return GestureDetector( - onTap: () { - onTap!.call(); - }, + onTap: onTapCall, child: Padding( padding: margin ?? EdgeInsets.zero, child: Text( @@ -35,4 +33,8 @@ class AppbarSubtitle extends StatelessWidget { ), ); } + + void onTapCall() { + onTap!.call(); + } } diff --git a/lib/widgets/app_bar/appbar_title_circleimage.dart b/lib/widgets/app_bar/appbar_title_circleimage.dart index ee412291..66f82ef9 100644 --- a/lib/widgets/app_bar/appbar_title_circleimage.dart +++ b/lib/widgets/app_bar/appbar_title_circleimage.dart @@ -22,9 +22,7 @@ class AppbarTitleCircleimage extends StatelessWidget { Widget build(BuildContext context) { return InkWell( borderRadius: BorderRadiusStyle.circleBorder8, - onTap: () { - onTap!.call(); - }, + onTap: onTapCall, child: Padding( padding: margin ?? EdgeInsets.zero, child: CustomImageView( @@ -39,4 +37,8 @@ class AppbarTitleCircleimage extends StatelessWidget { ), ); } + + void onTapCall() { + onTap!.call(); + } } diff --git a/lib/widgets/app_bar/appbar_title_image.dart b/lib/widgets/app_bar/appbar_title_image.dart index 3ccfd581..e15f1c52 100644 --- a/lib/widgets/app_bar/appbar_title_image.dart +++ b/lib/widgets/app_bar/appbar_title_image.dart @@ -21,9 +21,7 @@ class AppbarTitleImage extends StatelessWidget { @override Widget build(BuildContext context) { return InkWell( - onTap: () { - onTap!.call(); - }, + onTap: onTapCall, child: Padding( padding: margin ?? EdgeInsets.zero, child: CustomImageView( @@ -35,4 +33,8 @@ class AppbarTitleImage extends StatelessWidget { ), ); } + + void onTapCall() { + onTap!.call(); + } } diff --git a/lib/widgets/app_bar/exit_button.dart b/lib/widgets/app_bar/exit_button.dart index a93c9a84..ee0d27dd 100644 --- a/lib/widgets/app_bar/exit_button.dart +++ b/lib/widgets/app_bar/exit_button.dart @@ -21,9 +21,7 @@ class ExitButton extends StatelessWidget { @override Widget build(BuildContext context) { return GestureDetector( - onTap: () { - onTap!.call(); - }, + onTap: onTapCall, child: Padding( padding: margin ?? EdgeInsets.zero, child: Text( @@ -35,4 +33,8 @@ class ExitButton extends StatelessWidget { ), ); } + + void onTapCall() { + onTap!.call(); + } } diff --git a/lib/widgets/custom_bottom_bar.dart b/lib/widgets/custom_bottom_bar.dart index c32781a2..45928ebd 100644 --- a/lib/widgets/custom_bottom_bar.dart +++ b/lib/widgets/custom_bottom_bar.dart @@ -65,60 +65,63 @@ class CustomBottomBarState extends State { elevation: 0, currentIndex: selectedIndex, type: BottomNavigationBarType.fixed, - items: List.generate(bottomMenuList.length, (index) { - return BottomNavigationBarItem( - icon: Column( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - CustomImageView( - imagePath: bottomMenuList[index].icon, - height: 24.v, - width: 19.h, - color: appTheme.blueGray400, - ), - Padding( - padding: EdgeInsets.only(top: 8.v), - child: Text( - bottomMenuList[index].title ?? "", - style: - CustomTextStyles.bodySmallMulishBluegray80012.copyWith( - color: appTheme.blueGray800, - ), - ), - ), - ], - ), - activeIcon: Column( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - CustomImageView( - imagePath: bottomMenuList[index].activeIcon, - height: 24.adaptSize, - width: 24.adaptSize, - color: theme.colorScheme.primary, - ), - Padding( - padding: EdgeInsets.only(top: 9.v), - child: Text( - bottomMenuList[index].title ?? "", - style: CustomTextStyles.bodySmallMulishPrimary.copyWith( - color: theme.colorScheme.primary, - ), - ), - ), - ], - ), - label: '', - ); - }), - onTap: (index) { - selectedIndex = index; - widget.onChanged?.call(bottomMenuList[index].type); - setState(() {}); - }, + items: List.generate(bottomMenuList.length, bottomMenu), + onTap: changeMeny, ), ); } + + void changeMeny(index) { + selectedIndex = index; + widget.onChanged?.call(bottomMenuList[index].type); + setState(() {}); + } + + BottomNavigationBarItem bottomMenu(index) { + return BottomNavigationBarItem( + icon: Column( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + CustomImageView( + imagePath: bottomMenuList[index].icon, + height: 24.v, + width: 19.h, + color: appTheme.blueGray400, + ), + Padding( + padding: EdgeInsets.only(top: 8.v), + child: Text( + bottomMenuList[index].title ?? "", + style: CustomTextStyles.bodySmallMulishBluegray80012.copyWith( + color: appTheme.blueGray800, + ), + ), + ), + ], + ), + activeIcon: Column( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + CustomImageView( + imagePath: bottomMenuList[index].activeIcon, + height: 24.adaptSize, + width: 24.adaptSize, + color: theme.colorScheme.primary, + ), + Padding( + padding: EdgeInsets.only(top: 9.v), + child: Text( + bottomMenuList[index].title ?? "", + style: CustomTextStyles.bodySmallMulishPrimary.copyWith( + color: theme.colorScheme.primary, + ), + ), + ), + ], + ), + label: '', + ); + } } enum BottomBarEnum { @@ -141,27 +144,3 @@ class BottomMenuModel { BottomBarEnum type; } - -class DefaultWidget extends StatelessWidget { - @override - Widget build(BuildContext context) { - return Container( - color: Colors.white, - padding: EdgeInsets.all(10), - child: Center( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - 'Please replace the respective Widget here', - style: TextStyle( - fontSize: 18, - ), - ), - ], - ), - ), - ); - } -} diff --git a/lib/widgets/custom_text_form_field.dart b/lib/widgets/custom_text_form_field.dart index a96c5a1c..65ee6920 100644 --- a/lib/widgets/custom_text_form_field.dart +++ b/lib/widgets/custom_text_form_field.dart @@ -81,67 +81,79 @@ class CustomTextFormField extends StatelessWidget { final void Function(String?)? onSaved; + To resolve the error "Avoid returning widgets from a getter," you can refactor the textFormFieldWidget and decoration getters into regular methods and then call those methods within the build method. Here's the refactored code: + +dart + +class CustomTextFormField extends StatelessWidget { + // ... other properties remain the same + @override Widget build(BuildContext context) { return alignment != null ? Align( alignment: alignment ?? Alignment.center, - child: textFormFieldWidget, + child: _buildTextFormFieldWidget(), ) - : textFormFieldWidget; + : _buildTextFormFieldWidget(); } - Widget get textFormFieldWidget => SizedBox( - width: width ?? double.maxFinite, - child: TextFormField( - controller: controller, - focusNode: focusNode ?? FocusNode(), - autofocus: autofocus!, - style: textStyle ?? theme.textTheme.titleMedium, - obscureText: obscureText!, - textInputAction: textInputAction, - maxLines: maxLines ?? 1, - decoration: decoration, - validator: validator, - keyboardType: keyboardType, - onSaved: onSaved, - inputFormatters: inputFormatters, - ), - ); - InputDecoration get decoration => InputDecoration( - hintText: hintText ?? "", - hintStyle: hintStyle ?? theme.textTheme.bodyLarge, - prefixIcon: prefix, - prefixIconConstraints: prefixConstraints, - suffixIcon: suffix, - suffixIconConstraints: suffixConstraints, - isDense: true, - contentPadding: contentPadding ?? EdgeInsets.all(6.h), - fillColor: fillColor ?? theme.colorScheme.onPrimary, - filled: filled, - border: borderDecoration ?? - OutlineInputBorder( - borderRadius: BorderRadius.circular(8.h), - borderSide: BorderSide( - color: appTheme.blueA400, - width: 1, - ), + Widget _buildTextFormFieldWidget() { + return SizedBox( + width: width ?? double.maxFinite, + child: TextFormField( + controller: controller, + focusNode: focusNode ?? FocusNode(), + autofocus: autofocus!, + style: textStyle ?? theme.textTheme.titleMedium, + obscureText: obscureText!, + textInputAction: textInputAction, + maxLines: maxLines ?? 1, + decoration: _buildInputDecoration(), + validator: validator, + keyboardType: keyboardType, + onSaved: onSaved, + inputFormatters: inputFormatters, + ), + ); + } + + InputDecoration _buildInputDecoration() { + return InputDecoration( + hintText: hintText ?? "", + hintStyle: hintStyle ?? theme.textTheme.bodyLarge, + prefixIcon: prefix, + prefixIconConstraints: prefixConstraints, + suffixIcon: suffix, + suffixIconConstraints: suffixConstraints, + isDense: true, + contentPadding: contentPadding ?? EdgeInsets.all(6.h), + fillColor: fillColor ?? theme.colorScheme.onPrimary, + filled: filled, + border: borderDecoration ?? + OutlineInputBorder( + borderRadius: BorderRadius.circular(8.h), + borderSide: BorderSide( + color: appTheme.blueA400, + width: 1, ), - enabledBorder: borderDecoration ?? - OutlineInputBorder( - borderRadius: BorderRadius.circular(8.h), - borderSide: BorderSide( - color: appTheme.blueA400, - width: 1, - ), + ), + enabledBorder: borderDecoration ?? + OutlineInputBorder( + borderRadius: BorderRadius.circular(8.h), + borderSide: BorderSide( + color: appTheme.blueA400, + width: 1, ), - focusedBorder: borderDecoration ?? - OutlineInputBorder( - borderRadius: BorderRadius.circular(8.h), - borderSide: BorderSide( - color: appTheme.blueA400, - width: 1, - ), + ), + focusedBorder: borderDecoration ?? + OutlineInputBorder( + borderRadius: BorderRadius.circular(8.h), + borderSide: BorderSide( + color: appTheme.blueA400, + width: 1, ), - ); -} + ), + ); + } +} \ No newline at end of file diff --git a/lib/widgets/next_step_button.dart b/lib/widgets/next_step_button.dart index ce972835..455f8f94 100644 --- a/lib/widgets/next_step_button.dart +++ b/lib/widgets/next_step_button.dart @@ -31,43 +31,59 @@ class NextStepButton extends BaseButton { ); final BoxDecoration? decoration; - final Widget? leftIcon; - final Widget? rightIcon; - final Widget? label; @override Widget build(BuildContext context) { - return alignment != null + return (alignment != null) ? Align( - alignment: alignment ?? Alignment.center, - child: buildOutlinedButtonWidget, - ) - : buildOutlinedButtonWidget; - } - - Widget get buildOutlinedButtonWidget => Container( - height: this.height ?? 30.v, - width: this.width ?? double.maxFinite, - margin: margin, - decoration: decoration, - child: OutlinedButton( - style: buttonStyle, - onPressed: isDisabled ?? false ? null : onPressed ?? () {}, - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - leftIcon ?? const SizedBox.shrink(), - Text( - text, - style: buttonTextStyle ?? theme.textTheme.titleSmall, + alignment: alignment!, + child: Container( + height: this.height ?? 30.v, + width: this.width ?? double.maxFinite, + margin: margin, + decoration: decoration, + child: OutlinedButton( + style: buttonStyle, + onPressed: isDisabled ?? false ? null : onPressed ?? () {}, + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + leftIcon ?? const SizedBox.shrink(), + Text( + text, + style: buttonTextStyle ?? theme.textTheme.titleSmall, + ), + rightIcon ?? const SizedBox.shrink(), + ], + ), ), - rightIcon ?? const SizedBox.shrink(), - ], - ), - ), - ); + ), + ) + : Container( + height: this.height ?? 30.v, + width: this.width ?? double.maxFinite, + margin: margin, + decoration: decoration, + child: OutlinedButton( + style: buttonStyle, + onPressed: isDisabled ?? false ? null : onPressed ?? () {}, + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + leftIcon ?? const SizedBox.shrink(), + Text( + text, + style: buttonTextStyle ?? theme.textTheme.titleSmall, + ), + rightIcon ?? const SizedBox.shrink(), + ], + ), + ), + ); + } }