stacked
stacked copied to clipboard
[bug]: SheetResponse type
Describe the bug
I would like to type the response of SheetResponse & DialogResponse. I am aware that SheetResponse accepts a <T> generic which can be anything you want, but when typing inside the generated bottomsheet eg. SheetResponse
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
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.
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.
Hey @shayant98 SheetRequest is generic, you can make your own. Might not be supported with the generator This is how to use it:
- 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});
}
- 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);
....
}
- 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.
Any idea if this will get implemented?
This will make many codebases more typesafe and reduce the need for infering types.
@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.