Stacked-Org / stacked

A Flutter application architecture created from real world scenarios
MIT License
944 stars 255 forks source link

[bug]: SheetResponse type #970

Open shayant98 opened 1 year ago

shayant98 commented 1 year ago

Describe the bug

I would like to type the response of SheetResponse & DialogResponse. I am aware that SheetResponse accepts a generic which can be anything you want, but when typing inside the generated bottomsheet eg. SheetResponse to SheetResponse the application complains that SheetResponse is not of type SheetResponse, Which is a valid error, but is there a way to type the response (and maybe even the request)?

I am sure there is a way is t was available in previous versions of Stacked (before app.bottomsheet.dart), but i am unable to get the types in the current version.

NOTE: I did run build_runner after updating the type but in the app.bottomsheet.dart it still shows up as 'dynamic'

To reproduce

Nothing

Expected behavior

Abilty to type SheetResponses

Screenshots

No response

Additional Context

No response

ferrarafer commented 1 year ago

Hi @shayant98 , you can set the type of SheetResponse when you define the completer in your Sheet class like below.

class ShayantSheet extends StatelessWidget {
  final Function(SheetResponse<int> response)? completer;
  final SheetRequest request;
  const ShayantSheet({
    Key? key,
    required this.completer,
    required this.request,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(10),
          topRight: Radius.circular(10),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            request.title ?? 'Hello Stacked Sheet!!',
            style: const TextStyle(fontSize: 25, fontWeight: FontWeight.w900),
          ),
          if (request.description != null) ...[
            verticalSpaceTiny,
            Text(
              request.description!,
              style: const TextStyle(fontSize: 14, color: kcMediumGrey),
              maxLines: 3,
              softWrap: true,
            ),
          ],
          verticalSpaceMedium,
          MaterialButton(
            onPressed: completer?.call(SheetResponse(data: 666)),
            child: const Text('test'),
          ),
          verticalSpaceLarge,
        ],
      ),
    );
  }
}

Let me know if that is what you wanted to achieve.

shayant98 commented 1 year ago

Hey @ferrarafer

This js great!

But what i also wanted to know was how to type the SheetRequest currently the data object is typed as Dynamic but i want to supply my own Map.

FilledStacks commented 1 year ago

Hey @shayant98 SheetRequest is generic, you can make your own. Might not be supported with the generator This is how to use it:

  1. Create your class that you want to use as your request
class TimeRangeSelectionRequest {
  final List<Event> events;
  final int day;

  TimeRangeSelectionRequest({required this.events, required this.day});
}
  1. In your bottom sheet accept the request
class TimeRangeSelectionBottomSheet extends StatelessWidget {
  final SheetRequest<TimeRangeSelectionRequest> request;
  final Function(SheetResponse)? completer;

  TimeRangeSelectionBottomSheet({
    Key? key,
    required this.request,
    this.completer,
  }) : super(key: key);

  ....

}
  1. Your bottom sheet setup can then use the following code to register
  BottomSheetType.timeRangeSelection: (
            context,
            SheetRequest<dynamic> sheetRequest,
            Function(SheetResponse<dynamic>) completer) =>
        TimeRangeSelectionBottomSheet(
          request: sheetRequest as SheetRequest<TimeRangeSelectionRequest>,
          completer: completer,
        ),

@ferrarafer we can use the same pattern above and generate the registration accordingly through the generator.

shayant98 commented 4 months ago

Any idea if this will get implemented?

This will make many codebases more typesafe and reduce the need for infering types.

FilledStacks commented 4 months ago

@shayant98 I'd suggest creating a registerBottomSheetsFunction where you can register custom response types like above yourself.

Then you keep the generic ones in the bottom sheet generator.

We don't have a timeline for implementing this.

Happy to accept and review PR's for this functionality.