Issue
I am trying to find a way to dispose of flash bar (flash package) when Navigator.of(context).pop()
is called. When user pops current screen where flash bar is visible it disappears only after its duration time has elapsed.
I tried creating FlashController in screen that displays flash bar, and creating FlashController
callback function from showCustomFlashbar()
method where I would assign FlashController
from builder
to FlashController
from screen it is used in.
But let’s say I want to use this flash bar in 50 different screens. That’s too much copy/paste code each time. Is there a way it can be dismissed universally so that I wouldn’t have to do that in every screen.
My custom flash bar:
Future<void> showCustomFlashbar(BuildContext context,
{required String message, Duration duration}) async {
await showFlash(
context: context,
duration: const Duration(seconds: 5),
transitionDuration: const Duration(milliseconds: 400),
builder: (_, controller) {
return CustomFlashbar(
controller: controller,
message: message,
);
});
return;
}
class CustomFlashbar extends StatelessWidget {
final FlashController controller;
final String message;
const CustomFlashbar(
{Key? key, required this.controller, required this.message})
: super(key: key);
@override
Widget build(BuildContext context) {
return Flash(
controller: controller,
behavior: FlashBehavior.floating,
position: FlashPosition.top,
backgroundColor: Colors.red,
child: FlashBar(
content: Text(
message,
),
),
);
}
}
How it is used in code:
void showTestFlashbar() async {
await showCustomFlashbar(context, 'This is test flashbar');
}
Solution
Call controller.dispose;
in dispose method of stateful widget.
Reference: https://github.com/sososdk/flash/issues/7#issuecomment-619475438
Answered By – Harish
Answer Checked By – Timothy Miller (FlutterFixes Admin)