1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/utils/ui_utils.dart

212 lines
5.7 KiB
Dart
Raw Normal View History

2019-04-26 09:34:24 +00:00
import 'package:cached_network_image/cached_network_image.dart';
2019-11-01 13:17:46 +00:00
import 'package:comunic/helpers/preferences_helper.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';
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-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
2020-04-16 07:29:37 +00:00
Widget buildErrorCard(String message,
{List<Widget> actions, bool hide = false}) {
if (hide) return Container();
2020-04-15 10:04:19 +00:00
2019-04-22 17:16:26 +00:00
return 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,
),
),
Flexible(
child: Text(
message,
style: TextStyle(color: Colors.white),
maxLines: null,
),
2019-04-23 12:35: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
void showImageFullScreen(BuildContext context, String url) {
Navigator.of(context).push(MaterialPageRoute(builder: (c) {
// TODO : add better support later
return CachedNetworkImage(
imageUrl: url,
);
}));
}
/// Show simple snack
void showSimpleSnack(BuildContext context, String message) {
Scaffold.of(context).showSnackBar(SnackBar(content: Text(message)));
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
2019-05-04 08:24:38 +00:00
Future<String> askUserString({
@required BuildContext context,
@required String title,
@required String message,
@required String defaultValue,
@required String hint,
}) async {
assert(context != null);
assert(title != null);
assert(message != null);
assert(defaultValue != null);
assert(hint != null);
TextEditingController controller = TextEditingController(text: defaultValue);
final confirm = await showDialog<bool>(
context: context,
builder: (c) => AlertDialog(
title: Text(title),
content: Column(
children: <Widget>[
Text(message),
TextField(
controller: controller,
maxLines: null,
maxLength: 200,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: hint,
alignLabelWithHint: true,
),
)
],
),
actions: <Widget>[
FlatButton(
child: Text(tr("Cancel").toUpperCase()),
onPressed: () => Navigator.pop(c, false),
),
FlatButton(
child: Text(tr("OK")),
onPressed: () => Navigator.pop(c, true),
),
],
));
if (confirm == null || !confirm) return null;
return controller.text;
}
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({
2019-05-18 07:45:15 +00:00
@required BuildContext context,
String title,
@required String message,
}) async {
if (title == null) title = tr("Confirm operation");
final result = await showDialog<bool>(
context: context,
builder: (c) => AlertDialog(
title: Text(title),
content: Text(message),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context, false),
child: Text(tr("Cancel").toUpperCase()),
),
FlatButton(
onPressed: () => Navigator.pop(context, true),
child: Text(
tr("Confirm").toUpperCase(),
style: TextStyle(color: Colors.red),
),
),
],
));
return result != null && result;
}
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,
}) =>
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 "";
2019-06-24 08:48:31 +00:00
return parse(input).documentElement.text;
}
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
bool darkTheme() => preferences().getBool(PreferencesKeyList.ENABLE_DARK_THEME);
2020-04-16 07:29:37 +00:00
/// Show about Comunic dialog
void showAboutAppDialog(BuildContext context) {
showAboutDialog(
context: context,
applicationName: "Comunic",
children: <Widget>[
Text(
tr("Comunic is a free and OpenSource social network that respect your privacy."),
textAlign: TextAlign.center,
),
SizedBox(
height: 20,
),
Text(
"Application built by Pierre Hubert",
textAlign: TextAlign.center,
),
]);
}