Skip to content

Commit bcf1b16

Browse files
committed
Fixes dark mode theming.
1 parent 1f0c53a commit bcf1b16

File tree

1 file changed

+62
-23
lines changed

1 file changed

+62
-23
lines changed

example/lib/main.dart

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,34 @@ const double _fontSize = 21.0;
4141

4242
void main() => runApp(MyApp());
4343

44-
class MyApp extends StatefulWidget {
44+
class MyApp extends StatelessWidget {
4545
@override
46-
_MyAppState createState() => _MyAppState();
46+
Widget build(BuildContext context) {
47+
if (Theme.of(context).platform == TargetPlatform.iOS) {
48+
return CupertinoApp(home: HomePage());
49+
} else {
50+
return MaterialApp(home: HomePage());
51+
}
52+
}
4753
}
4854

49-
class _MyAppState extends State<MyApp> {
55+
class HomePage extends StatefulWidget {
56+
@override
57+
_HomePageState createState() => _HomePageState();
58+
}
59+
60+
class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
61+
static final ThemeData lightTheme = ThemeData(
62+
backgroundColor: Colors.transparent,
63+
primaryColor: Colors.black
64+
);
65+
66+
static final ThemeData darkTheme = ThemeData(
67+
backgroundColor: Colors.grey[900],
68+
primaryColor: Colors.white
69+
);
5070
String _frameworkVersion = '';
71+
ThemeData currentTheme = lightTheme;
5172

5273
Future<File> extractAsset(String assetPath) async {
5374
final ByteData bytes = await DefaultAssetBundle.of(context).load(assetPath);
@@ -200,9 +221,25 @@ class _MyAppState extends State<MyApp> {
200221
@override
201222
initState() {
202223
super.initState();
224+
WidgetsBinding.instance.addObserver(this);
203225
initPlatformState();
204226
}
205227

228+
@override
229+
void dispose() {
230+
WidgetsBinding.instance.removeObserver(this);
231+
super.dispose();
232+
}
233+
234+
@override
235+
void didChangePlatformBrightness() {
236+
currentTheme = WidgetsBinding.instance.window.platformBrightness == Brightness.light ? lightTheme : darkTheme;
237+
setState(() {
238+
build(context);
239+
});
240+
super.didChangePlatformBrightness();
241+
}
242+
206243
String frameworkVersion() {
207244
return '$_pspdfkitFor $_frameworkVersion\n';
208245
}
@@ -232,17 +269,19 @@ class _MyAppState extends State<MyApp> {
232269
@override
233270
Widget build(BuildContext context) {
234271
final ThemeData themeData = Theme.of(context);
272+
currentTheme = MediaQuery.of(context).platformBrightness == Brightness.light ? lightTheme : darkTheme;
235273
bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
236274
if (isIOS) {
237-
var title = themeData.textTheme.title;
238-
var subhead = themeData.textTheme.subhead;
275+
var title = themeData.textTheme.title.copyWith(color: currentTheme.primaryColor);
276+
var subhead = themeData.textTheme.subhead.copyWith(color: currentTheme.primaryColor);
239277
var crossAxisAlignment = CrossAxisAlignment.start;
240278
var padding = EdgeInsets.all(16.0);
279+
241280
List<Widget> cupertinoListTiles = <Widget>[
242-
Divider(),
243281
GestureDetector(
244282
onTap: showDocument,
245283
child: Container(
284+
color: currentTheme.backgroundColor,
246285
padding: padding,
247286
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
248287
Text(_basicExample, style: title),
@@ -253,6 +292,7 @@ class _MyAppState extends State<MyApp> {
253292
GestureDetector(
254293
onTap: showImage,
255294
child: Container(
295+
color: currentTheme.backgroundColor,
256296
padding: padding,
257297
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
258298
Text(_imageDocument, style: title),
@@ -263,6 +303,7 @@ class _MyAppState extends State<MyApp> {
263303
GestureDetector(
264304
onTap: applyDarkTheme,
265305
child: Container(
306+
color: currentTheme.backgroundColor,
266307
padding: padding,
267308
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
268309
Text(_darkTheme, style: title),
@@ -273,9 +314,9 @@ class _MyAppState extends State<MyApp> {
273314
GestureDetector(
274315
onTap: applyCustomConfiguration,
275316
child: Container(
317+
color: currentTheme.backgroundColor,
276318
padding: padding,
277319
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
278-
279320
Text(_customConfiguration, style: title),
280321
Text(_customConfigurationSub, style: subhead)
281322
])),
@@ -284,9 +325,9 @@ class _MyAppState extends State<MyApp> {
284325
GestureDetector(
285326
onTap: unlockPasswordProtectedDocument,
286327
child: Container(
328+
color: currentTheme.backgroundColor,
287329
padding: padding,
288330
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
289-
290331
Text(_passwordProtectedDocument, style: title),
291332
Text(_passwordProtectedDocumentSub, style: subhead)
292333
])),
@@ -295,9 +336,9 @@ class _MyAppState extends State<MyApp> {
295336
GestureDetector(
296337
onTap: showFormDocumentExample,
297338
child: Container(
339+
color: currentTheme.backgroundColor,
298340
padding: padding,
299341
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
300-
301342
Text(_formExample, style: title),
302343
Text(_formExampleSub, style: subhead)
303344
])),
@@ -306,22 +347,22 @@ class _MyAppState extends State<MyApp> {
306347
GestureDetector(
307348
onTap: importInstantJsonExample,
308349
child: Container(
350+
color: currentTheme.backgroundColor,
309351
padding: padding,
310352
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
311-
312353
Text(_importInstantJsonExample, style: title),
313354
Text(_importInstantJsonExampleSub, style: subhead)
314355
])),
315356
),
316357
Divider()
317358
];
318-
return CupertinoApp(
319-
home: CupertinoPageScaffold(
359+
return CupertinoPageScaffold(
320360
navigationBar: CupertinoNavigationBar(
321-
middle: Text(_pspdfkitFlutterPluginTitle,
322-
style: themeData.textTheme.title)),
323-
child: ExampleListView(
324-
themeData, frameworkVersion(), cupertinoListTiles)));
361+
middle: Text(_pspdfkitFlutterPluginTitle)),
362+
child: SafeArea(
363+
bottom: false,
364+
child: ExampleListView(themeData, frameworkVersion(), cupertinoListTiles))
365+
);
325366
} else {
326367
List<Widget> listTiles = <Widget>[
327368
ListTile(
@@ -360,13 +401,10 @@ class _MyAppState extends State<MyApp> {
360401
onTap: () => importInstantJsonExample()),
361402
Divider(),
362403
];
363-
return MaterialApp(
364-
home: Scaffold(
365-
appBar: AppBar(
366-
title: Text(_pspdfkitFlutterPluginTitle),
367-
),
368-
body: ExampleListView(themeData, frameworkVersion(), listTiles)),
369-
);
404+
return Scaffold(
405+
appBar: AppBar(title: Text(_pspdfkitFlutterPluginTitle)),
406+
body: ExampleListView(themeData, frameworkVersion(), listTiles)
407+
);
370408
}
371409
}
372410
}
@@ -382,6 +420,7 @@ class ExampleListView extends StatelessWidget {
382420
Widget build(BuildContext buildContext) {
383421
return Column(mainAxisSize: MainAxisSize.max, children: [
384422
Container(
423+
color: Colors.grey[300],
385424
padding: EdgeInsets.only(top: 24),
386425
child: Center(
387426
child: Text(_frameworkVersion,

0 commit comments

Comments
 (0)