fmassive/lib/settings_page.dart

192 lines
6.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as material;
import 'package:fmassive/database.dart';
import 'package:fmassive/main.dart';
import 'package:fmassive/settings.dart';
import 'package:moor/moor.dart';
class SettingsPage extends StatelessWidget {
SettingsPage({super.key});
final List<Map<String, dynamic>> routes = [
{'title': 'Home', 'icon': Icons.home},
{'title': 'Plans', 'icon': Icons.calendar_today},
{'title': 'Best', 'icon': Icons.star},
{'title': 'Workouts', 'icon': Icons.fitness_center},
{'title': 'Timer', 'icon': Icons.timer},
{'title': 'Settings', 'icon': Icons.settings},
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Settings'),
),
drawer: Drawer(
child: ListView.builder(
itemCount: routes.length,
itemBuilder: (context, index) {
return ListTile(
leading: Icon(routes[index]['icon']),
title: Text(routes[index]['title']),
onTap: () {
Navigator.pop(context);
Navigator.pushNamed(
context, '/${routes[index]['title'].toLowerCase()}');
},
);
},
),
),
body: const Center(
child: _SettingsPage(),
),
);
}
}
class _SettingsPage extends StatefulWidget {
const _SettingsPage({Key? key}) : super(key: key);
@override
createState() => _SettingsPageState();
}
class _SettingsPageState extends State<_SettingsPage> {
late Stream<Setting> stream;
final TextEditingController searchController = TextEditingController();
void reset() async {
var data = await db.select(db.settings).get();
if (data.isEmpty) await db.into(db.settings).insert(defaultSettings);
setState(() {
if (data.isEmpty) return;
});
}
@override
void initState() {
super.initState();
stream = db.select(db.settings).watchSingle();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<Setting>(
stream: stream,
builder: (context, snapshot) {
final settings = snapshot.data;
if (settings == null)
return const Center(child: CircularProgressIndicator());
return SingleChildScrollView(
padding: const EdgeInsets.all(8.0),
child: material.Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SwitchListTile(
title: const Text('Alarm'),
value: settings.alarm,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(alarm: Value(value)));
},
),
SwitchListTile(
title: const Text('Vibrate'),
value: settings.vibrate,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(vibrate: Value(value)));
},
),
SwitchListTile(
title: const Text('Notify'),
value: settings.notify,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(notify: Value(value)));
},
),
SwitchListTile(
title: const Text('Images'),
value: settings.images,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(images: Value(value)));
},
),
SwitchListTile(
title: const Text('Show Unit'),
value: settings.showUnit,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(showUnit: Value(value)));
},
),
SwitchListTile(
title: const Text('Steps'),
value: settings.steps,
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(steps: Value(value)));
},
),
TextField(
decoration: const InputDecoration(
labelText: 'Sound',
),
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(sound: Value(value)));
},
),
TextField(
decoration: const InputDecoration(
labelText: 'Light Color',
),
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(lightColor: Value(value)));
},
),
TextField(
decoration: const InputDecoration(
labelText: 'Dark Color',
),
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(darkColor: Value(value)));
},
),
TextField(
decoration: const InputDecoration(
labelText: 'Date',
),
onChanged: (value) {
db
.update(db.settings)
.write(SettingsCompanion(date: Value(value)));
},
),
],
),
);
}),
);
}
}