Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created March 22, 2024 19:58
Show Gist options
  • Save fredgrott/408b705ba54c764af056a421d23816e6 to your computer and use it in GitHub Desktop.
Save fredgrott/408b705ba54c764af056a421d23816e6 to your computer and use it in GitHub Desktop.
settings view
// ignore_for_file: cast_nullable_to_non_nullable
import 'package:easy_hive/easy_hive.dart';
import 'package:flutter/material.dart';
import 'package:theme_change_anim/src/domain/general_settings_extension.dart';
import 'package:theme_change_anim/src/domain/settings_box_service.dart';
import 'package:theme_change_anim/src/domain/settings_model.dart';
class SettingsView extends StatefulWidget {
const SettingsView({super.key});
static const routeName = '/settings';
@override
State<SettingsView> createState() => _SettingsView();
}
/// Displays the various settings that can be customized by the user.
///
/// When a user changes a setting, the SettingsController is updated and
/// Widgets that listen to the SettingsController are rebuilt.
class _SettingsView extends State<SettingsView> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Settings'),
),
body: Padding(
padding: const EdgeInsets.all(16),
// Glue the SettingsController to the theme selection DropdownButton.
//
// When a user selects a theme from the dropdown list, the
// SettingsController is updated, which rebuilds the MaterialApp.
child: DropdownButton<ThemeMode>(
// Read the selected themeMode from the controller
value: [SettingsModel.themeMode].of(SettingsBoxService()) as ThemeMode,
// Call the updateThemeMode method any time the user selects a theme.
onChanged: (ThemeMode? themeModeValue) {
setState(() {
// the getter is always called this way as far as extension methods
SettingsBoxService().themeMode = themeModeValue as ThemeMode;
});
},
items: const [
DropdownMenuItem(
value: ThemeMode.system,
child: Text('System Theme'),
),
DropdownMenuItem(
value: ThemeMode.light,
child: Text('Light Theme'),
),
DropdownMenuItem(
value: ThemeMode.dark,
child: Text('Dark Theme'),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment