diff --git a/lib/ui/widgets/banner_widget.dart b/lib/ui/widgets/banner_widget.dart index 213d75a..70e753e 100644 --- a/lib/ui/widgets/banner_widget.dart +++ b/lib/ui/widgets/banner_widget.dart @@ -1,5 +1,8 @@ +import 'dart:async'; + import 'package:comunic/helpers/server_config_helper.dart'; import 'package:comunic/models/server_config.dart'; +import 'package:comunic/utils/date_utils.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -14,17 +17,37 @@ class BannerWidget extends StatefulWidget { } class _BannerWidgetState extends State { + Timer _timer; + + bool get _shouldShowBanner => showBanner && !_bannerDismissed; + void _hideBanner() { - setState(() => _bannerDismissed = true); + if (this.mounted) setState(() => _bannerDismissed = true); } void _openLink() { launch(srvConfig.banner.link); } + @override + void initState() { + super.initState(); + + if (_shouldShowBanner && srvConfig.banner.expire != null) { + _timer = Timer( + Duration(seconds: srvConfig.banner.expire - time()), _hideBanner); + } + } + + @override + void dispose() { + if (_timer != null) _timer.cancel(); + super.dispose(); + } + @override Widget build(BuildContext context) { - if (!showBanner || _bannerDismissed) return Container(); + if (!_shouldShowBanner) return Container(); final banner = srvConfig.banner; return Card(