Skip to content

Commit 4df3c21

Browse files
authored
Merge pull request #69 from PSPDFKit/akshat/fix-dark-mode
Fixes dark mode theming.
2 parents 1f0c53a + e69151e commit 4df3c21

File tree

1 file changed

+74
-33
lines changed

1 file changed

+74
-33
lines changed

example/lib/main.dart

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,40 @@ const String _formExampleSub = 'Programmatically set and get the value of a form
3737
const String _importInstantJsonExample = 'Import Instant Document JSON';
3838
const String _importInstantJsonExampleSub = 'Shows how to programmatically import Instant Document JSON.';
3939
const String _pspdfkitFor = 'PSPDFKit for';
40-
const double _fontSize = 21.0;
40+
const double _fontSize = 18.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+
}
53+
}
54+
55+
class HomePage extends StatefulWidget {
56+
@override
57+
_HomePageState createState() => _HomePageState();
4758
}
4859

49-
class _MyAppState extends State<MyApp> {
60+
class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
61+
static final ThemeData lightTheme = ThemeData(
62+
backgroundColor: Colors.transparent,
63+
primaryColor: Colors.black,
64+
dividerColor: Colors.grey[400]
65+
);
66+
67+
static final ThemeData darkTheme = ThemeData(
68+
backgroundColor: Colors.transparent,
69+
primaryColor: Colors.white,
70+
dividerColor: Colors.grey[800]
71+
);
5072
String _frameworkVersion = '';
73+
ThemeData currentTheme = lightTheme;
5174

5275
Future<File> extractAsset(String assetPath) async {
5376
final ByteData bytes = await DefaultAssetBundle.of(context).load(assetPath);
@@ -200,9 +223,25 @@ class _MyAppState extends State<MyApp> {
200223
@override
201224
initState() {
202225
super.initState();
226+
WidgetsBinding.instance.addObserver(this);
203227
initPlatformState();
204228
}
205229

230+
@override
231+
void dispose() {
232+
WidgetsBinding.instance.removeObserver(this);
233+
super.dispose();
234+
}
235+
236+
@override
237+
void didChangePlatformBrightness() {
238+
currentTheme = WidgetsBinding.instance.window.platformBrightness == Brightness.light ? lightTheme : darkTheme;
239+
setState(() {
240+
build(context);
241+
});
242+
super.didChangePlatformBrightness();
243+
}
244+
206245
String frameworkVersion() {
207246
return '$_pspdfkitFor $_frameworkVersion\n';
208247
}
@@ -231,97 +270,101 @@ class _MyAppState extends State<MyApp> {
231270

232271
@override
233272
Widget build(BuildContext context) {
234-
final ThemeData themeData = Theme.of(context);
273+
currentTheme = MediaQuery.of(context).platformBrightness == Brightness.light ? lightTheme : darkTheme;
235274
bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
236275
if (isIOS) {
237-
var title = themeData.textTheme.title;
238-
var subhead = themeData.textTheme.subhead;
276+
var title = Theme.of(context).textTheme.title.copyWith(color: currentTheme.primaryColor);
277+
var subhead = Theme.of(context).textTheme.subhead.copyWith(color: currentTheme.primaryColor);
239278
var crossAxisAlignment = CrossAxisAlignment.start;
240279
var padding = EdgeInsets.all(16.0);
280+
241281
List<Widget> cupertinoListTiles = <Widget>[
242-
Divider(),
282+
Divider(color: currentTheme.dividerColor),
243283
GestureDetector(
244284
onTap: showDocument,
245285
child: Container(
286+
color: currentTheme.backgroundColor,
246287
padding: padding,
247288
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
248289
Text(_basicExample, style: title),
249290
Text(_basicExampleSub, style: subhead)
250291
])),
251292
),
252-
Divider(),
293+
Divider(color: currentTheme.dividerColor),
253294
GestureDetector(
254295
onTap: showImage,
255296
child: Container(
297+
color: currentTheme.backgroundColor,
256298
padding: padding,
257299
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
258300
Text(_imageDocument, style: title),
259301
Text(_imageDocumentSub, style: subhead)
260302
])),
261303
),
262-
Divider(),
304+
Divider(color: currentTheme.dividerColor),
263305
GestureDetector(
264306
onTap: applyDarkTheme,
265307
child: Container(
308+
color: currentTheme.backgroundColor,
266309
padding: padding,
267310
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
268311
Text(_darkTheme, style: title),
269312
Text(_darkThemeSub, style: subhead)
270313
])),
271314
),
272-
Divider(),
315+
Divider(color: currentTheme.dividerColor),
273316
GestureDetector(
274317
onTap: applyCustomConfiguration,
275318
child: Container(
319+
color: currentTheme.backgroundColor,
276320
padding: padding,
277321
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
278-
279322
Text(_customConfiguration, style: title),
280323
Text(_customConfigurationSub, style: subhead)
281324
])),
282325
),
283-
Divider(),
326+
Divider(color: currentTheme.dividerColor),
284327
GestureDetector(
285328
onTap: unlockPasswordProtectedDocument,
286329
child: Container(
330+
color: currentTheme.backgroundColor,
287331
padding: padding,
288332
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
289-
290333
Text(_passwordProtectedDocument, style: title),
291334
Text(_passwordProtectedDocumentSub, style: subhead)
292335
])),
293336
),
294-
Divider(),
337+
Divider(color: currentTheme.dividerColor),
295338
GestureDetector(
296339
onTap: showFormDocumentExample,
297340
child: Container(
341+
color: currentTheme.backgroundColor,
298342
padding: padding,
299343
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
300-
301344
Text(_formExample, style: title),
302345
Text(_formExampleSub, style: subhead)
303346
])),
304347
),
305-
Divider(),
348+
Divider(color: currentTheme.dividerColor),
306349
GestureDetector(
307350
onTap: importInstantJsonExample,
308351
child: Container(
352+
color: currentTheme.backgroundColor,
309353
padding: padding,
310354
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
311-
312355
Text(_importInstantJsonExample, style: title),
313356
Text(_importInstantJsonExampleSub, style: subhead)
314357
])),
315358
),
316-
Divider()
359+
Divider(color: currentTheme.dividerColor)
317360
];
318-
return CupertinoApp(
319-
home: CupertinoPageScaffold(
361+
return CupertinoPageScaffold(
320362
navigationBar: CupertinoNavigationBar(
321-
middle: Text(_pspdfkitFlutterPluginTitle,
322-
style: themeData.textTheme.title)),
323-
child: ExampleListView(
324-
themeData, frameworkVersion(), cupertinoListTiles)));
363+
middle: Text(_pspdfkitFlutterPluginTitle)),
364+
child: SafeArea(
365+
bottom: false,
366+
child: ExampleListView(currentTheme, frameworkVersion(), cupertinoListTiles))
367+
);
325368
} else {
326369
List<Widget> listTiles = <Widget>[
327370
ListTile(
@@ -360,13 +403,10 @@ class _MyAppState extends State<MyApp> {
360403
onTap: () => importInstantJsonExample()),
361404
Divider(),
362405
];
363-
return MaterialApp(
364-
home: Scaffold(
365-
appBar: AppBar(
366-
title: Text(_pspdfkitFlutterPluginTitle),
367-
),
368-
body: ExampleListView(themeData, frameworkVersion(), listTiles)),
369-
);
406+
return Scaffold(
407+
appBar: AppBar(title: Text(_pspdfkitFlutterPluginTitle)),
408+
body: ExampleListView(currentTheme, frameworkVersion(), listTiles)
409+
);
370410
}
371411
}
372412
}
@@ -382,11 +422,12 @@ class ExampleListView extends StatelessWidget {
382422
Widget build(BuildContext buildContext) {
383423
return Column(mainAxisSize: MainAxisSize.max, children: [
384424
Container(
425+
color: Colors.transparent,
385426
padding: EdgeInsets.only(top: 24),
386427
child: Center(
387428
child: Text(_frameworkVersion,
388429
style: _themeData.textTheme.display1
389-
.copyWith(fontSize: _fontSize, fontWeight: FontWeight.bold)),
430+
.copyWith(fontSize: _fontSize, fontWeight: FontWeight.bold, color: _themeData.primaryColor)),
390431
),
391432
),
392433
Expanded(

0 commit comments

Comments
 (0)