show method

void show(
  1. BuildContext context, {
  2. bool dismissCurrent = true,
})

Implementation

void show(BuildContext context, {bool dismissCurrent = true}) {
  final messenger = ScaffoldMessenger.maybeOf(context);
  if (messenger == null) return;
  if (dismissCurrent) messenger.removeCurrentMaterialBanner();
  final foregroundColor = color.brightness.isDark ? Colors.white : Colors.black;
  final textTheme = Theme.of(context).textTheme;
  final actions = this.actions ??
      [
        MessageAction(
            label: MaterialLocalizations.of(context).closeButtonLabel,
            onTap: () => messenger.hideCurrentMaterialBanner(reason: MaterialBannerClosedReason.dismiss))
      ];
  final banner = MaterialBanner(
      backgroundColor: color,
      leading: Icon(icon, color: foregroundColor),
      content: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text(title, style: textTheme.titleMedium!.copyWith(color: foregroundColor)),
        if (body != null) Text(body!, style: textTheme.bodySmall!.copyWith(color: foregroundColor.blend(color, .12))),
      ]),
      onVisible: () =>
          (cancel ?? Future.delayed(const Duration(seconds: 4))).whenComplete(messenger.hideCurrentMaterialBanner),
      actions: actions
          .map((action) => OutlinedButton(
              onPressed: action.onTap, child: Text(action.label, style: TextStyle(color: foregroundColor))))
          .toList());
  messenger.showMaterialBanner(banner);
}