| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:intl/intl.dart';
- /// Black night screen with a very dim clock. Tapping anywhere returns.
- class NightModeOverlay extends StatefulWidget {
- const NightModeOverlay({super.key, required this.onDismiss});
- final VoidCallback onDismiss;
- @override
- State<NightModeOverlay> createState() => _NightModeOverlayState();
- }
- class _NightModeOverlayState extends State<NightModeOverlay> {
- late DateTime _now = DateTime.now();
- Timer? _timer;
- @override
- void initState() {
- super.initState();
- _scheduleTick();
- }
- void _scheduleTick() {
- _timer?.cancel();
- final now = DateTime.now();
- final next = DateTime(
- now.year,
- now.month,
- now.day,
- now.hour,
- now.minute,
- ).add(const Duration(minutes: 1));
- _timer = Timer(next.difference(now) + const Duration(milliseconds: 80), () {
- if (!mounted) return;
- setState(() => _now = DateTime.now());
- _scheduleTick();
- });
- }
- @override
- void dispose() {
- _timer?.cancel();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- final locale = Localizations.localeOf(context).toLanguageTag();
- final use24Hour = MediaQuery.alwaysUse24HourFormatOf(context);
- final time = (use24Hour ? DateFormat.Hm(locale) : DateFormat.jm(locale))
- .format(_now);
- return Semantics(
- button: true,
- label: 'Exit night mode',
- child: GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTap: widget.onDismiss,
- child: ColoredBox(
- color: Colors.black,
- child: Center(
- child: Text(
- time,
- maxLines: 1,
- style: TextStyle(
- color: Colors.white.withValues(alpha: 0.12),
- fontSize: 84,
- fontWeight: FontWeight.w300,
- height: 1,
- fontFeatures: const [FontFeature.tabularFigures()],
- ),
- ),
- ),
- ),
- ),
- );
- }
- }
|