Skip to content

Instantly share code, notes, and snippets.

@ahmetgungor
Created October 21, 2021 09:40
Show Gist options
  • Save ahmetgungor/58e3e4929e3da9639a7835f4fef8ecac to your computer and use it in GitHub Desktop.
Save ahmetgungor/58e3e4929e3da9639a7835f4fef8ecac to your computer and use it in GitHub Desktop.
Flutter Dark Mode switch
pubspec.yaml ekle
shared_preferences: ^2.0.8
provider: ^6.0.1
== dark_theme_preferences.dart ========================================================================================================================
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:ui';
class DarkThemePreferences
{
static const themeStatus = "THEMESTATUS";
setDarkTheme(bool value) async{
SharedPreferences pref = await SharedPreferences.getInstance();
pref.setBool(themeStatus, value);
}
Future<bool> getTheme() async{
SharedPreferences pref = await SharedPreferences.getInstance();
return pref.getBool(themeStatus)?? false;
}
}
class DarkThemeProvider with ChangeNotifier
{
DarkThemePreferences darkThemePreferences = DarkThemePreferences();
bool _darkTheme =false;
bool get darkTheme => _darkTheme;
set darkTheme(bool value){
_darkTheme = value;
darkThemePreferences.setDarkTheme(value);
notifyListeners();
}
}
class Style1
{
static ThemeData themeData(bool isDarkTheme,BuildContext context)
{
return ThemeData(
primarySwatch: Colors.red,
primaryColor: isDarkTheme ? Colors.black : Colors.white,
backgroundColor: isDarkTheme ? Colors.black : const Color(0xffF1F5FB),
indicatorColor: isDarkTheme ? const Color(0xff0E1D36) : const Color(0xffCBDCF8),
//button: isDarkTheme ? Color(0xff3B3B3B) : Color(0xffF1F5FB),
hintColor: isDarkTheme ? const Color(0xff280C0B) : const Color(0xffEECED3),
highlightColor: isDarkTheme ? const Color(0xff372901) : const Color(0xffFCE192),
hoverColor: isDarkTheme ? const Color(0xff3A3A3B) : const Color(0xff4285F4),
focusColor: isDarkTheme ? const Color(0xff0B2512) : const Color(0xffA8DAB5),
disabledColor: Colors.grey,
// textSelectionColor: isDarkTheme ? Colors.white : Colors.black,
cardColor: isDarkTheme ? const Color(0xFF151515) : Colors.white,
canvasColor: isDarkTheme ? Colors.black : Colors.grey[50],
brightness: isDarkTheme ? Brightness.dark : Brightness.light,
buttonTheme: Theme.of(context).buttonTheme.copyWith(
colorScheme: isDarkTheme ? const ColorScheme.dark() : const ColorScheme.light()),
appBarTheme: const AppBarTheme(
elevation: 0.0,
),
);
}
}
Main.dart ===================================================================================================================
import 'package:finjan/pages/register_page.dart';
import 'package:finjan/theme/dark_theme_preferences.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
DarkThemeProvider themeChangeProvider = DarkThemeProvider();
@override
void initState() {
super.initState();
getCurrentAppTheme();
}
void getCurrentAppTheme() async {
themeChangeProvider.darkTheme =
await themeChangeProvider.darkThemePreferences.getTheme();
}
@override
Widget build(BuildContext context) {
return
ChangeNotifierProvider(create: (_){return themeChangeProvider;},
child:Consumer<DarkThemeProvider>(
builder: (BuildContext context, value, child){
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'App Name',
theme: Style1.themeData(themeChangeProvider.darkTheme, context),
home: const RegisterPage(),
);
}),
);
}
}
Register.dart========================================================================================
import 'package:finjan/theme/dark_theme_preferences.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class RegisterPage extends StatefulWidget {
const RegisterPage({Key? key}) : super(key: key);
@override
RegisterPageState createState() => RegisterPageState();
}
class RegisterPageState extends State<RegisterPage> {
DarkThemeProvider themeChangeProvider = DarkThemeProvider();
@override
Widget build(BuildContext context) {
final themeChange = Provider.of<DarkThemeProvider>(context);
final form = Form(
child: Column(
mainAxisAlignment:MainAxisAlignment.center ,
children: [
Checkbox(value: themeChange.darkTheme, onChanged: (bool? value){
themeChange.darkTheme = value!;
}),
Text(themeChange.darkTheme.toString())
],
),
);
return Padding(padding: const EdgeInsets.only(left: 20,right: 20),
child: Center(child: Scaffold(body: form,),),);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment