fix(ui): display selected device name when add devices in DetailGroupScreen

This commit is contained in:
anhtunz
2025-02-11 11:55:51 +07:00
parent a98e84880b
commit bfeba03490
6 changed files with 124 additions and 98 deletions

View File

@@ -21,8 +21,11 @@ addDeviceDialog(BuildContext context, DetailGroupBloc detailGroupBloc,
showDialog( showDialog(
context: context, context: context,
builder: (dialogContext) { builder: (dialogContext) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog( return AlertDialog(
title: Center(child: Text(appLocalization(context).add_device_title)), title:
Center(child: Text(appLocalization(context).add_device_title)),
content: DropdownButtonFormField2<String>( content: DropdownButtonFormField2<String>(
isExpanded: true, isExpanded: true,
decoration: InputDecoration( decoration: InputDecoration(
@@ -36,24 +39,30 @@ addDeviceDialog(BuildContext context, DetailGroupBloc detailGroupBloc,
style: const TextStyle(fontSize: 14), style: const TextStyle(fontSize: 14),
), ),
items: ownerDevices items: ownerDevices
.map((item) => DropdownMenuItem<String>( .map(
(item) => DropdownMenuItem<String>(
value: item.thingId, value: item.thingId,
child: StatefulBuilder(builder: (context, menuSetState) { enabled: false,
final isSelected = selectedItems.contains(item.thingId); child: StatefulBuilder(
final isNameSelected = selectedItems.contains(item.name); builder: (context, menuSetState) {
final isSelected =
selectedItems.contains(item.thingId);
return InkWell( return InkWell(
onTap: () { onTap: () {
isSelected if (isSelected) {
? selectedItems.remove(item.thingId) selectedItems.remove(item.thingId);
: selectedItems.add(item.thingId!); } else {
isNameSelected selectedItems.add(item.thingId!);
? selectedDevices.remove(item.name) }
: selectedDevices.add(item.name!); selectedDevices =
getDevices(ownerDevices, selectedItems);
setState(() {});
menuSetState(() {}); menuSetState(() {});
}, },
child: SizedBox( child: Container(
height: double.infinity, height: double.infinity,
padding:
const EdgeInsets.symmetric(horizontal: 16.0),
child: Row( child: Row(
children: [ children: [
if (isSelected) if (isSelected)
@@ -65,28 +74,29 @@ addDeviceDialog(BuildContext context, DetailGroupBloc detailGroupBloc,
child: Text( child: Text(
item.name!, item.name!,
style: const TextStyle(fontSize: 14), style: const TextStyle(fontSize: 14),
)) ),
),
], ],
), ),
), ),
); );
}))) },
),
),
)
.toList(), .toList(),
value: selectedItems.isEmpty ? null : selectedItems.last,
buttonStyleData: const ButtonStyleData( buttonStyleData: const ButtonStyleData(
padding: EdgeInsets.only(right: 8), padding: EdgeInsets.only(right: 8),
), ),
onChanged: (value) { onChanged: (value) {},
// thingID = value!;
// setState(() {});
},
onSaved: (value) {}, onSaved: (value) {},
selectedItemBuilder: (context) { selectedItemBuilder: (context) {
return selectedDevices.map( return ownerDevices.map((device) {
(item) {
return Container( return Container(
alignment: AlignmentDirectional.center, alignment: AlignmentDirectional.center,
child: Text( child: Text(
item, selectedDevices.join(', '),
style: const TextStyle( style: const TextStyle(
fontSize: 14, fontSize: 14,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
@@ -94,8 +104,7 @@ addDeviceDialog(BuildContext context, DetailGroupBloc detailGroupBloc,
maxLines: 1, maxLines: 1,
), ),
); );
}, }).toList();
).toList();
}, },
iconStyleData: IconStyleData( iconStyleData: IconStyleData(
icon: IconConstants.instance icon: IconConstants.instance
@@ -105,9 +114,6 @@ addDeviceDialog(BuildContext context, DetailGroupBloc detailGroupBloc,
borderRadius: BorderRadius.circular(15), borderRadius: BorderRadius.circular(15),
), ),
), ),
menuItemStyleData: const MenuItemStyleData(
padding: EdgeInsets.symmetric(horizontal: 16),
),
), ),
actions: <Widget>[ actions: <Widget>[
TextButton( TextButton(
@@ -126,9 +132,22 @@ addDeviceDialog(BuildContext context, DetailGroupBloc detailGroupBloc,
context, groupID, device); context, groupID, device);
await detailGroupBloc.getGroupDetail(groupID); await detailGroupBloc.getGroupDetail(groupID);
} }
Navigator.of(dialogContext).pop();
}, },
child: Text(appLocalization(context).add_device_title)) child: Text(appLocalization(context).add_device_title),
),
], ],
); );
}); },
);
},
);
}
List<String> getDevices(List<Device> devices, List<String> selectedDevices) {
return devices
.where((device) => selectedDevices.contains(device.thingId))
.map((device) => device.name ?? '')
.toList();
} }

