mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
Refactor sub-space dialog to use Bloc for state management and simplify confirmation handling
This commit is contained in:
@ -1,7 +1,10 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flutter_svg/svg.dart';
|
import 'package:flutter_svg/svg.dart';
|
||||||
|
import 'package:syncrow_web/pages/device_managment/all_devices/bloc/device_mgmt_bloc/device_managment_bloc.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
|
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
|
||||||
|
import 'package:syncrow_web/pages/device_managment/device_setting/bloc/setting_bloc_bloc.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/device_setting/settings_model/device_info_model.dart';
|
import 'package:syncrow_web/pages/device_managment/device_setting/settings_model/device_info_model.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/device_setting/settings_model/sub_space_model.dart';
|
import 'package:syncrow_web/pages/device_managment/device_setting/settings_model/sub_space_model.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/device_setting/sub_space_dialog.dart';
|
import 'package:syncrow_web/pages/device_managment/device_setting/sub_space_dialog.dart';
|
||||||
@ -66,14 +69,25 @@ class DeviceManagementContent extends StatelessWidget {
|
|||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.all(10.0),
|
padding: const EdgeInsets.all(10.0),
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
onTap: () {
|
onTap: () async {
|
||||||
showSubSpaceDialog(
|
final selectedSubSpace = await showSubSpaceDialog(
|
||||||
context,
|
context,
|
||||||
communityUuid: device.community!.uuid!,
|
communityUuid: device.community!.uuid!,
|
||||||
spaceUuid: device.spaces!.first.uuid!,
|
spaceUuid: device.spaces!.first.uuid!,
|
||||||
subSpaces: subSpaces,
|
subSpaces: subSpaces,
|
||||||
selected: device.subspace!.uuid,
|
selected: deviceInfo.subspace.uuid,
|
||||||
);
|
);
|
||||||
|
if (selectedSubSpace != null) {
|
||||||
|
Future.delayed(const Duration(milliseconds: 500), () {
|
||||||
|
context.read<SettingDeviceBloc>().add(
|
||||||
|
SettingBlocAssignRoom(
|
||||||
|
communityUuid: device.community!.uuid!,
|
||||||
|
spaceUuid: device.spaces!.first.uuid!,
|
||||||
|
subSpaceUuid: selectedSubSpace.id ?? '',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
child: infoRow(
|
child: infoRow(
|
||||||
label: 'Sub-Space:',
|
label: 'Sub-Space:',
|
||||||
|
@ -9,13 +9,11 @@ import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
|||||||
class SubSpaceDialog extends StatefulWidget {
|
class SubSpaceDialog extends StatefulWidget {
|
||||||
final List<SubSpaceModel> subSpaces;
|
final List<SubSpaceModel> subSpaces;
|
||||||
final String? selected;
|
final String? selected;
|
||||||
final void Function(SubSpaceModel?) onConfirmed;
|
|
||||||
|
|
||||||
const SubSpaceDialog({
|
const SubSpaceDialog({
|
||||||
Key? key,
|
Key? key,
|
||||||
required this.subSpaces,
|
required this.subSpaces,
|
||||||
this.selected,
|
this.selected,
|
||||||
required this.onConfirmed,
|
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -25,6 +23,17 @@ class SubSpaceDialog extends StatefulWidget {
|
|||||||
class _SubSpaceDialogState extends State<SubSpaceDialog> {
|
class _SubSpaceDialogState extends State<SubSpaceDialog> {
|
||||||
String? _selectedId;
|
String? _selectedId;
|
||||||
|
|
||||||
|
// @override
|
||||||
|
// void didUpdateWidget(SubSpaceDialog oldWidget) {
|
||||||
|
// super.didUpdateWidget(oldWidget);
|
||||||
|
// // Update internal state when external value changes
|
||||||
|
// if (widget.selected != _selectedId) {
|
||||||
|
// setState(() {
|
||||||
|
// _selectedId = widget.selected;
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
@ -86,30 +95,21 @@ class _SubSpaceDialogState extends State<SubSpaceDialog> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void showSubSpaceDialog(
|
Future<SubSpaceModel?> showSubSpaceDialog(
|
||||||
BuildContext context, {
|
BuildContext context, {
|
||||||
required List<SubSpaceModel> subSpaces,
|
required List<SubSpaceModel> subSpaces,
|
||||||
String? selected,
|
String? selected,
|
||||||
required String communityUuid,
|
required String communityUuid,
|
||||||
required String spaceUuid,
|
required String spaceUuid,
|
||||||
}) {
|
}) {
|
||||||
showDialog(
|
return showDialog<SubSpaceModel>(
|
||||||
context: context,
|
context: context,
|
||||||
barrierDismissible: true,
|
builder: (ctx) => BlocProvider.value(
|
||||||
builder: (ctx) => SubSpaceDialog(
|
value: BlocProvider.of<SettingDeviceBloc>(context),
|
||||||
subSpaces: subSpaces,
|
child: SubSpaceDialog(
|
||||||
selected: selected,
|
subSpaces: subSpaces,
|
||||||
onConfirmed: (selectedModel) {
|
selected: selected,
|
||||||
if (selectedModel != null) {
|
),
|
||||||
context.read<SettingDeviceBloc>().add(
|
|
||||||
SettingBlocAssignRoom(
|
|
||||||
communityUuid: communityUuid,
|
|
||||||
spaceUuid: spaceUuid,
|
|
||||||
subSpaceUuid: selectedModel.id ?? '',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
||||||
import 'package:syncrow_web/pages/device_managment/device_setting/bloc/setting_bloc_bloc.dart';
|
|
||||||
import 'package:syncrow_web/pages/device_managment/device_setting/settings_model/sub_space_model.dart';
|
import 'package:syncrow_web/pages/device_managment/device_setting/settings_model/sub_space_model.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/device_setting/sub_space_dialog.dart';
|
import 'package:syncrow_web/pages/device_managment/device_setting/sub_space_dialog.dart';
|
||||||
import 'package:syncrow_web/utils/color_manager.dart';
|
import 'package:syncrow_web/utils/color_manager.dart';
|
||||||
@ -62,11 +60,12 @@ class SubSpaceDialogButtons extends StatelessWidget {
|
|||||||
? null
|
? null
|
||||||
: () {
|
: () {
|
||||||
final selectedModel = widget.subSpaces.firstWhere(
|
final selectedModel = widget.subSpaces.firstWhere(
|
||||||
(space) => space.id == _selectedId,
|
(space) => space.id == _selectedId,
|
||||||
orElse: () =>
|
orElse: () =>
|
||||||
SubSpaceModel(id: null, name: '', devices: []));
|
SubSpaceModel(id: null, name: '', devices: []),
|
||||||
widget.onConfirmed(selectedModel);
|
);
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context)
|
||||||
|
.pop(selectedModel);
|
||||||
},
|
},
|
||||||
child: Text(
|
child: Text(
|
||||||
'Confirm',
|
'Confirm',
|
||||||
@ -84,31 +83,3 @@ class SubSpaceDialogButtons extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void showSubSpaceDialog(
|
|
||||||
BuildContext context, {
|
|
||||||
required List<SubSpaceModel> subSpaces,
|
|
||||||
String? selected,
|
|
||||||
required String communityUuid,
|
|
||||||
required String spaceUuid,
|
|
||||||
}) {
|
|
||||||
showDialog(
|
|
||||||
context: context,
|
|
||||||
barrierDismissible: true,
|
|
||||||
builder: (ctx) => SubSpaceDialog(
|
|
||||||
subSpaces: subSpaces,
|
|
||||||
selected: selected,
|
|
||||||
onConfirmed: (selectedModel) {
|
|
||||||
if (selectedModel != null) {
|
|
||||||
context.read<SettingDeviceBloc>().add(
|
|
||||||
SettingBlocAssignRoom(
|
|
||||||
communityUuid: communityUuid,
|
|
||||||
spaceUuid: spaceUuid,
|
|
||||||
subSpaceUuid: selectedModel.id ?? '',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user