Skip to content

Commit

Permalink
Complete theme color selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Xmarmalade committed Sep 17, 2022
1 parent ad8b9c4 commit 582f085
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 53 deletions.
1 change: 0 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:ice_live_viewer/pages/home.dart';
import 'package:ice_live_viewer/pages/newhome.dart';
Expand Down
17 changes: 13 additions & 4 deletions lib/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class SettingsPage extends StatelessWidget {
title: 'General',
),
ListTile(
title: const Text('Settings'),
title: const Text('Under Construction'),
subtitle: const Text('This page is still not complete'),
leading: const Icon(
Icons.warning_amber_rounded,
Icons.construction_rounded,
size: 32,
),
onTap: () {},
Expand All @@ -41,9 +41,18 @@ class SettingsPage extends StatelessWidget {
),
onTap: () {
Provider.of<AppThemeProvider>(context, listen: false)
.showThemeDialog(context);
.showThemeSelectorDialog(context);
},
),
ListTile(
title: const Text('Change Language'),
subtitle: const Text('Change the language of the app'),
leading: const Icon(
Icons.translate,
size: 32,
),
onTap: () {},
),
const SwitchTile(
title: 'Use custom resolution for Huya',
subtitle:
Expand All @@ -60,7 +69,7 @@ class SettingsPage extends StatelessWidget {
title: 'Experimental',
),
const SwitchTile(
title: '[Only Windows] Use Native Player',
title: '[Only Windows|Not complete] Use Native Player',
subtitle:
'This setup only uses Win32 APIs & no texture, intermediate buffers or copying of pixel buffers.',
settingKey: 'use_native_player',
Expand Down
66 changes: 32 additions & 34 deletions lib/provider/themeprovider.dart
Original file line number Diff line number Diff line change
@@ -1,72 +1,70 @@
import 'package:flutter/material.dart';
import 'package:ice_live_viewer/utils/prefs_helper.dart';

class AppThemeProvider extends ChangeNotifier {
AppTheme() {
changeThemeColor(0);
AppThemeProvider() {
changeThemeColor(PrefsHelper.getThemeColorPrefIndex());
}

int _themeModeIndex = 0;
int get themeModeIndex => _themeModeIndex;
int _themeColorIndex = 4;
int get themeColorIndex => _themeColorIndex;
String _themeColorName = '';

get themeColor => themeColors.values.toList()[_themeColorIndex];

static Map<String, ThemeMode> themeModes = {
"system": ThemeMode.system,
"on": ThemeMode.dark,
"off": ThemeMode.light,
};

static Map<String, Color> themeColors = {
"indigo": Colors.indigo,
"violet": Colors.deepPurple,
"grass": Colors.lightGreen,
"teal": Colors.teal,
"seafoam": const Color.fromARGB(255, 112, 193, 207),
"Crimson": const Color.fromARGB(255, 220, 20, 60),
"Orange": Colors.orange,
"Chrome": const Color.fromARGB(255, 230, 184, 0),
"Grass": Colors.lightGreen,
"Teal": Colors.teal,
"Sea Foam": const Color.fromARGB(255, 112, 193, 207),
"Blue": Colors.blue,
"Indigo": Colors.indigo,
"Violet": Colors.deepPurple,
"Orchid": const Color.fromARGB(255, 218, 112, 214),
};

void setThemeModeIndex(int index) {
_themeModeIndex = index;
notifyListeners();
}

void setThemeColorIndex(int index) {
_themeColorIndex = index;
notifyListeners();
}

late Color _themeColor;
late String _themeColorName;
get themeColor => _themeColor;
get themeColorName => _themeColorName;
void changeThemeColor(int index) {
_themeColor = AppThemeProvider.themeColors.values.toList()[index];
_themeColorName = AppThemeProvider.themeColors.keys.toList()[index];
notifyListeners();
PrefsHelper.setThemeColorPrefIndex(index);
}

List<Widget> _createThemeWidget(BuildContext context) {
List<Widget> widgets = [];
List<Widget> _createThemeSelectorWidget(BuildContext context) {
List<Widget> themeSelectorWidgets = [];
for (var item in AppThemeProvider.themeColors.keys) {
widgets.add(RadioListTile(
themeSelectorWidgets.add(RadioListTile(
groupValue: item,
value: _themeColorName,
title: Text(item,
style: TextStyle(color: AppThemeProvider.themeColors[item])),
onChanged: (value) {
setThemeColorIndex(
changeThemeColor(
AppThemeProvider.themeColors.keys.toList().indexOf(item));
Navigator.of(context).pop();
//Navigator.of(context).pop();
},
));
}
return widgets;
return themeSelectorWidgets;
}

void showThemeDialog(BuildContext context) {
void showThemeSelectorDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: const Text('切换主题'),
children: _createThemeWidget(context),
title: Text(
'Change Theme',
style:
TextStyle(color: AppThemeProvider.themeColors[_themeColorName]),
),
children: _createThemeSelectorWidget(context),
);
},
);
Expand Down
1 change: 0 additions & 1 deletion lib/utils/http/huyaparser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ Future<Map<String, dynamic>> getLiveInfo(String url) async {
} else {
int lUid = roomInfo['profileInfo']['uid'];
String cover = roomInfo['liveData']['screenshot'];
//TODO fix cover 403
Map streamDict = roomInfo['stream']['flv'];
List multiLine = streamDict['multiLine'];
List rateArray = streamDict['rateArray'];
Expand Down
18 changes: 10 additions & 8 deletions lib/utils/init/ioinit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import 'package:dart_vlc/dart_vlc.dart';
import 'dart:io';
import 'package:ice_live_viewer/main.dart';
import 'package:ice_live_viewer/provider/themeprovider.dart';
import 'package:ice_live_viewer/utils/prefs_helper.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

void init() {
void init() async {
if (Platform.isWindows) {
DartVLC.initialize(useFlutterNativeView: false);
runApp(MultiProvider(providers: [
ChangeNotifierProvider(create: (_) => AppThemeProvider()),
], child: const MyApp()));
} else {
runApp(MultiProvider(providers: [
ChangeNotifierProvider(create: (_) => AppThemeProvider()),
], child: const MyApp()));
}
PrefsHelper.prefs = await SharedPreferences.getInstance();
runApp(MultiProvider(providers: [
ChangeNotifierProvider(
create: (_) => AppThemeProvider(),
lazy: false,
),
], child: const MyApp()));
}
15 changes: 15 additions & 0 deletions lib/utils/prefs_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:shared_preferences/shared_preferences.dart';

///This is the new class for the shared preferences.
///And the old storage.dart will be deprecated.
class PrefsHelper {
static late SharedPreferences prefs;

static int getThemeColorPrefIndex() {
return prefs.getInt('theme_color') ?? 0;
}

static void setThemeColorPrefIndex(int pref) async {
prefs.setInt('theme_color', pref);
}
}
2 changes: 1 addition & 1 deletion lib/utils/storage.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
//import 'dart:convert';
//TODO deprecated this util file and use the new one.

//create a function to get all the data and store every value into a map
Future<Map<String, dynamic>> getAllData() async {
Expand Down
10 changes: 6 additions & 4 deletions lib/utils/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class MyTheme {

get lightThemeData => ThemeData.from(
colorScheme: ColorScheme.fromSeed(
seedColor: primaryColor,
brightness: Brightness.light,
primary: primaryColor,
secondary: primaryColor),
seedColor: primaryColor,
brightness: Brightness.light,
primary: primaryColor,
secondary: primaryColor,
background: Colors.grey[50],
),
useMaterial3: true,
textTheme: TextTheme(
headline1: TextStyle(
Expand Down

0 comments on commit 582f085

Please sign in to comment.