2021-12-30 11:22:10 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2021-12-30 11:02:18 +00:00
|
|
|
import 'package:comunic/helpers/server_config_helper.dart';
|
|
|
|
import 'package:comunic/models/server_config.dart';
|
2021-12-30 11:22:10 +00:00
|
|
|
import 'package:comunic/utils/date_utils.dart';
|
2021-12-30 11:02:18 +00:00
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2022-06-11 13:04:11 +00:00
|
|
|
import 'package:url_launcher/url_launcher_string.dart';
|
2021-12-30 11:02:18 +00:00
|
|
|
|
|
|
|
bool _bannerDismissed = false;
|
|
|
|
|
|
|
|
class BannerWidget extends StatefulWidget {
|
2022-03-10 18:39:57 +00:00
|
|
|
const BannerWidget({Key? key}) : super(key: key);
|
2021-12-30 11:02:18 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_BannerWidgetState createState() => _BannerWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BannerWidgetState extends State<BannerWidget> {
|
2022-03-10 18:39:57 +00:00
|
|
|
Timer? _timer;
|
2021-12-30 11:22:10 +00:00
|
|
|
|
|
|
|
bool get _shouldShowBanner => showBanner && !_bannerDismissed;
|
|
|
|
|
2021-12-30 11:02:18 +00:00
|
|
|
void _hideBanner() {
|
2021-12-30 11:22:10 +00:00
|
|
|
if (this.mounted) setState(() => _bannerDismissed = true);
|
2021-12-30 11:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _openLink() {
|
2022-06-11 13:04:11 +00:00
|
|
|
launchUrlString(srvConfig!.banner!.link!);
|
2021-12-30 11:02:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 11:22:10 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
if (_shouldShowBanner && srvConfig!.banner!.expire != null) {
|
2021-12-30 11:22:10 +00:00
|
|
|
_timer = Timer(
|
2022-03-10 18:39:57 +00:00
|
|
|
Duration(seconds: srvConfig!.banner!.expire! - time()), _hideBanner);
|
2021-12-30 11:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2022-03-10 18:39:57 +00:00
|
|
|
if (_timer != null) _timer!.cancel();
|
2021-12-30 11:22:10 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2021-12-30 11:02:18 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-12-30 11:22:10 +00:00
|
|
|
if (!_shouldShowBanner) return Container();
|
2021-12-30 11:02:18 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
final banner = srvConfig!.banner!;
|
2021-12-30 11:02:18 +00:00
|
|
|
return Card(
|
|
|
|
color: banner.nature == BannerNature.Information
|
|
|
|
? Colors.blue
|
|
|
|
: banner.nature == BannerNature.Success
|
|
|
|
? Colors.green
|
|
|
|
: Colors.red,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(2.0),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
BannerButton(
|
|
|
|
icon: Icon(
|
|
|
|
banner.nature == BannerNature.Information
|
|
|
|
? Icons.info
|
|
|
|
: banner.nature == BannerNature.Success
|
|
|
|
? Icons.check
|
|
|
|
: Icons.warning,
|
|
|
|
),
|
|
|
|
),
|
2021-12-30 12:36:12 +00:00
|
|
|
Expanded(
|
2021-12-30 11:02:18 +00:00
|
|
|
child: Text(
|
2021-12-30 11:11:26 +00:00
|
|
|
banner.message.containsKey(shortLang)
|
2022-03-10 18:39:57 +00:00
|
|
|
? banner.message[shortLang]!
|
|
|
|
: banner.message["en"]!,
|
2021-12-30 11:02:18 +00:00
|
|
|
style: TextStyle(color: Colors.white),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
banner.link == null
|
|
|
|
? Container()
|
|
|
|
: BannerButton(
|
|
|
|
onPressed: _openLink,
|
|
|
|
icon: Icon(Icons.open_in_new),
|
|
|
|
),
|
|
|
|
BannerButton(
|
|
|
|
onPressed: _hideBanner,
|
|
|
|
icon: Icon(Icons.close),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BannerButton extends StatelessWidget {
|
2022-03-10 18:39:57 +00:00
|
|
|
final Function()? onPressed;
|
|
|
|
final Widget? icon;
|
2021-12-30 11:02:18 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
const BannerButton({this.onPressed, this.icon, Key? key}) : super(key: key);
|
2021-12-30 11:02:18 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return IconButton(
|
|
|
|
onPressed: onPressed,
|
2022-03-10 18:39:57 +00:00
|
|
|
icon: icon!,
|
2021-12-30 12:36:12 +00:00
|
|
|
color: Colors.white,
|
2021-12-30 11:02:18 +00:00
|
|
|
disabledColor: Colors.white,
|
|
|
|
padding: EdgeInsets.all(1.0),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|