jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.08k stars 1.58k forks source link

Null safety with <Widget?>Obx function. #2951

Open SittiphanSittisak opened 9 months ago

SittiphanSittisak commented 9 months ago

Sometimes, I need to get the Widget? type but at the moment the Obx function must return a Widget type then when I want a Widget? type, I need to return an empty SizedBox widget or Wrap the Widget? with a Container widget.

For example:

  1. That is what I suggest.

    Widget example() {
    final isLoading = false.obs;
    
    const leadingWidget = Icon(Icons.download);
    const titleWidget = Text('Download file');
    const trailingWidget = Wrap(
    children: [
      Text('loading...'),
      CircularProgressIndicator(),
    ],
    );
    
    return TextFieldTapRegion(
      child: ListTile(
    leading: leadingWidget,
    title: titleWidget,
    trailing: Obx(() => isLoading.value ? trailingWidget : null), //Focus this.
    dense: true,
    minLeadingWidth: 0,
    ));
    }
  2. That is a way to do that at this moment.
    //Cover all widgets with Obx
    return Obx(() => TextFieldTapRegion(
          child: ListTile(
        leading: leadingWidget,
        title: titleWidget,
        trailing: isLoading.value ? trailingWidget : null,
        dense: true,
        minLeadingWidth: 0,
      )));
    //Using empty SizedBox
    return TextFieldTapRegion(
      child: ListTile(
    leading: leadingWidget,
    title: titleWidget,
    trailing: Obx(() => isLoading.value ? trailingWidget : const SizedBox()),
    dense: true,
    minLeadingWidth: 0,
    ));

Sometimes, the custom widget has a property with Widget? type. If the Obx function returns with an empty SizedBox widget (not null value). This custom widget will do something with this (like add space) and make this widget seem to have space.

yudaprama commented 8 months ago

Need this too. Maybe create new widget Obxn