0

So I'm trying to pick a date with a DatePicker package from inside a showDialog using Bloc.

Here's the code that shows the Dialog:

onPressed: () {
   showDialog(
     context: context,
     barrierDismissible: true,
     child: _buildEditDialog(context, arguments),
   );
},

And here's the Dialog's content:

void _openCalendarPicker(BuildContext context, SearchHotelArguments arguments) async {
    final DateTime dateTimeNow = DateTime.now();
    final List<DateTime> picked = await DateRagePicker.showDatePicker(
        context: context,
        initialFirstDate: arguments.checkInDate ?? dateTimeNow,
        initialLastDate: arguments.checkOutDate ?? dateTimeNow.add(Duration(days: 1)),
        firstDate: dateTimeNow,
        lastDate: dateTimeNow.add(Duration(days: 365 * 10)));
    if (picked != null && picked.length == 2) {
      context.read<HotelChangeParamsBloc>().setCheckInDate(picked[0]);
      context.read<HotelChangeParamsBloc>().setCheckOutDate(picked[1]);
    }
}

Now, the issue here is that from a Dialog I open a popup with the DateRangePicker, pick the Date and submit it but on the Dialog the date stays the same as it was previously. And if I close and re-open the Dialog I can see that there was a change in the date. So the Dialog is not refreshing the data by itself(unless I re-open it).

Does anyone know how I can refresh the Dialog with the new Date from the DateRangePicker?

João Martins
  • 706
  • 1
  • 8
  • 20

1 Answers1

0

Edit: If you're interested in adhering to the intended Bloc pattern, you wouldn't be firing a regular function from your dialog. That's why I said 'assuming you're emitting a new state'.

Ideally it would be

context.read<HotelChangeParamsBloc>().add(UserSelectedDateEvent(setCheckInDate(picked[0])));

In that example UserSelectedDateEvent is an event that passes in a DateTime object that gets emitted to an updated state.

That event would hit the mapEventToState method in your Bloc, emit a new state, and if you wrap your first dialog in a BlocBuilder as mentioned below, it will show the updated date

Original answer:

Assuming that your setCheckInDate() method is emitting a new state in your HotelChangeParamsBloc, you just need to wrap your first dialog in

BlocBuilder<HotelChangeParamsBloc, HotelChangeParamsBlocState>

Then within that, display the updated date with state.yourBlocVariable.toString()

Without the BlocBuilder theres nothing telling it to rebuild so it won't show the updated state until you close and rebuild it.

Loren.A
  • 4,872
  • 1
  • 10
  • 17
  • So, even if I have a BlocConsumer on the `build` override method I must have the BlocBuilder anyways? – João Martins Feb 22 '21 at 06:48
  • If you mean you have a `BlocConsumer ` on whatever page you're building, then yes. The page doesn't build the dialog. `showDialog` does, and it has its own build method that does not share context with where ever it's being called from. So in your case if you want that first dialog to update in the way you describe, it needs its own `BlocBuilder` or `BlocConsumer`. I tested it before I posted that answer a few days ago, it works. – Loren.A Feb 22 '21 at 15:04
  • @Loren.A Could you provide a working example of wrapping the dialog in BlocBuilder? I tried this and am receiving a "Could not find the correct Provider above this BlocBuilder" error. – most200 Jun 02 '22 at 13:01
  • @most200 you should start your own question for that with your code posted, and paste the link here and I'll have a look. – Loren.A Jun 02 '22 at 16:03