View File

@@ -62,8 +62,10 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
} }
if (theme == AppThemes.LIGHT.name) { if (theme == AppThemes.LIGHT.name) {
isLight = true; isLight = true;
} else { } else if (theme == AppThemes.DARK.name) {
isLight = false; isLight = false;
} else {
isLight = true;
} }
mainBloc.sinkIsVNIcon.add(isVN); mainBloc.sinkIsVNIcon.add(isVN);
mainBloc.sinkThemeMode.add(isLight); mainBloc.sinkThemeMode.add(isLight);
@@ -325,7 +327,7 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
controller: controller, controller: controller,
screens: _buildScreens(), screens: _buildScreens(),
items: _navBarsItems(), items: _navBarsItems(),
handleAndroidBackButtonPress: true, handleAndroidBackButtonPress: false,
resizeToAvoidBottomInset: true, resizeToAvoidBottomInset: true,
stateManagement: true, stateManagement: true,
backgroundColor: backgroundColor:

View File

@@ -246,8 +246,6 @@ class _MapScreenState extends State<MapScreen> with WidgetsBindingObserver {
} }
} }
// log("Has state = 1: $hasStateOne, Has other state: $hasOtherState");
if (hasStateOne) { if (hasStateOne) {
return true; // flameIcon return true; // flameIcon
} else if (hasOtherState) { } else if (hasOtherState) {

View File

@@ -23,4 +23,6 @@ class ApplicationConstants {
static const DEVICE_NOTIFICATIONS_SETTINGS = "/device-notifications-settings"; static const DEVICE_NOTIFICATIONS_SETTINGS = "/device-notifications-settings";
static const OWNER_GROUP = "owner"; static const OWNER_GROUP = "owner";
static const PARTICIPANT_GROUP = "participant"; static const PARTICIPANT_GROUP = "participant";
static const NO_DATA = "no_data";
static const LOADING = "loading";
} }

View File

@@ -136,9 +136,12 @@ class DeviceUtils {
List<Device> sortDeviceByState(List<Device> devices) { List<Device> sortDeviceByState(List<Device> devices) {
List<Device> sortedDevices = List.from(devices); List<Device> sortedDevices = List.from(devices);
sortedDevices.sort((a, b) { sortedDevices.sort((a, b) {
int stateOrder = [2, 1, 3, 0, -1, 3].indexOf(a.state!) - int stateOrder = [2, 1, 3, 0, -1].indexOf(a.state!) -
[2, 1, 3, 0, -1, 3].indexOf(b.state!); [2, 1, 3, 0, -1].indexOf(b.state!);
if (stateOrder != 0) {
return stateOrder; return stateOrder;
}
return a.name!.compareTo(b.name!);
}); });
return sortedDevices; return sortedDevices;

View File

@@ -21,3 +21,5 @@ void showSnackBarResponseByStatusCodeNoIcon(BuildContext context, int statusCode
showNoIconTopSnackBar(context, failedMessage, Colors.red, Colors.white); showNoIconTopSnackBar(context, failedMessage, Colors.red, Colors.white);
} }
} }