showSwitchAudioDialog function

Future<void> showSwitchAudioDialog(
  1. BuildContext context,
  2. EnxController obj
)

Implementation

Future<void> showSwitchAudioDialog(BuildContext context, EnxController obj) async {
  try {
    await showModalBottomSheet(
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      isDismissible: true, // 👈 this enables tapping outside to dismiss
      enableDrag: true,    // 👈 this allows swipe-down to dismiss
      context: context,
      builder: (BuildContext context) {
        return LayoutBuilder(
          builder: (context, constraints) {
            final mediaQuery = MediaQuery.of(context);
            final isLandscape = mediaQuery.orientation == Orientation.landscape;

            final sheetWidth = isLandscape
                ? mediaQuery.size.width * 0.5
                : mediaQuery.size.width;

            return Obx(() => Padding(
              padding: EdgeInsets.only(
                bottom: mediaQuery.viewInsets.bottom + 120,
                left: 10,
                right: 10,
              ),
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                  width: sheetWidth,
                  constraints: BoxConstraints(
                    // Let content define height but prevent overflow
                    maxHeight: mediaQuery.size.height * (isLandscape ? 0.7 : 0.5),
                  ),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border.all(width: 1.0),
                    borderRadius: BorderRadius.circular(15.0),
                  ),
                  child: RadioGroup<MediaDeviceModel>(
                    groupValue: obj.selectedMediaDeviceModel.value,
                    onChanged: (selected) {
                      if (selected != null) {
                        obj.selectedMediaDeviceModel.value = selected;
                        obj.selectedDevice.value = selected.name;
                        EnxRtc.switchMediaDevice(selected.name);
                        Get.back(); // Close modal after selection
                      }
                    },
                    child: ListView.builder(
                      shrinkWrap: true,
                      itemCount: obj.mediaDeviceList.length,
                      itemBuilder: (_, index) {
                        final item = obj.mediaDeviceList[index];
                        return RadioListTile<MediaDeviceModel>(
                          title: Text(
                            item.name,
                            style: const TextStyle(
                              color: Colors.black,
                              fontWeight: FontWeight.w800,
                              fontSize: 16,
                            ),
                          ),
                          value: item,
                          activeColor: Colors.red,
                          selected: obj.selectedMediaDeviceModel.value == item,
                        );
                      },
                    ),
                  ),
                ),
              ),
            ));
          },
        );
      },
    );
  } catch (e) {
    // Handle errors if needed
  }
}