revanced-manager/lib/ui/widgets/patchesSelectorView/patch_item.dart

199 lines
7.2 KiB
Dart
Raw Normal View History

2022-08-07 20:05:44 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/services/manager_api.dart';
import 'package:revanced_manager/services/toast.dart';
import 'package:revanced_manager/ui/widgets/shared/custom_card.dart';
import 'package:revanced_manager/ui/widgets/shared/custom_material_button.dart';
2022-08-07 20:05:44 +02:00
2022-08-09 01:01:06 +02:00
// ignore: must_be_immutable
2022-08-07 21:15:52 +02:00
class PatchItem extends StatefulWidget {
PatchItem({
Key? key,
required this.name,
required this.simpleName,
required this.description,
required this.packageVersion,
required this.supportedPackageVersions,
required this.isUnsupported,
required this.isSelected,
required this.onChanged,
this.child,
}) : super(key: key);
2022-08-07 20:05:44 +02:00
final String name;
2022-08-09 03:30:12 +02:00
final String simpleName;
2022-08-07 20:05:44 +02:00
final String description;
final String packageVersion;
final List<String> supportedPackageVersions;
final bool isUnsupported;
2022-08-07 21:15:52 +02:00
bool isSelected;
2022-08-18 16:33:33 +02:00
final Function(bool) onChanged;
2022-09-04 19:54:21 +02:00
final Widget? child;
final toast = locator<Toast>();
final _managerAPI = locator<ManagerAPI>();
2022-08-07 20:05:44 +02:00
2022-08-07 21:15:52 +02:00
@override
State<PatchItem> createState() => _PatchItemState();
}
class _PatchItemState extends State<PatchItem> {
2022-08-07 20:05:44 +02:00
@override
Widget build(BuildContext context) {
widget.isSelected = widget.isSelected &&
(!widget.isUnsupported ||
widget._managerAPI.areExperimentalPatchesEnabled());
2022-09-07 12:55:37 +02:00
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0),
child: Opacity(
opacity: widget.isUnsupported &&
widget._managerAPI.areExperimentalPatchesEnabled() == false
? 0.5
: 1,
child: CustomCard(
onTap: () {
setState(() {
if (widget.isUnsupported &&
!widget._managerAPI.areExperimentalPatchesEnabled()) {
widget.isSelected = false;
widget.toast.showBottom('patchItem.unsupportedPatchVersion');
} else {
widget.isSelected = !widget.isSelected;
}
});
widget.onChanged(widget.isSelected);
},
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Expanded(
child: Text(
widget.simpleName,
maxLines: 2,
overflow: TextOverflow.visible,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
2022-09-24 10:17:09 +02:00
),
],
),
const SizedBox(height: 4),
Text(
widget.description,
softWrap: true,
overflow: TextOverflow.visible,
style: TextStyle(
fontSize: 14,
color: Theme.of(context)
.colorScheme
.onSecondaryContainer,
),
),
],
),
2022-09-19 18:55:44 +02:00
),
Transform.scale(
scale: 1.2,
child: Checkbox(
value: widget.isSelected,
activeColor: Theme.of(context).colorScheme.primary,
checkColor:
Theme.of(context).colorScheme.secondaryContainer,
side: BorderSide(
width: 2.0,
color: Theme.of(context).colorScheme.primary,
),
onChanged: (newValue) {
setState(() {
if (widget.isUnsupported &&
!widget._managerAPI
.areExperimentalPatchesEnabled()) {
widget.isSelected = false;
widget.toast.showBottom(
'patchItem.unsupportedPatchVersion',
);
} else {
widget.isSelected = newValue!;
}
});
widget.onChanged(widget.isSelected);
},
),
)
],
),
if (widget.isUnsupported &&
widget._managerAPI.areExperimentalPatchesEnabled())
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8),
child: TextButton.icon(
label: I18nText('warning'),
icon: const Icon(Icons.warning, size: 20.0),
onPressed: () => _showUnsupportedWarningDialog(),
style: ButtonStyle(
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
side: BorderSide(
color: Theme.of(context).colorScheme.secondary,
),
),
),
backgroundColor: MaterialStateProperty.all(
Colors.transparent,
),
foregroundColor: MaterialStateProperty.all(
Theme.of(context).colorScheme.secondary,
),
),
2022-09-19 18:55:44 +02:00
),
),
],
)
else
Container(),
widget.child ?? const SizedBox(),
],
),
),
2022-08-07 20:05:44 +02:00
),
);
}
Future<void> _showUnsupportedWarningDialog() {
2022-09-02 01:10:41 +02:00
return showDialog(
context: context,
2022-09-02 01:10:41 +02:00
builder: (context) => AlertDialog(
title: I18nText('warning'),
2022-09-07 12:55:37 +02:00
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
2022-09-02 01:10:41 +02:00
content: I18nText(
'patchItem.unsupportedDialogText',
2022-09-02 01:10:41 +02:00
translationParams: {
'packageVersion': widget.packageVersion,
'supportedVersions':
'\u2022 ${widget.supportedPackageVersions.reversed.join('\n\u2022 ')}',
2022-09-02 01:10:41 +02:00
},
),
actions: <Widget>[
CustomMaterialButton(
label: I18nText('okButton'),
2022-09-02 01:10:41 +02:00
onPressed: () => Navigator.of(context).pop(),
)
],
),
);
}
}