fmassive/lib/settings_page.dart

133 lines
4.4 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/sound_picker.dart';
import 'package:moor/moor.dart';
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key, required this.search});
final String search;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _SettingsPage(search: search),
),
);
}
}
class _SettingsPage extends StatefulWidget {
const _SettingsPage({Key? key, required this.search}) : super(key: key);
final String search;
@override
createState() => _SettingsPageState();
}
class _SettingsPageState extends State<_SettingsPage> {
late Stream<Setting> stream;
@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());
final filteredItems = [
{'title': 'Alarm', 'value': settings.alarm},
{'title': 'Vibrate', 'value': settings.vibrate},
{'title': 'Notify', 'value': settings.notify},
{'title': 'Images', 'value': settings.images},
{'title': 'Show Unit', 'value': settings.showUnit},
{'title': 'Steps', 'value': settings.steps},
{'title': 'Sound', 'value': settings.sound},
]
.where((item) => (item['title'] as String)
.toLowerCase()
.contains(widget.search.toLowerCase()))
.toList();
return material.Column(
children: [
Expanded(
child: ListView.builder(
itemCount: filteredItems.length,
itemBuilder: (context, index) {
final item = filteredItems[index];
if (item['title'] == 'Sound') {
return Center(
child: SoundPicker(
path: settings.sound,
onSelect: (path) {
db
.update(db.settings)
.write(SettingsCompanion(sound: Value(path)));
},
),
);
}
return SwitchListTile(
title: Text(item['title'].toString()),
value: item['value'] as bool,
onChanged: (value) {
switch (item['title']) {
case 'Alarm':
db
.update(db.settings)
.write(SettingsCompanion(alarm: Value(value)));
break;
case 'Vibrate':
db.update(db.settings).write(
SettingsCompanion(vibrate: Value(value)));
break;
case 'Notify':
db
.update(db.settings)
.write(SettingsCompanion(notify: Value(value)));
break;
case 'Images':
db
.update(db.settings)
.write(SettingsCompanion(images: Value(value)));
break;
case 'Show Unit':
db.update(db.settings).write(
SettingsCompanion(showUnit: Value(value)));
break;
case 'Steps':
db
.update(db.settings)
.write(SettingsCompanion(steps: Value(value)));
break;
}
},
);
},
),
),
],
);
},
),
);
}
}