Skip to content

Instantly share code, notes, and snippets.

@nonsocchi
Last active May 17, 2023 19:05
Show Gist options
  • Save nonsocchi/b7ffd347db3ae23d1e0fef500cd89a5e to your computer and use it in GitHub Desktop.
Save nonsocchi/b7ffd347db3ae23d1e0fef500cd89a5e to your computer and use it in GitHub Desktop.
riverpod_theme_app: creating widgets in the presentation layer.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'theming_controller.dart';
class ThemingView extends StatelessWidget {
const ThemingView({super.key});
static const String themingRoute = '/theming';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Theming'),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
...ThemeMode.values
.map((themeMode) => ThemeSelector(themeMode: themeMode))
.toList(),
const SizedBox(height: 100),
const ThemeMessage()
],
),
),
);
}
}
/// Custom RadioListTiles for selecting theming options.
class ThemeSelector extends ConsumerWidget {
const ThemeSelector({super.key, required this.themeMode});
final ThemeMode themeMode;
@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = ref.watch(themingControllerProvider);
return RadioListTile<ThemeMode>(
title: Text('${themeMode.name} theme'),
groupValue: theme,
value: themeMode,
onChanged: ref.watch(themingControllerProvider.notifier).updateThemeMode,
controlAffinity: ListTileControlAffinity.trailing,
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
activeColor: Theme.of(context).colorScheme.primary,
);
}
}
class ThemeMessage extends ConsumerWidget {
const ThemeMessage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = ref.watch(themingControllerProvider);
return switch (theme) {
ThemeMode.system => const Text('We are who the master say we are'),
ThemeMode.dark => const Text('Welcome to the dark side'),
ThemeMode.light => const Text('The light is blinding')
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment