Skip to content

Commit

Permalink
done: app theme based on today's record
Browse files Browse the repository at this point in the history
  • Loading branch information
apoleon33 committed Jan 2, 2024
1 parent ba83c0c commit 616b998
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ One album per day
- [x] better algorithm
- [x] always accurate cover & links
- [x] deactivate spotify link if it does not exist
- [ ] app theme based on today's record
- [x] app theme based on today's record
- [ ] working buttons
- [ ] link to spotify
- [ ] see more (...)
Expand Down
17 changes: 3 additions & 14 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:cradle/albumCard.dart';
import 'package:cradle/theme_manager.dart';
import 'package:flutter/material.dart';

void main() async {
Expand All @@ -12,20 +13,8 @@ class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
Color seedColor = const Color(0x00b3272c);
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: seedColor),
useMaterial3: true,
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: seedColor, brightness: Brightness.dark),
useMaterial3: true,
),
themeMode: ThemeMode.system,
home: const MyHomePage(title: 'CRADLE'),
return DynamicTheme(
child: const MyHomePage(title: 'CRADLE'),
);
}
}
Expand Down
67 changes: 67 additions & 0 deletions lib/theme_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class DynamicTheme extends StatefulWidget {
Widget child;
ImageProvider image = const AssetImage("assets/default.png");

DynamicTheme({super.key, required this.child});

@override
State<DynamicTheme> createState() => _DynamicTheme();
}

class _DynamicTheme extends State<DynamicTheme> {
late ColorScheme currentColorScheme;
late ColorScheme currentDarkColorScheme;

@override
void initState() {
super.initState();
_getTodayImage();
currentColorScheme = const ColorScheme.light();
currentDarkColorScheme = const ColorScheme.dark();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
createTheme(widget.image);
});
}

void _getTodayImage() async {
DateTime todayDate = DateTime.now();
Dio dio = Dio();
String website =
"https://cradle-api.vercel.app/album/${todayDate.year}/${todayDate.month}/${todayDate.day}";

Response apiCall = await dio.get(website);
Map result = apiCall.data;

widget.image = NetworkImage(result['image']);
createTheme(widget.image);
}

@override
Widget build(BuildContext context) {
final ColorScheme lightColorScheme = currentColorScheme;
final ColorScheme darkColorScheme = currentDarkColorScheme;
return MaterialApp(
title: 'Cradle',
theme: ThemeData(useMaterial3: true, colorScheme: lightColorScheme),
darkTheme: ThemeData(useMaterial3: true, colorScheme: darkColorScheme),
themeMode: ThemeMode.system,
debugShowCheckedModeBanner: false,
home: widget.child);
}

Future<void> createTheme(ImageProvider provider) async {
final ColorScheme newLightColorScheme = await ColorScheme.fromImageProvider(
provider: provider, brightness: Brightness.light);

final ColorScheme newDarkColorScheme = await ColorScheme.fromImageProvider(
provider: provider, brightness: Brightness.dark);

setState(() {
currentColorScheme = newLightColorScheme;
currentDarkColorScheme = newDarkColorScheme;
});
}
}

0 comments on commit 616b998

Please sign in to comment.