103 lines
3.6 KiB
Dart
103 lines
3.6 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../ui/adaptive/host.dart';
|
|
import '../notify/notification_models.dart';
|
|
|
|
/// The permission tile's root, so a caller can find it whichever host draws it.
|
|
const notificationTileKey = ValueKey<String>('notification-permission-tile');
|
|
|
|
/// The explicit-gesture Enable action, present only when a request is possible.
|
|
const notificationEnableKey = ValueKey<String>(
|
|
'notification-permission-enable',
|
|
);
|
|
|
|
/// The honest state line, which says plainly when the in-app surface is the
|
|
/// only channel.
|
|
const notificationStateKey = ValueKey<String>('notification-permission-state');
|
|
|
|
/// The reusable Notifications permission tile for Settings.
|
|
///
|
|
/// It renders the platform's own [state] with the browser's own copy, shows the
|
|
/// Android rationale verbatim, and offers the [notificationRequestLabel] action
|
|
/// only while a request is still possible. Nothing here asks for permission on
|
|
/// its own — [onRequest] is the caller's explicit-gesture handler, and the tile
|
|
/// never guesses a grant.
|
|
///
|
|
/// The caller owns the surrounding section/card and passes the live state; the
|
|
/// tile reads no store, bridge or clock.
|
|
class NotificationPermissionTile extends StatelessWidget {
|
|
const NotificationPermissionTile({
|
|
super.key,
|
|
required this.state,
|
|
this.asked = false,
|
|
this.onRequest,
|
|
this.cupertino,
|
|
});
|
|
|
|
final NotificationPermissionState state;
|
|
|
|
/// Whether the operator has already been asked; only changes the wording.
|
|
final bool asked;
|
|
|
|
/// The explicit-gesture handler. Absent means no action is drawn even when a
|
|
/// request would be possible.
|
|
final VoidCallback? onRequest;
|
|
|
|
/// Forces the native host in tests and fixtures; otherwise the ambient
|
|
/// [usesCupertinoHost] decides.
|
|
final bool? cupertino;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final view = notificationPermissionView(state, asked: asked);
|
|
final native = cupertino ?? usesCupertinoHost(context);
|
|
final action = view.canRequest && onRequest != null
|
|
? _action(native, view.actionLabel!, onRequest!)
|
|
: null;
|
|
final bodyStyle = native
|
|
? CupertinoTheme.of(context).textTheme.textStyle.copyWith(
|
|
color: CupertinoColors.secondaryLabel.resolveFrom(context),
|
|
)
|
|
: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
);
|
|
// The Settings section supplies the `Notifications` heading and the page
|
|
// inset, as for every other section, so the body starts at that edge.
|
|
return Column(
|
|
key: notificationTileKey,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(notificationRationale, style: bodyStyle),
|
|
const SizedBox(height: 8),
|
|
Semantics(
|
|
liveRegion: true,
|
|
child: Text(view.body, key: notificationStateKey, style: bodyStyle),
|
|
),
|
|
// The action sits on its own row, as the browser's settings section
|
|
// does: its label is too long to share a row at large text.
|
|
if (action != null) ...[
|
|
const SizedBox(height: 8),
|
|
Align(alignment: AlignmentDirectional.centerStart, child: action),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _action(bool native, String label, VoidCallback onRequest) {
|
|
if (native) {
|
|
return CupertinoButton(
|
|
key: notificationEnableKey,
|
|
padding: EdgeInsets.zero,
|
|
onPressed: onRequest,
|
|
child: Text(label),
|
|
);
|
|
}
|
|
return TextButton(
|
|
key: notificationEnableKey,
|
|
onPressed: onRequest,
|
|
child: Text(label),
|
|
);
|
|
}
|
|
}
|