fluffychat/lib/components/dialogs/permission_slider_dialog.dart

98 lines
2.7 KiB
Dart
Raw Normal View History

2020-12-05 13:03:57 +01:00
import 'package:fluffychat/components/dialogs/adaptive_flat_button.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
class PermissionSliderDialog extends StatefulWidget {
2021-01-19 15:46:43 +01:00
const PermissionSliderDialog({
Key key,
this.initialPermission = 0,
}) : super(key: key);
2020-12-05 13:03:57 +01:00
Future<int> show(BuildContext context) => PlatformInfos.isCupertinoStyle
2021-02-24 12:17:23 +01:00
? showCupertinoDialog<int>(
context: context,
builder: (context) => this,
useRootNavigator: false,
)
: showDialog<int>(
context: context,
builder: (context) => this,
useRootNavigator: false,
);
2020-12-05 13:03:57 +01:00
final int initialPermission;
@override
_PermissionSliderDialogState createState() => _PermissionSliderDialogState();
}
class _PermissionSliderDialogState extends State<PermissionSliderDialog> {
int _permission;
@override
void initState() {
_permission = widget.initialPermission;
super.initState();
}
@override
Widget build(BuildContext context) {
final slider = PlatformInfos.isCupertinoStyle
? CupertinoSlider(
value: _permission.toDouble(),
onChanged: (d) => setState(() => _permission = d.round()),
max: 100.0,
min: 0.0,
)
: Slider(
value: _permission.toDouble(),
onChanged: (d) => setState(() => _permission = d.round()),
max: 100.0,
min: 0.0,
);
final title = Text(
2021-02-24 12:17:23 +01:00
L10n.of(context).setPermissionsLevel,
2020-12-05 13:03:57 +01:00
textAlign: TextAlign.center,
);
final content = Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Level: ' +
(_permission == 100
2021-02-24 12:17:23 +01:00
? '$_permission (${L10n.of(context).admin})'
2020-12-05 13:03:57 +01:00
: _permission >= 50
2021-02-24 12:17:23 +01:00
? '$_permission (${L10n.of(context).moderator})'
2020-12-05 13:03:57 +01:00
: _permission.toString())),
Container(
height: 56,
child: slider,
),
],
);
final buttons = [
AdaptiveFlatButton(
2021-02-27 07:53:34 +01:00
label: L10n.of(context).cancel,
2021-02-24 12:17:23 +01:00
onPressed: () =>
Navigator.of(context, rootNavigator: false).pop<int>(null),
2020-12-05 13:03:57 +01:00
),
AdaptiveFlatButton(
2021-02-27 07:53:34 +01:00
label: L10n.of(context).confirm,
2021-02-24 12:17:23 +01:00
onPressed: () =>
Navigator.of(context, rootNavigator: false).pop<int>(_permission),
2020-12-05 13:03:57 +01:00
),
];
if (PlatformInfos.isCupertinoStyle) {
return CupertinoAlertDialog(
title: title,
content: content,
actions: buttons,
);
}
return AlertDialog(
title: title,
content: content,
actions: buttons,
);
}
}