Refactor CreateSubSpaceDialog layout for improved readability and maintainability

This commit is contained in:
Faris Armoush
2025-04-17 09:38:54 +03:00
parent ae95d06482
commit a6fc99443b

View File

@ -54,150 +54,139 @@ class CreateSubSpaceDialog extends StatelessWidget {
child: BlocBuilder<SubSpaceBloc, SubSpaceState>( child: BlocBuilder<SubSpaceBloc, SubSpaceState>(
builder: (context, state) { builder: (context, state) {
return Container( return Container(
width: context.screenWidth * 0.35,
color: ColorsManager.whiteColors, color: ColorsManager.whiteColors,
child: SizedBox( padding: const EdgeInsets.all(16),
width: context.screenWidth * 0.35, child: Column(
child: Padding( crossAxisAlignment: CrossAxisAlignment.start,
padding: const EdgeInsets.all(16.0), mainAxisSize: MainAxisSize.min,
child: Column( children: [
crossAxisAlignment: CrossAxisAlignment.start, Text(
mainAxisSize: MainAxisSize.min, dialogTitle,
children: [ style: context.textTheme.headlineLarge?.copyWith(
Text( color: ColorsManager.blackColor,
dialogTitle, ),
style: Theme.of(context).textTheme.headlineLarge?.copyWith( ),
color: ColorsManager.blackColor, const SizedBox(height: 16),
), Container(
), width: context.screenWidth * 0.35,
const SizedBox(height: 16), padding: const EdgeInsets.symmetric(
Container( vertical: 10,
width: context.screenWidth * 0.35, horizontal: 16,
padding: const EdgeInsets.symmetric( ),
vertical: 10.0, decoration: BoxDecoration(
horizontal: 16.0, color: ColorsManager.boxColor,
), borderRadius: BorderRadius.circular(10),
decoration: BoxDecoration( ),
color: ColorsManager.boxColor, child: Wrap(
borderRadius: BorderRadius.circular(10), spacing: 8,
), runSpacing: 8,
child: Wrap( alignment: WrapAlignment.start,
spacing: 8, crossAxisAlignment: WrapCrossAlignment.center,
runSpacing: 8, children: [
alignment: WrapAlignment.start, ...state.subSpaces.asMap().entries.map(
crossAxisAlignment: WrapCrossAlignment.center, (entry) {
children: [ final index = entry.key;
...state.subSpaces.asMap().entries.map( final subSpace = entry.value;
(entry) {
final index = entry.key;
final subSpace = entry.value;
final lowerName = final lowerName = subSpace.subspaceName.toLowerCase();
subSpace.subspaceName.toLowerCase();
final duplicateIndices = state.subSpaces final duplicateIndices = state.subSpaces
.asMap() .asMap()
.entries .entries
.where((e) => .where((e) =>
e.value.subspaceName.toLowerCase() == e.value.subspaceName.toLowerCase() == lowerName)
lowerName) .map((e) => e.key)
.map((e) => e.key) .toList();
.toList(); final isDuplicate = duplicateIndices.length > 1 &&
final isDuplicate = duplicateIndices.length > 1 && duplicateIndices.indexOf(index) != 0;
duplicateIndices.indexOf(index) != 0; return SubspaceChip(
return SubspaceChip( subSpace: SubspaceTemplateModel(
subSpace: SubspaceTemplateModel( subspaceName: entry.value.subspaceName,
subspaceName: entry.value.subspaceName, disabled: entry.value.disabled,
disabled: entry.value.disabled, ),
isDuplicate: isDuplicate,
onDeleted: () => context.read<SubSpaceBloc>().add(
RemoveSubSpace(subSpace),
), ),
isDuplicate: isDuplicate, );
onDeleted: () => context.read<SubSpaceBloc>().add( },
RemoveSubSpace(subSpace), ),
), SizedBox(
); width: 200,
}, child: TextField(
), controller: textController,
SizedBox( decoration: InputDecoration(
width: 200, border: InputBorder.none,
child: TextField( hintText: state.subSpaces.isEmpty
controller: textController, ? 'Please enter the name'
decoration: InputDecoration( : null,
border: InputBorder.none, hintStyle: context.textTheme.bodySmall?.copyWith(
hintText: state.subSpaces.isEmpty color: ColorsManager.lightGrayColor,
? 'Please enter the name'
: null,
hintStyle: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(
color: ColorsManager.lightGrayColor,
),
),
onSubmitted: (value) {
if (value.trim().isNotEmpty) {
context.read<SubSpaceBloc>().add(
AddSubSpace(
SubspaceModel(
subspaceName: value.trim(),
disabled: false,
),
),
);
textController.clear();
}
},
style: Theme.of(context).textTheme.bodyMedium,
), ),
), ),
], onSubmitted: (value) {
if (value.trim().isNotEmpty) {
context.read<SubSpaceBloc>().add(
AddSubSpace(
SubspaceModel(
subspaceName: value.trim(),
disabled: false,
),
),
);
textController.clear();
}
},
style: context.textTheme.bodyMedium,
),
),
],
),
),
if (state.errorMessage.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
state.errorMessage,
style: context.textTheme.bodySmall?.copyWith(
color: ColorsManager.warningRed,
), ),
), ),
if (state.errorMessage.isNotEmpty) ),
Padding( const SizedBox(height: 16),
padding: const EdgeInsets.only(top: 8.0), Row(
child: Text( children: [
state.errorMessage, Expanded(
style: Theme.of(context).textTheme.bodySmall?.copyWith( child: CancelButton(
color: ColorsManager.warningRed, label: 'Cancel',
), onPressed: () async {
), Navigator.of(context).pop();
},
),
),
const SizedBox(width: 10),
Expanded(
child: DefaultButton(
onPressed: state.errorMessage.isEmpty
? () {
final subSpacesBloc = context.read<SubSpaceBloc>();
final subSpaces = subSpacesBloc.state.subSpaces;
onSave?.call(subSpaces);
Navigator.of(context).pop();
}
: null,
backgroundColor: ColorsManager.secondaryColor,
borderRadius: 10,
foregroundColor: state.errorMessage.isNotEmpty
? ColorsManager.whiteColorsWithOpacity
: ColorsManager.whiteColors,
child: const Text('OK'),
), ),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: CancelButton(
label: 'Cancel',
onPressed: () async {
Navigator.of(context).pop();
},
),
),
const SizedBox(width: 10),
Expanded(
child: DefaultButton(
onPressed: (state.errorMessage.isNotEmpty)
? null
: () async {
final subSpaces = context
.read<SubSpaceBloc>()
.state
.subSpaces;
onSave!(subSpaces);
Navigator.of(context).pop();
},
backgroundColor: ColorsManager.secondaryColor,
borderRadius: 10,
foregroundColor: state.errorMessage.isNotEmpty
? ColorsManager.whiteColorsWithOpacity
: ColorsManager.whiteColors,
child: const Text('OK'),
),
),
],
), ),
], ],
), ),
), ],
), ),
); );
}, },