Last active
August 9, 2021 16:38
-
-
Save IsmailAlamKhan/805760f1443295e5b4e97fee18801564 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Material App', | |
builder: (context, child) { | |
if (child == null) return const SizedBox.shrink(); | |
final brightness = Theme.of(context).brightness; | |
IsmailThemeData theme; | |
if (brightness == Brightness.dark) { | |
theme = IsmailThemeData.dark(); | |
} else { | |
theme = IsmailThemeData.light(); | |
} | |
return IsmailTheme( | |
data: theme, | |
child: child, | |
); | |
}, | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatelessWidget { | |
const Home({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: IsmailTheme.of(context).backgroundColor, | |
appBar: AppBar( | |
title: Text('Material App Bar'), | |
), | |
body: Center( | |
child: Container( | |
child: Text('Hello World'), | |
), | |
), | |
); | |
} | |
} | |
class IsmailThemeData { | |
final Color backgroundColor; | |
IsmailThemeData({required this.backgroundColor}); | |
factory IsmailThemeData.dark() => | |
IsmailThemeData(backgroundColor: Colors.green); | |
factory IsmailThemeData.light() => | |
IsmailThemeData(backgroundColor: Colors.blue); | |
@override | |
bool operator ==(Object other) { | |
if (identical(this, other)) return true; | |
return other is IsmailThemeData && | |
other.backgroundColor == backgroundColor; | |
} | |
@override | |
int get hashCode => backgroundColor.hashCode; | |
IsmailThemeData copyWith({ | |
Color? backgroundColor, | |
}) { | |
return IsmailThemeData( | |
backgroundColor: backgroundColor ?? this.backgroundColor, | |
); | |
} | |
} | |
class IsmailTheme extends StatelessWidget { | |
const IsmailTheme({ | |
Key? key, | |
required this.child, | |
required this.data, | |
}) : super(key: key); | |
final IsmailThemeData data; | |
final Widget child; | |
static IsmailThemeData of(BuildContext context) => | |
context.dependOnInheritedWidgetOfExactType<_IsmailThemeScope>()!.theme; | |
@override | |
Widget build(BuildContext context) { | |
return _IsmailThemeScope( | |
theme: data, | |
child: child, | |
); | |
} | |
} | |
class _IsmailThemeScope extends InheritedWidget { | |
const _IsmailThemeScope({ | |
Key? key, | |
required Widget child, | |
required this.theme, | |
}) : super(key: key, child: child); | |
final IsmailThemeData theme; | |
@override | |
bool updateShouldNotify(_IsmailThemeScope oldWidget) => | |
theme != oldWidget.theme; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment