Skip to content

Instantly share code, notes, and snippets.

@Luckey-Elijah
Created December 16, 2024 18:40
Show Gist options
  • Save Luckey-Elijah/a9abceec9b97eaa0d5f2d9e6a41e9f78 to your computer and use it in GitHub Desktop.
Save Luckey-Elijah/a9abceec9b97eaa0d5f2d9e6a41e9f78 to your computer and use it in GitHub Desktop.
Simple Theme Picker
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: controller,
builder: (context, child) => MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(colorScheme: controller.scheme),
home: const ThemePickerPage(),
),
);
}
}
class ThemePickerPage extends StatelessWidget {
const ThemePickerPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
for (final scheme in controller.options)
ColorSchemeViewButton(scheme: scheme),
],
),
);
}
}
class ColorSchemeViewButton extends StatelessWidget {
const ColorSchemeViewButton({
super.key,
required this.scheme,
});
final ColorScheme scheme;
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: controller,
builder: (context, _) {
return Theme(
data: ThemeData(colorScheme: scheme),
child: Builder(
builder: (context) {
final colors = Theme.of(context).colorScheme;
return InkWell(
onTap: () => controller.setScheme(scheme),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Visibility.maintain(
visible: scheme == controller.scheme,
child: const Icon(Icons.check_circle_sharp),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox.square(
dimension: 20,
child: ColoredBox(color: colors.primary),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox.square(
dimension: 20,
child: ColoredBox(color: colors.secondary),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox.square(
dimension: 20,
child: ColoredBox(color: colors.tertiary),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox.square(
dimension: 20,
child: ColoredBox(color: colors.error),
),
)
],
),
);
},
),
);
},
);
}
}
final controller = ColorSchemeController();
class ColorSchemeController extends ChangeNotifier {
static final _defaultScheme = ColorScheme.fromSeed(seedColor: Colors.blue);
var scheme = _defaultScheme;
final options = <ColorScheme>[
_defaultScheme,
ColorScheme.fromSeed(seedColor: Colors.red),
ColorScheme.fromSeed(seedColor: Colors.yellow),
ColorScheme.fromSeed(seedColor: Colors.green),
];
void setScheme(ColorScheme colorScheme) {
scheme = colorScheme;
notifyListeners();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment