Last active
March 22, 2025 03:55
-
-
Save prof3ssorSt3v3/2a368a05e775675982755f469e854890 to your computer and use it in GitHub Desktop.
Flutter Example that builds a MaterialDesign 3 colorScheme from an image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
ColorScheme? _colorScheme; | |
@override | |
void initState() { | |
super.initState(); | |
_loadColorScheme(); | |
} | |
Future<void> _loadColorScheme() async { | |
final imageProvider = await NetworkImage("https://picsum.photos/id/28/400/300"); | |
final colorScheme = await ColorScheme.fromImageProvider( | |
provider: imageProvider, | |
brightness: Brightness.dark, | |
); | |
if (mounted) { | |
print('colorScheme set'); | |
setState(() { | |
_colorScheme = colorScheme; | |
}); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData( | |
colorScheme: _colorScheme ?? ColorScheme.dark(), | |
brightness: Brightness.dark, | |
), | |
home: Scaffold( | |
backgroundColor: Theme.of(context).colorScheme.secondary, | |
appBar: AppBar(title: Text("Dynamic Color Scheme")), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text(Theme.of(context).colorScheme.primary.toString()), | |
Text(Theme.of(context).colorScheme.secondary.toString()), | |
Text(Theme.of(context).colorScheme.tertiary.toString()), | |
Text(Theme.of(context).colorScheme.surface.toString()), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment