Skip to content

Instantly share code, notes, and snippets.

View chaudharydeepanshu's full-sized avatar
🎯
New Journey

Deepanshu Chaudhary chaudharydeepanshu

🎯
New Journey
View GitHub Profile
import 'package:flutter/material.dart';
class CustomDrawer extends StatefulWidget {
const CustomDrawer({
Key? key,
required this.themeMode,
required this.onThemeMode,
}) : super(key: key);
final ThemeMode? themeMode;
final ValueChanged<ThemeMode>? onThemeMode;
class _MyAppState extends State<MyApp> {
//Keeps track of theme
late ThemeMode? themeMode = widget.savedThemeMode;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
//Add themeMode as themeMode
class MyApp extends StatefulWidget {
const MyApp({Key? key, this.savedThemeMode}) : super(key: key);
final ThemeMode? savedThemeMode;
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
class MyApp extends StatelessWidget {
const MyApp({Key? key, this.savedThemeMode}) : super(key: key);
final ThemeMode? savedThemeMode;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
import 'package:flutter/material.dart';
import 'package:flutter_dark_mode_demo/sharedPreferences/app_theme_shared_preferences.dart';
Future<void> main() async {
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
int? retrieveSavedThemeModeIndex = await retrieveSavedThemeMode();
final ThemeMode savedThemeMode = retrieveSavedThemeModeIndex == 0
? ThemeMode.system
: retrieveSavedThemeModeIndex == 1
? ThemeMode.light
import 'package:shared_preferences/shared_preferences.dart';
Future<int?> retrieveSavedThemeMode() async {
final prefs = await SharedPreferences.getInstance();
// Check where the name is saved before or not
if (!prefs.containsKey('savedThemeModeIndex')) {
//if not return zero for ThemeMode.System
return 0;
}
//else return the saved theme
Dart 18 hrs 40 mins ███████████████████▊░ 94.5%
TypeScript 31 mins ▌░░░░░░░░░░░░░░░░░░░░ 2.6%
Ezhil 13 mins ▏░░░░░░░░░░░░░░░░░░░░ 1.2%
JavaScript 10 mins ▏░░░░░░░░░░░░░░░░░░░░ 0.9%
YAML 7 mins ▏░░░░░░░░░░░░░░░░░░░░ 0.7%
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
// Platform messages are asynchronous, so we initialize in an async method.
Future<ConnectivityResult?> initConnectivity(
{required Connectivity connectivity}) async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
return await connectivity.checkConnectivity();
<manifest>
<application>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
</application>
</manifest>
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final initFuture = MobileAds.instance.initialize();
final adState = AdState(initFuture);
runApp(Provider.value(
value: adState,
builder: (context, child) => const MyApp(),
));
}