fluffychat/lib/widgets/settings_switch_list_tile.dart

42 lines
1.1 KiB
Dart
Raw Normal View History

2021-01-19 16:36:21 +01:00
import 'package:flutter/material.dart';
import 'matrix.dart';
class SettingsSwitchListTile extends StatefulWidget {
final bool defaultValue;
final String storeKey;
final String title;
2022-01-29 12:35:03 +01:00
final Function(bool)? onChanged;
2021-01-19 16:36:21 +01:00
2021-11-27 10:10:29 +01:00
const SettingsSwitchListTile.adaptive({
2022-01-29 12:35:03 +01:00
Key? key,
2021-01-19 16:36:21 +01:00
this.defaultValue = false,
2022-01-29 12:35:03 +01:00
required this.storeKey,
required this.title,
2021-01-19 16:36:21 +01:00
this.onChanged,
}) : super(key: key);
@override
2022-08-14 16:59:21 +02:00
SettingsSwitchListTileState createState() => SettingsSwitchListTileState();
2021-01-19 16:36:21 +01:00
}
2022-08-14 16:59:21 +02:00
class SettingsSwitchListTileState extends State<SettingsSwitchListTile> {
2021-01-19 16:36:21 +01:00
@override
Widget build(BuildContext context) {
return FutureBuilder<bool>(
future: Matrix.of(context)
.store
.getItemBool(widget.storeKey, widget.defaultValue),
2021-11-27 10:10:29 +01:00
builder: (context, snapshot) => SwitchListTile.adaptive(
2021-01-19 16:36:21 +01:00
value: snapshot.data ?? widget.defaultValue,
title: Text(widget.title),
onChanged: (bool newValue) async {
widget.onChanged?.call(newValue);
2022-02-19 11:58:21 +01:00
await Matrix.of(context).store.setItemBool(widget.storeKey, newValue);
2022-01-29 12:35:03 +01:00
setState(() {});
2021-01-19 16:36:21 +01:00
},
),
);
}
}