1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 06:19:22 +00:00
comunicmobile/lib/utils/ui_utils.dart

296 lines
8.3 KiB
Dart
Raw Normal View History

2019-11-01 13:17:46 +00:00
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/main.dart';
2021-04-30 16:37:03 +00:00
import 'package:comunic/models/config.dart';
2020-04-25 13:36:07 +00:00
import 'package:comunic/ui/routes/full_screen_image.dart';
2020-05-02 16:15:55 +00:00
import 'package:comunic/ui/widgets/dialogs/auto_sized_dialog_content_widget.dart';
2021-04-06 16:04:16 +00:00
import 'package:comunic/ui/widgets/dialogs/cancel_dialog_button.dart';
2019-05-04 08:24:38 +00:00
import 'package:comunic/utils/intl_utils.dart';
2019-04-22 17:16:26 +00:00
import 'package:flutter/material.dart';
2020-05-17 12:26:52 +00:00
import 'package:flutter_emoji/flutter_emoji.dart';
2019-06-24 08:48:31 +00:00
import 'package:html/parser.dart';
2019-04-22 17:16:26 +00:00
/// User interface utilities
2019-05-04 08:24:38 +00:00
///
/// @author Pierre HUBERT
2019-04-22 17:16:26 +00:00
2019-04-23 06:46:50 +00:00
/// Build centered progress bar
Widget buildCenteredProgressBar() {
return Center(
child: CircularProgressIndicator(),
);
}
2019-04-22 17:16:26 +00:00
/// Build and return a full loading page
2019-06-15 06:16:47 +00:00
Widget buildLoadingPage({
bool showAppBar = false,
String? routeTitle,
2019-06-15 06:16:47 +00:00
}) {
2019-04-22 17:16:26 +00:00
return Scaffold(
2019-06-15 06:16:47 +00:00
appBar: showAppBar
? AppBar(
2019-06-15 14:01:58 +00:00
title: routeTitle == null ? null : Text(routeTitle),
2019-06-15 06:16:47 +00:00
)
: null,
2019-04-23 06:46:50 +00:00
body: buildCenteredProgressBar(),
2019-04-22 17:16:26 +00:00
);
}
/// Build and return an error card
Widget buildErrorCard(String? message,
{List<Widget>? actions, bool hide = false}) {
2020-04-16 07:29:37 +00:00
if (hide) return Container();
2020-04-15 10:04:19 +00:00
2020-05-09 07:44:41 +00:00
return Theme(
2021-02-07 16:09:08 +00:00
data: ThemeData(
textTheme: TextTheme(bodyText2: TextStyle(color: Colors.white))),
2020-05-09 07:44:41 +00:00
child: Card(
elevation: 2.0,
color: Colors.red,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Icon(
Icons.error,
color: Colors.white,
),
2019-04-22 17:16:26 +00:00
),
2020-05-09 07:44:41 +00:00
Flexible(
child: Text(
message!,
2020-05-09 07:44:41 +00:00
maxLines: null,
),
2019-04-22 17:16:26 +00:00
),
2020-05-09 07:44:41 +00:00
Row(
children: actions == null ? <Widget>[] : actions,
)
],
),
2019-04-22 17:16:26 +00:00
),
),
);
}
2019-04-26 09:34:24 +00:00
/// Show an image with a given [url] in full screen
2022-03-12 09:57:11 +00:00
void showImageFullScreen(BuildContext context, String? url,
{String? cacheKey}) {
2019-04-26 09:34:24 +00:00
Navigator.of(context).push(MaterialPageRoute(builder: (c) {
2022-03-12 09:57:11 +00:00
return FullScreenImageRoute(
url: url!,
cacheKey: cacheKey,
);
2019-04-26 09:34:24 +00:00
}));
}
/// Show simple snack
void showSimpleSnack(BuildContext context, String message) {
2021-03-13 14:14:54 +00:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
2019-05-04 08:24:38 +00:00
}
2021-03-12 19:52:26 +00:00
void snack(BuildContext context, String message) {
2021-03-13 14:14:54 +00:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
2021-03-12 19:52:26 +00:00
}
2019-05-04 08:24:38 +00:00
/// Show an alert dialog to ask the user to enter a string
2019-05-18 16:48:12 +00:00
///
/// Returns entered string if the dialog is confirmed, null else
Future<String?> askUserString({
required BuildContext context,
required String title,
required String message,
required String defaultValue,
required String hint,
2020-05-02 16:15:55 +00:00
int maxLength = 200,
2021-03-12 15:37:21 +00:00
int minLength = 1,
2019-05-04 08:24:38 +00:00
}) async {
TextEditingController controller = TextEditingController(text: defaultValue);
final confirm = await showDialog<bool>(
context: context,
2021-03-12 15:37:21 +00:00
builder: (c) => _InputTextDialog(
title: title,
message: message,
controller: controller,
maxLength: maxLength,
minLength: minLength,
hint: hint,
2019-05-04 08:24:38 +00:00
));
if (confirm == null || !confirm) return null;
return controller.text;
}
2019-05-18 07:45:15 +00:00
2021-03-12 15:37:21 +00:00
class _InputTextDialog extends StatefulWidget {
final String title;
final String message;
final TextEditingController controller;
final int maxLength;
final int minLength;
final String hint;
const _InputTextDialog({
Key? key,
required this.title,
required this.message,
required this.controller,
required this.maxLength,
required this.minLength,
required this.hint,
2021-03-12 15:37:21 +00:00
}) : super(key: key);
@override
__InputTextDialogState createState() => __InputTextDialogState();
}
class __InputTextDialogState extends State<_InputTextDialog> {
@override
Widget build(BuildContext c) => AlertDialog(
title: Text(widget.title),
content: AutoSizeDialogContentWidget(
child: Column(
children: <Widget>[
Text(widget.message),
TextField(
controller: widget.controller,
maxLines: null,
maxLength: widget.maxLength,
keyboardType: TextInputType.text,
onChanged: (s) => setState(() {}),
decoration: InputDecoration(
labelText: widget.hint,
alignLabelWithHint: true,
),
)
],
),
),
actions: <Widget>[
2021-03-13 14:38:43 +00:00
TextButton(
child: Text(tr("Cancel")!.toUpperCase()),
2021-03-12 15:37:21 +00:00
onPressed: () => Navigator.pop(c, false),
),
2021-03-13 14:38:43 +00:00
TextButton(
child: Text(tr("OK")!),
2021-03-12 15:37:21 +00:00
onPressed: widget.controller.text.length >= widget.minLength
? () => Navigator.pop(c, true)
: null,
),
],
);
}
2019-05-18 07:45:15 +00:00
/// Show an alert dialog to get user confirmation for something
///
/// Return value of this function is never null
2019-05-18 14:48:19 +00:00
Future<bool> showConfirmDialog({
required BuildContext context,
String? title,
required String? message,
2019-05-18 07:45:15 +00:00
}) async {
if (title == null) title = tr("Confirm operation");
2020-05-06 15:53:27 +00:00
// Avoid potential theme issues
2021-03-13 14:14:54 +00:00
final scaffold = Scaffold.maybeOf(context);
2020-05-10 16:32:14 +00:00
final ctx = scaffold != null ? scaffold.context : context;
2020-05-06 15:53:27 +00:00
2019-05-18 07:45:15 +00:00
final result = await showDialog<bool>(
2020-05-06 15:53:27 +00:00
context: ctx,
2019-05-18 07:45:15 +00:00
builder: (c) => AlertDialog(
title: Text(title!),
content: Text(message!),
2019-05-18 07:45:15 +00:00
actions: <Widget>[
2021-03-13 14:38:43 +00:00
TextButton(
2019-05-18 07:45:15 +00:00
onPressed: () => Navigator.pop(context, false),
child: Text(tr("Cancel")!.toUpperCase()),
2019-05-18 07:45:15 +00:00
),
2021-03-13 14:38:43 +00:00
TextButton(
2019-05-18 07:45:15 +00:00
onPressed: () => Navigator.pop(context, true),
child: Text(
tr("Confirm")!.toUpperCase(),
2019-05-18 07:45:15 +00:00
style: TextStyle(color: Colors.red),
),
),
],
));
return result != null && result;
}
2019-05-20 07:20:11 +00:00
2021-04-06 16:04:16 +00:00
/// Show a simple alert dialog
Future<void> showAlert({
required BuildContext context,
required String? message,
String? title,
2021-04-06 16:04:16 +00:00
}) async =>
await showDialog(
context: context,
builder: (c) => AlertDialog(
title: title == null ? null : Text(title),
content: Text(message!),
2021-04-06 16:04:16 +00:00
actions: [CancelDialogButton()],
));
2019-05-20 07:20:11 +00:00
/// Smart [InputCounterWidgetBuilder] that show text limit only when some
/// text has already been entered by the user
Widget smartInputCounterWidgetBuilder(
BuildContext context, {
required int currentLength,
required int? maxLength,
required bool isFocused,
2019-05-20 07:20:11 +00:00
}) =>
currentLength > 0 ? Text("$currentLength/$maxLength") : Container();
2019-06-24 08:48:31 +00:00
/// Parse an HTML String to decode special characters
String htmlDecodeCharacters(String? input) {
2019-06-24 18:24:40 +00:00
if (input == null || input == "") return "";
return parse(input).documentElement!.text;
2019-06-24 08:48:31 +00:00
}
2019-11-01 13:17:46 +00:00
const darkAccentColor = Colors.white70;
2019-11-02 11:48:47 +00:00
const darkerAccentColor = Colors.white30;
2019-11-01 13:17:46 +00:00
/// Check out whether dark theme is enabled or not
2022-03-11 15:36:42 +00:00
bool darkTheme() =>
preferences()!.getBool(PreferencesKeyList.ENABLE_DARK_THEME);
2020-04-16 07:29:37 +00:00
2020-05-05 11:21:37 +00:00
/// Check out whether we use tablet mode or not
bool isTablet(BuildContext context) =>
!preferences()!.getBool(PreferencesKeyList.FORCE_MOBILE_MODE) &&
2020-05-05 11:21:37 +00:00
MediaQuery.of(context).size.width >= 1024;
2020-04-16 07:29:37 +00:00
/// Show about Comunic dialog
void showAboutAppDialog(BuildContext context) {
showAboutDialog(
context: context,
2021-04-30 16:37:03 +00:00
applicationName: config().appName,
2020-04-16 07:29:37 +00:00
children: <Widget>[
Text(
2021-04-30 16:38:26 +00:00
tr(config().appQuickDescription) ??
tr("Comunic is a free and OpenSource social network that respect your privacy.")!,
2020-04-16 07:29:37 +00:00
textAlign: TextAlign.center,
),
SizedBox(
height: 20,
),
Text(
"Application built by Pierre Hubert",
textAlign: TextAlign.center,
),
]);
}
/// Apply new theme settings
void applyNewThemeSettings(BuildContext context) =>
context.findAncestorStateOfType<ComunicApplicationState>()!.refresh();
2020-05-17 12:26:52 +00:00
/// Parse emojies
String parseEmojies(String input) => EmojiParser().emojify(input);
2021-03-16 16:41:14 +00:00
/// Create a white text style for dart heme and a black text otherwise
TextStyle blackForWhiteTheme() =>
TextStyle(color: darkTheme() ? Colors.white : Colors.black);