Skip to content

Commit

Permalink
Make feature switch more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
Process-ing committed Nov 11, 2024
1 parent 911b2b1 commit 87189c7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,17 @@ import 'package:uni/controller/feature_flags/feature_flag_table.dart';
import 'package:uni/generated/l10n.dart';
import 'package:uni/view/settings/widgets/feature_flags/feature_switch_tile.dart';

class FeatureFlagsDialog extends StatefulWidget {
class FeatureFlagsDialog extends StatelessWidget {
const FeatureFlagsDialog({super.key});

@override
FeatureFlagsDialogState createState() => FeatureFlagsDialogState();
}

class FeatureFlagsDialogState extends State<FeatureFlagsDialog> {
@override
void initState() {
super.initState();
}

void refresh() {
setState(() {});
}

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(S.of(context).feature_flags),
content: Column(
mainAxisSize: MainAxisSize.min,
children: FeatureFlagTable.getFeatureFlags()
.map(
(featureFlag) => FeatureSwitchTile(
featureFlag: featureFlag,
refreshDialog: refresh,
),
)
.map((featureFlag) => FeatureSwitchTile(featureFlag: featureFlag))
.toList(),
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import 'package:flutter/material.dart';
import 'package:uni/model/feature_flags/generic_feature_flag.dart';

class FeatureSwitchTile extends StatelessWidget {
class FeatureSwitchTile extends StatefulWidget {
const FeatureSwitchTile({
required this.featureFlag,
required this.refreshDialog,
super.key,
});

final GenericFeatureFlag featureFlag;
final void Function() refreshDialog;

@override
FeatureSwitchTileState createState() => FeatureSwitchTileState();
}

class FeatureSwitchTileState extends State<FeatureSwitchTile> {
void refresh() {
setState(() {});
}

Future<void> _onChanged(bool value) async {
await widget.featureFlag.setEnabled(enabled: value);
refresh();
}

@override
Widget build(BuildContext context) {
return ListTile(
title: Text(featureFlag.getName(context)),
title: Text(widget.featureFlag.getName(context)),
trailing: Switch.adaptive(
value: featureFlag.isEnabled(),
onChanged: (value) async {
await featureFlag.setEnabled(enabled: value);
refreshDialog();
},
value: widget.featureFlag.isEnabled(),
onChanged: _onChanged,
),
);
}
Expand Down

0 comments on commit 87189c7

Please sign in to comment.