1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +00:00
comunicmobile/lib/ui/widgets/banner_widget.dart

114 lines
2.9 KiB
Dart
Raw Normal View History

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 {
const BannerWidget({Key? key}) : super(key: key);
2021-12-30 11:02:18 +00:00
@override
_BannerWidgetState createState() => _BannerWidgetState();
}
class _BannerWidgetState extends State<BannerWidget> {
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();
if (_shouldShowBanner && srvConfig!.banner!.expire != null) {
2021-12-30 11:22:10 +00:00
_timer = Timer(
Duration(seconds: srvConfig!.banner!.expire! - time()), _hideBanner);
2021-12-30 11:22:10 +00:00
}
}
@override
void dispose() {
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
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)
? 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 {
final Function()? onPressed;
final Widget? icon;
2021-12-30 11:02:18 +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,
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),
);
}
}