Skip to content

Instantly share code, notes, and snippets.

@SouravKumarPandit
Created April 8, 2019 12:41
Show Gist options
  • Save SouravKumarPandit/1fc7b29ab4cfe95ab4b46660804f84c6 to your computer and use it in GitHub Desktop.
Save SouravKumarPandit/1fc7b29ab4cfe95ab4b46660804f84c6 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
enum CLThemeConstants {
LIGHT_RED,
LIGHT_BLUE,
LIGHT_GRAY,
DARK_ORANGE,
DARK_GREEN,
DARK_BROWN
}
class CLThemeItemsDto {
CLThemeItemsDto(this.sThemeName, this.sThemeNameId, this.themeColors,
this.isSelectedTheme, this.themeBuilder);
final String sThemeName;
final CLThemeConstants sThemeNameId;
final LinearGradient themeColors;
final bool isSelectedTheme;
final _ThemeColor themeBuilder;
}
class _CLThemeListOption {
_CLThemeListOption() {
// for setting Default Theme in case
_themeData = _appLightThemeBuilder(_ThemeColorDarkGreen());
}
ThemeData _themeData;
CLThemeConstants currentTheme;
ThemeData buildThemeData(CLThemeConstants themeConstants) {
if (currentTheme == null || currentTheme.index != themeConstants.index) {
switch (themeConstants) {
case CLThemeConstants.LIGHT_RED:
_themeData = _appLightThemeBuilder(_ThemeColorLightRed());
break;
case CLThemeConstants.LIGHT_BLUE:
_themeData = _appLightThemeBuilder(_ThemeColorLightBlue());
break;
case CLThemeConstants.LIGHT_GRAY:
_themeData = _appLightThemeBuilder(_ThemeColorLightGray());
break;
case CLThemeConstants.DARK_ORANGE:
_themeData = _appDarkThemeBuilder(_ThemeColorDarkOrange());
break;
case CLThemeConstants.DARK_GREEN:
_themeData = _appDarkThemeBuilder(_ThemeColorDarkGreen());
break;
case CLThemeConstants.DARK_BROWN:
_themeData = _appDarkThemeBuilder(_ThemeColorDarkBrown());
break;
default:
_themeData = _appLightThemeBuilder(_ThemeColorLightRed());
}
}
currentTheme = themeConstants;
return _themeData;
}
ThemeData _appLightThemeBuilder(_ThemeColor baseTheme) {
Color primaryColor = baseTheme._primaryColor;
Color secondaryColor = baseTheme._primaryColorDark;
final ColorScheme colorScheme = const ColorScheme.light().copyWith(
primary: primaryColor,
secondary: secondaryColor,
);
final ThemeData base = ThemeData.light();
return base.copyWith(
colorScheme: colorScheme,
primaryColor: primaryColor,
buttonColor: primaryColor,
indicatorColor: Colors.white,
splashColor: Colors.white24,
splashFactory: InkRipple.splashFactory,
accentColor: secondaryColor,
canvasColor: Colors.white,
scaffoldBackgroundColor: Colors.white,
backgroundColor: Colors.white,
errorColor: const Color(0xFFB00020),
buttonTheme: ButtonThemeData(
colorScheme: colorScheme,
textTheme: ButtonTextTheme.primary,
),
textTheme: _buildTextTheme(base.textTheme),
primaryTextTheme: _buildTextTheme(base.primaryTextTheme),
accentTextTheme: _buildTextTheme(base.accentTextTheme),
);
}
ThemeData _appDarkThemeBuilder(_ThemeColor baseTheme) {
Color primaryColor = baseTheme._primaryColor;
Color secondaryColor = baseTheme._primaryColorDark;
final ThemeData base = ThemeData.dark();
final ColorScheme colorScheme = const ColorScheme.dark().copyWith(
primary: primaryColor,
secondary: secondaryColor,
);
return base.copyWith(
primaryColor: primaryColor,
buttonColor: primaryColor,
indicatorColor: Colors.white,
accentColor: secondaryColor,
canvasColor: const Color(0xFF202124),
scaffoldBackgroundColor: const Color(0xFF202124),
backgroundColor: const Color(0xFF202124),
errorColor: const Color(0xFFB00020),
buttonTheme: ButtonThemeData(
colorScheme: colorScheme,
textTheme: ButtonTextTheme.primary,
),
textTheme: _buildTextTheme(base.textTheme),
primaryTextTheme: _buildTextTheme(base.primaryTextTheme),
accentTextTheme: _buildTextTheme(base.accentTextTheme),
);
}
TextTheme _buildTextTheme(TextTheme base) {
return base.copyWith(
title: base.title.copyWith(
fontFamily: 'GoogleSans',
),
);
}
openThemePickerDialog(BuildContext context) {
_themeList = getThemeList();
double dialogWidth = MediaQuery.of(context).size.width;
bool isSelectedTheme;
return showDialog(
context: context,
builder: (BuildContext builderContext) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
contentPadding:
EdgeInsets.only(top: 10.0, right: 10, left: 10, bottom: 10),
content: Container(
width: dialogWidth * 0.6 > 300 ? dialogWidth * 0.6 : dialogWidth,
child: new GridView.builder(
itemCount: _themeList.length,
shrinkWrap: true,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 5),
itemBuilder: (BuildContext itemContext, int index) {
if (index == selectedTheme) {
isSelectedTheme = true;
} else
isSelectedTheme = false;
return InkWell(
onTap: () => changeTheme(itemContext, index),
child: Card(
elevation: 5,
shape: CircleBorder(),
child: Container(
alignment: Alignment.center,
decoration: ShapeDecoration(
shape: CircleBorder(),
gradient: _themeList[index].themeColors),
child: isSelectedTheme
? Icon(
Icons.done,
color: Colors.white,
)
: null,
),
),
);
},
),
),
);
});
}
changeTheme(BuildContext context, int selIndex) {
selectedTheme = selIndex;
Navigator.pop(context, _themeList[selIndex]);
}
List<CLThemeItemsDto> getThemeList() {
return [
CLThemeItemsDto(
'Light Red',
CLThemeConstants.LIGHT_RED,
LinearGradient(
colors: [Colors.red, Colors.blue],
),
false,
_ThemeColorDarkGreen()),
CLThemeItemsDto(
'Light Blue ',
CLThemeConstants.LIGHT_BLUE,
LinearGradient(colors: [Colors.blue, Colors.green]),
false,
_ThemeColorDarkGreen()),
CLThemeItemsDto(
'Light Black gray',
CLThemeConstants.LIGHT_GRAY,
LinearGradient(colors: [Colors.black, Colors.grey]),
false,
_ThemeColorDarkGreen()),
CLThemeItemsDto(
'Dark Orange Blue',
CLThemeConstants.DARK_ORANGE,
LinearGradient(
colors: [Colors.deepOrangeAccent, Colors.orangeAccent]),
false,
_ThemeColorDarkGreen()),
CLThemeItemsDto(
'Dark Green ',
CLThemeConstants.DARK_GREEN,
LinearGradient(colors: [Colors.green, Colors.lightGreen]),
false,
_ThemeColorDarkGreen()),
CLThemeItemsDto(
'Dark brown',
CLThemeConstants.DARK_BROWN,
LinearGradient(colors: [Colors.brown, Colors.amber]),
false,
_ThemeColorDarkGreen())
];
}
}
//class _ThemeColorLightBlue implements _ThemeColor {
// Color _toolBarColor = Colors.blue;
//}
class _ThemeColorLightRed implements _ThemeColor {
@override
Color _primaryColor = Colors.red;
@override
Color _primaryColorDark = Colors.blue;
@override
Color _primaryColorLight = Color(0xffff686b);
@override
Color _toolBarColor = Color(0xffff686b);
}
class _ThemeColorLightBlue implements _ThemeColor {
@override
Color _primaryColor = Colors.blueAccent;
@override
Color _primaryColorDark = Colors.lightGreen;
@override
Color _primaryColorLight = Color(0xffff686b);
@override
Color _toolBarColor = Color(0xffff686b);
}
class _ThemeColorLightGray implements _ThemeColor {
@override
Color _toolBarColor = Colors.black;
@override
Color _primaryColor = Colors.grey;
@override
Color _primaryColorDark = Color(0xffff686b);
@override
Color _primaryColorLight = Color(0xffff686b);
}
class _ThemeColorDarkOrange implements _ThemeColor {
@override
Color _toolBarColor = Colors.orange;
@override
Color _primaryColor = Colors.deepOrangeAccent;
@override
Color _primaryColorDark = Color(0xffff686b);
@override
Color _primaryColorLight = Color(0xffff686b);
}
class _ThemeColorDarkGreen implements _ThemeColor {
@override
Color _primaryColor = Colors.green;
@override
Color _primaryColorDark = Colors.lightGreen;
@override
Color _primaryColorLight = Color(0xffff686b);
@override
Color _toolBarColor = Color(0xffff686b);
}
class _ThemeColorDarkBrown implements _ThemeColor {
@override
Color _primaryColor = Colors.brown;
@override
Color _primaryColorDark = Colors.yellowAccent;
@override
Color _primaryColorLight = Color(0xffff686b);
@override
Color _toolBarColor = Color(0xffff686b);
}
// AppColors.blue[500]
class AppColors {
AppColors._(); // this basically makes it so you can instantiate this class
static const int _deepPurplePrimaryValue = 0xFF673AB7;
static const MaterialColor green = MaterialColor(
_deepPurplePrimaryValue,
<int, Color>{
50: Color(0x25df7d35),
100: Color(0x40df7d35),
200: Color(0x70df7d35),
300: Color(0xa5df7d35),
400: Color(0xbFdf7d35),
500: Color(0xc0df7d35),
600: Color(0xd0df7d35),
700: Color(0xe0df7d35),
800: Color(0xF0df7d35),
900: Color(0xFFdf7d35),
},
);
}
List<CLThemeItemsDto> _themeList;
int selectedTheme = 0;
abstract class _ThemeColor {
Color _toolBarColor;
// Color _brightness;
Color _primaryColor;
// Color _primaryColorBrightness;
Color _primaryColorLight;
Color _primaryColorDark;
// Color _canvasColor;
// Color _accentColor;
// Color _accentColorBrightness;
// Color _scaffoldBackgroundColor;
// Color _bottomAppBarColor;
// Color _cardColor;
// Color _dividerColor;
// Color _highlightColor;
// Color _splashColor;
//
// Color _splashFactory;
// Color _selectedRowColor;
// Color _unselectedWidgetColor;
// Color _disabledColor;
// Color _buttonColor;
// Color _secondaryHeaderColor;
// Color _textSelectionColor;
// Color _cursorColor;
// Color _textSelectionHandleColor;
// Color _backgroundColor;
// Color _dialogBackgroundColor;
// Color _indicatorColor;
// Color _hintColor;
// Color _errorColor;
// Color _toggleableActiveColor;
//
// ButtonThemeData _buttonTheme();
// Color _textTheme();
// Color _primaryTextTheme();
// Color _accentTextTheme();
// Color _inputDecorationTheme();
// Color _iconTheme();
// Color _primaryIconTheme();
// Color _accentIconTheme();
// Color _sliderTheme();
// Color _tabBarTheme();
// Color _cardTheme();
// Color _chipTheme();
// Color _platform;
// Color _materialTapTargetSize;
// Color _pageTransitionsTheme();
// Color _appBarTheme();
// Color _bottomAppBarTheme();
// Color _colorScheme;
// Color _dialogTheme();
// Color _typography;
// Color _cupertinoOverrideThem;
// Color changeAlpha(Color color, int alpha);
}
_CLThemeListOption themeController = _CLThemeListOption();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment