Skip to content

Commit

Permalink
support theme select
Browse files Browse the repository at this point in the history
  • Loading branch information
gusibi committed Jul 2, 2023
1 parent aa30b83 commit c96d2ef
Show file tree
Hide file tree
Showing 21 changed files with 585 additions and 230 deletions.
169 changes: 169 additions & 0 deletions lib/constants/color_constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import 'package:flex_color_scheme/flex_color_scheme.dart';

var blumineBlueLight = FlexThemeData.light(
scheme: FlexScheme.blumineBlue,
surfaceMode: FlexSurfaceMode.highScaffoldLevelSurface,
blendLevel: 9,
subThemesData: const FlexSubThemesData(
blendOnLevel: 10,
blendOnColors: false,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
// To use the playground font, add GoogleFonts package and uncomment
// fontFamily: GoogleFonts.notoSans().fontFamily,
);

var blumineBlueDark = FlexThemeData.dark(
scheme: FlexScheme.blumineBlue,
surfaceMode: FlexSurfaceMode.levelSurfacesLowScaffold,
blendLevel: 15,
subThemesData: const FlexSubThemesData(
blendOnLevel: 20,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
// To use the Playground font, add GoogleFonts package and uncomment
// fontFamily: GoogleFonts.notoSans().fontFamily,
);

var bigStoneLight = FlexThemeData.light(
scheme: FlexScheme.bigStone,
surfaceMode: FlexSurfaceMode.highScaffoldLevelSurface,
blendLevel: 9,
subThemesData: const FlexSubThemesData(
blendOnLevel: 10,
blendOnColors: false,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
// To use the playground font, add GoogleFonts package and uncomment
// fontFamily: GoogleFonts.notoSans().fontFamily,
);
var bigStoneDark = FlexThemeData.dark(
scheme: FlexScheme.bigStone,
surfaceMode: FlexSurfaceMode.levelSurfacesLowScaffold,
blendLevel: 15,
subThemesData: const FlexSubThemesData(
blendOnLevel: 20,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
);

var bahamaBlueLight = FlexThemeData.light(
scheme: FlexScheme.bahamaBlue,
surfaceMode: FlexSurfaceMode.highScaffoldLevelSurface,
blendLevel: 9,
subThemesData: const FlexSubThemesData(
blendOnLevel: 10,
blendOnColors: false,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
// To use the playground font, add GoogleFonts package and uncomment
// fontFamily: GoogleFonts.notoSans().fontFamily,
);
var bahamaBlueDark = FlexThemeData.dark(
scheme: FlexScheme.bahamaBlue,
surfaceMode: FlexSurfaceMode.levelSurfacesLowScaffold,
blendLevel: 15,
subThemesData: const FlexSubThemesData(
blendOnLevel: 20,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
// To use the Playground font, add GoogleFonts package and uncomment
// fontFamily: GoogleFonts.notoSans().fontFamily,
);

var materialBaselineLight = FlexThemeData.light(
scheme: FlexScheme.materialBaseline,
surfaceMode: FlexSurfaceMode.highScaffoldLevelSurface,
blendLevel: 9,
subThemesData: const FlexSubThemesData(
blendOnLevel: 10,
blendOnColors: false,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
);
var materialBaselineDark = FlexThemeData.dark(
scheme: FlexScheme.materialBaseline,
surfaceMode: FlexSurfaceMode.levelSurfacesLowScaffold,
blendLevel: 15,
subThemesData: const FlexSubThemesData(
blendOnLevel: 20,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
);

var greenLight = FlexThemeData.light(
scheme: FlexScheme.green,
surfaceMode: FlexSurfaceMode.highScaffoldLevelSurface,
blendLevel: 9,
subThemesData: const FlexSubThemesData(
blendOnLevel: 10,
blendOnColors: false,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
// To use the playground font, add GoogleFonts package and uncomment
// fontFamily: GoogleFonts.notoSans().fontFamily,
);
var greenDark = FlexThemeData.dark(
scheme: FlexScheme.green,
surfaceMode: FlexSurfaceMode.levelSurfacesLowScaffold,
blendLevel: 15,
subThemesData: const FlexSubThemesData(
blendOnLevel: 20,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
// To use the Playground font, add GoogleFonts package and uncomment
// fontFamily: GoogleFonts.notoSans().fontFamily,
);

const String darkModeLight = "light";
const String darkModeDark = "dark";
const String darkModeSystem = "system";

const String blumineBlue = "blumineBlue";
const String bigStone = "bigStone";
const String bahamaBlue = "bahamaBlue";
const String materialBaseline = "materialBaseline";
const String green = "green";

var themesMap = {
blumineBlue: {darkModeLight: blumineBlueLight, darkModeDark: blumineBlueDark},
bigStone: {darkModeLight: bigStoneLight, darkModeDark: bigStoneDark},
bahamaBlue: {darkModeLight: bahamaBlueLight, darkModeDark: bahamaBlueDark},
materialBaseline: {
darkModeLight: materialBaselineLight,
darkModeDark: materialBaselineDark
},
green: {darkModeLight: greenLight, darkModeDark: greenDark},
};

const defaultTheme = blumineBlue;

enum DarkModes { darkModeLight, darkModeDark, darkModeSystem }

const defaultDarkMode = darkModeSystem;

final Map<DarkModes, String> darkModesMap = {
DarkModes.darkModeLight: darkModeLight,
DarkModes.darkModeDark: darkModeDark,
DarkModes.darkModeSystem: darkModeSystem,
};
3 changes: 1 addition & 2 deletions lib/constants/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:flutter/material.dart';

import '../models/conversation_model.dart';

Color scaffoldBackgroundColor = Colors.blueGrey;

List<String> modelsList = [
"chat-bison-001",
"text-bison-001",
Expand Down Expand Up @@ -52,6 +50,7 @@ final ConversationModel newConversation = ConversationModel(
);

const String palmConfigname = "palmConfig";
const String themeConfigname = "themeConfig";
const String roleUser = "user";
const String roleAI = "ai";
const String roleSys = "system";
Expand Down
58 changes: 45 additions & 13 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import 'package:flutter/material.dart';
import 'package:moli_ai/constants/color_constants.dart';
import 'package:provider/provider.dart';

import 'package:flex_color_scheme/flex_color_scheme.dart';

import 'package:moli_ai/constants/constants.dart';

import 'models/config_model.dart';
import 'repositories/configretion/config_repo.dart';
import 'repositories/datebase/client.dart';
import 'providers/palm_priovider.dart';
import 'screens/conversation_screen.dart';
Expand All @@ -26,8 +31,45 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
Map<String, ConfigModel> _configMap = {};
String themeName = "";
String darkMode = "";
ThemeData lightTheme = blumineBlueLight;
ThemeData darkTheme = blumineBlueDark;

@override
void initState() {
super.initState();
_initConfig();
}

void _initConfig() async {
_configMap = await ConfigReop().getAllConfigsMap();

ConfigModel? themeConf = _configMap[themeConfigname];
if (themeConf != null) {
final themeConfig = themeConf.toThemeConfig();
themeName = themeConfig.themeName;
darkMode = themeConfig.darkMode;
Map<String, ThemeData>? theme = themesMap[themeName];
if (theme != null) {
lightTheme = theme[darkModeLight]!;
darkTheme = theme[darkModeDark]!;
}
setState(() {
themeName = themeConfig.themeName;
darkMode = themeConfig.darkMode;
});
}
}

@override
Widget build(BuildContext context) {
var isDarkMode =
MediaQuery.of(context).platformBrightness == Brightness.dark;
if (darkMode != "" && darkMode == darkModeDark) {
isDarkMode = true;
}
// dbClient.q();
return MultiProvider(
providers: [
Expand All @@ -37,11 +79,9 @@ class _MyAppState extends State<MyApp> {
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
// theme: ThemeData(
// useMaterial3: true,
// colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.blue),
// ),
theme: ThemeData.light(useMaterial3: true),
theme: lightTheme,
darkTheme: darkTheme,
themeMode: isDarkMode ? ThemeMode.dark : ThemeMode.light,
home: const RootPage(),
),
);
Expand All @@ -56,10 +96,6 @@ class RootPage extends StatefulWidget {
}

class _RootPageState extends State<RootPage> {
late final _colorScheme = Theme.of(context).colorScheme;
late final _backgroundColor = Color.alphaBlend(
_colorScheme.primary.withOpacity(0.14), _colorScheme.surface);

int selectedIndex = 0;

static const List<Widget> _widgetOptions = <Widget>[
Expand Down Expand Up @@ -90,7 +126,6 @@ class _RootPageState extends State<RootPage> {
if (wideScreen)
DisappearingNavigationRail(
selectedIndex: selectedIndex,
backgroundColor: Colors.white,
onDestinationSelected: (index) {
setState(() {
selectedIndex = index;
Expand All @@ -99,7 +134,6 @@ class _RootPageState extends State<RootPage> {
),
Expanded(
child: Container(
color: _backgroundColor,
child: _widgetOptions.elementAt(selectedIndex),
),
),
Expand All @@ -109,8 +143,6 @@ class _RootPageState extends State<RootPage> {
? null
: FloatingActionButton(
heroTag: "newConversation",
backgroundColor: _colorScheme.tertiaryContainer,
foregroundColor: _colorScheme.onTertiaryContainer,
onPressed: () {
_navigateToCreateNewConversation();
},
Expand Down
27 changes: 27 additions & 0 deletions lib/models/config_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ class PalmConfig {
);
}

class ThemeConfig {
String themeName;
String darkMode; // 0 = light, 1 = dark, 2 = system

ThemeConfig({
required this.themeName,
required this.darkMode,
});

Map<String, dynamic> toMap() {
return {
'themeName': themeName,
'darkMode': darkMode,
};
}

factory ThemeConfig.fromJson(Map<dynamic, dynamic> json) => ThemeConfig(
themeName: json['themeName'] as String,
darkMode: json['darkMode'] as String,
);
}

class ConfigModel {
final int id;
final String configName;
Expand Down Expand Up @@ -55,4 +77,9 @@ class ConfigModel {
Map config = jsonDecode(value);
return PalmConfig.fromJson(config);
}

ThemeConfig toThemeConfig() {
Map config = jsonDecode(value);
return ThemeConfig.fromJson(config);
}
}
32 changes: 32 additions & 0 deletions lib/repositories/configretion/config_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@ class ConfigReop {
}
}

Future<int> createOrUpdateThemeConfig(ThemeConfig config) async {
final Database db = dbClient.get();
var configName = themeConfigname;

ConfigModel? conf = await getConfigByName(configName);
if (conf == null) {
return await db.insert(
'config_tab',
{
'configName': configName,
'value': jsonEncode(config.toMap()),
'createTime': DateTime.now().second,
'updateTime': DateTime.now().second,
},
conflictAlgorithm: ConflictAlgorithm.replace,
);
} else {
// Update the given Dog.
return await db.update(
'config_tab',
{
'value': jsonEncode(config.toMap()),
'updateTime': DateTime.now().second,
},
// Ensure that the Dog has a matching id.
where: 'id = ?',
// Pass the Dog's id as a whereArg to prevent SQL injection.
whereArgs: [conf.id],
);
}
}

Future<ConfigModel?> getConfigByName(String name) async {
final Database db = dbClient.get();

Expand Down
Loading

0 comments on commit c96d2ef

Please sign in to comment.