mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 14:47:23 +00:00
Refactor CreateSubSpaceDialog
to convert to StatefulWidget and manage text controller lifecycle
This commit is contained in:
@ -13,7 +13,7 @@ import 'package:syncrow_web/pages/spaces_management/space_model/models/subspace_
|
|||||||
import 'package:syncrow_web/utils/color_manager.dart';
|
import 'package:syncrow_web/utils/color_manager.dart';
|
||||||
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
||||||
|
|
||||||
class CreateSubSpaceDialog extends StatelessWidget {
|
class CreateSubSpaceDialog extends StatefulWidget {
|
||||||
final bool isEdit;
|
final bool isEdit;
|
||||||
final String dialogTitle;
|
final String dialogTitle;
|
||||||
final List<SubspaceModel>? existingSubSpaces;
|
final List<SubspaceModel>? existingSubSpaces;
|
||||||
@ -34,14 +34,31 @@ class CreateSubSpaceDialog extends StatelessWidget {
|
|||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
State<CreateSubSpaceDialog> createState() => _CreateSubSpaceDialogState();
|
||||||
final textController = TextEditingController();
|
}
|
||||||
|
|
||||||
|
class _CreateSubSpaceDialogState extends State<CreateSubSpaceDialog> {
|
||||||
|
late final TextEditingController _subspaceNameController;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
_subspaceNameController = TextEditingController();
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_subspaceNameController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (_) {
|
create: (_) {
|
||||||
final bloc = SubSpaceBloc();
|
final bloc = SubSpaceBloc();
|
||||||
if (existingSubSpaces != null) {
|
if (widget.existingSubSpaces != null) {
|
||||||
for (final subSpace in existingSubSpaces ?? []) {
|
for (final subSpace in widget.existingSubSpaces ?? []) {
|
||||||
bloc.add(AddSubSpace(subSpace));
|
bloc.add(AddSubSpace(subSpace));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,7 +79,7 @@ class CreateSubSpaceDialog extends StatelessWidget {
|
|||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
dialogTitle,
|
widget.dialogTitle,
|
||||||
style: context.textTheme.headlineLarge?.copyWith(
|
style: context.textTheme.headlineLarge?.copyWith(
|
||||||
color: ColorsManager.blackColor,
|
color: ColorsManager.blackColor,
|
||||||
),
|
),
|
||||||
@ -115,7 +132,7 @@ class CreateSubSpaceDialog extends StatelessWidget {
|
|||||||
SizedBox(
|
SizedBox(
|
||||||
width: 200,
|
width: 200,
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: textController,
|
controller: _subspaceNameController,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
border: InputBorder.none,
|
border: InputBorder.none,
|
||||||
hintText: state.subSpaces.isEmpty
|
hintText: state.subSpaces.isEmpty
|
||||||
@ -126,16 +143,17 @@ class CreateSubSpaceDialog extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
onSubmitted: (value) {
|
onSubmitted: (value) {
|
||||||
if (value.trim().isNotEmpty) {
|
final trimmedValue = value.trim();
|
||||||
|
if (trimmedValue.isNotEmpty) {
|
||||||
context.read<SubSpaceBloc>().add(
|
context.read<SubSpaceBloc>().add(
|
||||||
AddSubSpace(
|
AddSubSpace(
|
||||||
SubspaceModel(
|
SubspaceModel(
|
||||||
subspaceName: value.trim(),
|
subspaceName: trimmedValue,
|
||||||
disabled: false,
|
disabled: false,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
textController.clear();
|
_subspaceNameController.clear();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
style: context.textTheme.bodyMedium,
|
style: context.textTheme.bodyMedium,
|
||||||
@ -172,7 +190,7 @@ class CreateSubSpaceDialog extends StatelessWidget {
|
|||||||
? () {
|
? () {
|
||||||
final subSpacesBloc = context.read<SubSpaceBloc>();
|
final subSpacesBloc = context.read<SubSpaceBloc>();
|
||||||
final subSpaces = subSpacesBloc.state.subSpaces;
|
final subSpaces = subSpacesBloc.state.subSpaces;
|
||||||
onSave?.call(subSpaces);
|
widget.onSave?.call(subSpaces);
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
|
Reference in New Issue
Block a user