Skip to content

Commit

Permalink
feat: themes
Browse files Browse the repository at this point in the history
  • Loading branch information
z7workbench committed Feb 19, 2022
1 parent d56a23c commit e9cfbbe
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 16 deletions.
52 changes: 44 additions & 8 deletions lib/app_provider.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,78 @@
import 'package:flutter/material.dart';

class AppInfoProvider with ChangeNotifier {
String _themeColor = '';
String themeColor = '';

String get themeColor => _themeColor;
AppInfoProvider({required this.themeColor});

setTheme(String themeColor) {
_themeColor = themeColor;
setTheme(String theme) {
themeColor = theme;
notifyListeners();
}
}

final Map<String, ThemeData> darks = {
"ZeroGo_purple": ThemeData(
"ZeroGo Purple": ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(0xff, 0x64, 0x47, 0xbc),
brightness: Brightness.dark),
fontFamily: "MiSans",
useMaterial3: true),
"Yun_green": ThemeData(
"Yun Green": ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(0xff, 0x47, 0xc2, 0x29),
brightness: Brightness.dark),
fontFamily: "MiSans",
useMaterial3: true),
"Cyan": ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(0xff, 0x00, 0x5b, 0x9f),
brightness: Brightness.dark),
fontFamily: "MiSans",
useMaterial3: true),
"Amber": ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(0xff, 0xff, 0xa0, 0x00),
brightness: Brightness.dark),
fontFamily: "MiSans",
useMaterial3: true),
"Black": ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(0xff, 0x37, 0x37, 0x37),
brightness: Brightness.dark),
fontFamily: "MiSans",
useMaterial3: true),
};

final Map<String, ThemeData> lights = {
"ZeroGo_purple": ThemeData(
"ZeroGo Purple": ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(0xff, 0x97, 0x74, 0xef),
brightness: Brightness.light),
fontFamily: "MiSans",
useMaterial3: true),
"Yun_green": ThemeData(
"Yun Green": ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(0xff, 0x47, 0xc2, 0x29),
brightness: Brightness.light),
fontFamily: "MiSans",
useMaterial3: true),
"Cyan": ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(0xff, 0x03, 0xa9, 0xf4),
brightness: Brightness.light),
fontFamily: "MiSans",
useMaterial3: true),
"Amber": ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(0xff, 0xff, 0xc1, 0x07),
brightness: Brightness.light),
fontFamily: "MiSans",
useMaterial3: true),
"Black": ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: const Color.fromARGB(0xff, 0xbd, 0xbd, 0xbd),
brightness: Brightness.light),
fontFamily: "MiSans",
useMaterial3: true),
};
1 change: 1 addition & 0 deletions lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class MessageLookup extends MessageLookupByLibrary {
"start_game": MessageLookupByLibrary.simpleMessage("Start Game"),
"start_new_game":
MessageLookupByLibrary.simpleMessage("Start a Brand New Game"),
"theme": MessageLookupByLibrary.simpleMessage("Themes"),
"up_coming": MessageLookupByLibrary.simpleMessage("Up Coming Next..."),
"up_coming_desc": MessageLookupByLibrary.simpleMessage(
"More mini games will be released in this app, click here to see the version update logs and future version roadmap!")
Expand Down
1 change: 1 addition & 0 deletions lib/generated/intl/messages_zh_CN.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class MessageLookup extends MessageLookupByLibrary {
"roadmap": MessageLookupByLibrary.simpleMessage("版本更新规划"),
"start_game": MessageLookupByLibrary.simpleMessage("开始游戏"),
"start_new_game": MessageLookupByLibrary.simpleMessage("开始一场新游戏"),
"theme": MessageLookupByLibrary.simpleMessage("主题"),
"up_coming": MessageLookupByLibrary.simpleMessage("即将推出..."),
"up_coming_desc": MessageLookupByLibrary.simpleMessage(
"迷你小游戏合集将会推出更多的小游戏,点击这里可以看到版本更新日志以及未来版本路线图规划!")
Expand Down
10 changes: 10 additions & 0 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
"app": "Mini Games",
"up_coming": "Up Coming Next...",
"up_coming_desc": "More mini games will be released in this app, click here to see the version update logs and future version roadmap!",
"roadmap": "Roadmap"
"roadmap": "Roadmap",
"theme": "Themes"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_zh_CN.arb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
"app": "迷你游戏合集",
"up_coming": "即将推出...",
"up_coming_desc": "迷你小游戏合集将会推出更多的小游戏,点击这里可以看到版本更新日志以及未来版本路线图规划!",
"roadmap": "版本更新规划"
"roadmap": "版本更新规划",
"theme": "主题"
}
80 changes: 74 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,46 @@ import 'package:minigames/roadmap.dart';
import 'package:minigames/styles.dart';
import 'package:minigames/widgets/description_card.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
runApp(MyApp());
void main() async {
String theme = await getTheme();
runApp(MyApp(
theme: theme,
));
}

Future<String> getTheme() async {
SharedPreferences sp = await SharedPreferences.getInstance();
String? themeIndex = sp.getString("theme");
if (themeIndex != null) {
return themeIndex;
}
return '';
}

class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
MyApp({Key? key, required this.theme}) : super(key: key);
late ThemeData lightTheme;
late ThemeData darkTheme;
String theme = '';

// This widget is the root of your application.
@override
Widget build(BuildContext context) => MultiProvider(
providers: [ChangeNotifierProvider.value(value: AppInfoProvider())],
providers: [
ChangeNotifierProvider.value(
value: AppInfoProvider(themeColor: theme))
],
child: Consumer<AppInfoProvider>(
builder: (context, appInfo, _) {
var colorKey = appInfo.themeColor;
if (lights[colorKey] != null) {
lightTheme = lights[colorKey]!;
darkTheme = darks[colorKey]!;
} else {
lightTheme = lights['ZeroGo_purple']!;
darkTheme = darks['ZeroGo_purple']!;
lightTheme = lights['ZeroGo Purple']!;
darkTheme = darks['ZeroGo Purple']!;
}
return MaterialApp(
title: 'Mini Games',
Expand Down Expand Up @@ -63,6 +80,17 @@ class _MyHomePageState extends State<MyHomePage> {
return Scaffold(
appBar: AppBar(
title: Text(S.of(context).app),
actions: [
Container(
child: GestureDetector(
child: const Icon(Icons.format_paint),
onTap: () {
showThemeDialog();
},
),
margin: containerPadding,
)
],
),
body: SingleChildScrollView(
child: Center(
Expand Down Expand Up @@ -90,4 +118,44 @@ class _MyHomePageState extends State<MyHomePage> {
)),
));
}

void showThemeDialog() {
List themes = darks.keys.toList();

showDialog<void>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
title: Text(S.of(context).theme),
content: SingleChildScrollView(
child: Container(
width: MediaQuery.of(context).size.width * 0.6,
height: MediaQuery.of(context).size.height * 0.6,
child: ListView.builder(
shrinkWrap: true,
itemCount: themes.length,
itemBuilder: (BuildContext context, int position) {
return GestureDetector(
child: Container(
padding: const EdgeInsets.all(12.0),
margin: const EdgeInsets.only(bottom: 8),
child: Text(themes[position]),
),
onTap: () async {
Provider.of<AppInfoProvider>(context, listen: false)
.setTheme(themes[position]);
SharedPreferences sp =
await SharedPreferences.getInstance();
sp.setString('theme', themes[position]);
Navigator.of(context).pop();
},
);
}),
),
),
);
},
);
}
}

0 comments on commit e9cfbbe

Please sign in to comment.