Skip to content

Commit b233e52

Browse files
authored
Merge pull request #737 from timbotimbo/global_controller_nullsafe
[feat global_unity_controller] Update examples to null safety
2 parents c72e363 + 0f2c80e commit b233e52

File tree

10 files changed

+149
-120
lines changed

10 files changed

+149
-120
lines changed

README.md

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -434,40 +434,41 @@ Unable to find a matching variant of project :unityLibrary:
434434

435435
```dart
436436
import 'package:flutter/material.dart';
437-
import 'package:flutter/services.dart';
438437
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
439438
440439
void main() {
441-
runApp(MaterialApp(
442-
home: UnityDemoScreen()
443-
));
440+
runApp(
441+
const MaterialApp(
442+
home: UnityDemoScreen(),
443+
),
444+
);
444445
}
445446
446447
class UnityDemoScreen extends StatefulWidget {
447-
448-
UnityDemoScreen({Key key}) : super(key: key);
448+
const UnityDemoScreen({Key? key}) : super(key: key);
449449
450450
@override
451-
_UnityDemoScreenState createState() => _UnityDemoScreenState();
451+
State<UnityDemoScreen> createState() => _UnityDemoScreenState();
452452
}
453453
454-
class _UnityDemoScreenState extends State<UnityDemoScreen>{
454+
class _UnityDemoScreenState extends State<UnityDemoScreen> {
455455
static final GlobalKey<ScaffoldState> _scaffoldKey =
456456
GlobalKey<ScaffoldState>();
457-
UnityWidgetController _unityWidgetController;
457+
UnityWidgetController? _unityWidgetController;
458458
459+
@override
459460
Widget build(BuildContext context) {
460-
461461
return Scaffold(
462462
key: _scaffoldKey,
463463
body: SafeArea(
464464
bottom: false,
465465
child: WillPopScope(
466-
onWillPop: () {
466+
onWillPop: () async {
467467
// Pop the category page if Android back button is pressed.
468+
return true;
468469
},
469470
child: Container(
470-
color: colorYellow,
471+
color: Colors.yellow,
471472
child: UnityWidget(
472473
onUnityCreated: onUnityCreated,
473474
),
@@ -479,9 +480,10 @@ class _UnityDemoScreenState extends State<UnityDemoScreen>{
479480
480481
// Callback that connects the created controller to the unity controller
481482
void onUnityCreated(controller) {
482-
this._unityWidgetController = controller;
483+
_unityWidgetController = controller;
483484
}
484485
}
486+
485487
```
486488
<br />
487489

@@ -491,17 +493,19 @@ class _UnityDemoScreenState extends State<UnityDemoScreen>{
491493
import 'package:flutter/material.dart';
492494
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
493495
494-
void main() => runApp(MyApp());
496+
void main() => runApp(const MyApp());
495497
496498
class MyApp extends StatefulWidget {
499+
const MyApp({Key? key}) : super(key: key);
500+
497501
@override
498-
_MyAppState createState() => _MyAppState();
502+
State<MyApp> createState() => _MyAppState();
499503
}
500504
501505
class _MyAppState extends State<MyApp> {
502506
static final GlobalKey<ScaffoldState> _scaffoldKey =
503507
GlobalKey<ScaffoldState>();
504-
UnityWidgetController _unityWidgetController;
508+
UnityWidgetController? _unityWidgetController;
505509
double _sliderValue = 0.0;
506510
507511
@override
@@ -526,10 +530,10 @@ class _MyAppState extends State<MyApp> {
526530
child: Stack(
527531
children: <Widget>[
528532
UnityWidget(
529-
onUnityCreated: onUnityCreated,
530-
onUnityMessage: onUnityMessage,
531-
onUnitySceneLoaded: onUnitySceneLoaded,
532-
fullscreen: false,
533+
onUnityCreated: onUnityCreated,
534+
onUnityMessage: onUnityMessage,
535+
onUnitySceneLoaded: onUnitySceneLoaded,
536+
fullscreen: false,
533537
),
534538
Positioned(
535539
bottom: 20,
@@ -539,8 +543,8 @@ class _MyAppState extends State<MyApp> {
539543
elevation: 10,
540544
child: Column(
541545
children: <Widget>[
542-
Padding(
543-
padding: const EdgeInsets.only(top: 20),
546+
const Padding(
547+
padding: EdgeInsets.only(top: 20),
544548
child: Text("Rotation speed:"),
545549
),
546550
Slider(
@@ -567,7 +571,7 @@ class _MyAppState extends State<MyApp> {
567571
568572
// Communcation from Flutter to Unity
569573
void setRotationSpeed(String speed) {
570-
_unityWidgetController.postMessage(
574+
_unityWidgetController?.postMessage(
571575
'Cube',
572576
'SetRotationSpeed',
573577
speed,
@@ -581,15 +585,17 @@ class _MyAppState extends State<MyApp> {
581585
582586
// Callback that connects the created controller to the unity controller
583587
void onUnityCreated(controller) {
584-
this._unityWidgetController = controller;
588+
_unityWidgetController = controller;
585589
}
586590
587591
// Communication from Unity when new scene is loaded to Flutter
588-
void onUnitySceneLoaded(SceneLoaded sceneInfo) {
589-
print('Received scene loaded from unity: ${sceneInfo.name}');
590-
print('Received scene loaded from unity buildIndex: ${sceneInfo.buildIndex}');
592+
void onUnitySceneLoaded(SceneLoaded? sceneInfo) {
593+
if (sceneInfo != null) {
594+
print('Received scene loaded from unity: ${sceneInfo.name}');
595+
print(
596+
'Received scene loaded from unity buildIndex: ${sceneInfo.buildIndex}');
597+
}
591598
}
592-
593599
}
594600
595601
```

example/lib/main.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import 'screens/loader_screen.dart';
88
import 'screens/simple_screen.dart';
99

1010
void main() {
11-
runApp(MyApp());
11+
runApp(const MyApp());
1212
}
1313

1414
class MyApp extends StatelessWidget {
15+
const MyApp({Key? key}) : super(key: key);
16+
1517
// This widget is the root of your application.
1618
@override
1719
Widget build(BuildContext context) {
@@ -23,12 +25,12 @@ class MyApp extends StatelessWidget {
2325
),
2426
initialRoute: '/',
2527
routes: {
26-
'/': (context) => MenuScreen(),
27-
'/simple': (context) => SimpleScreen(),
28-
'/loader': (context) => LoaderScreen(),
29-
'/orientation': (context) => OrientationScreen(),
30-
'/api': (context) => ApiScreen(),
31-
'/none': (context) => NoInteractionScreen(),
28+
'/': (context) => const MenuScreen(),
29+
'/simple': (context) => const SimpleScreen(),
30+
'/loader': (context) => const LoaderScreen(),
31+
'/orientation': (context) => const OrientationScreen(),
32+
'/api': (context) => const ApiScreen(),
33+
'/none': (context) => const NoInteractionScreen(),
3234
},
3335
);
3436
}

example/lib/menu_screen.dart

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
import 'package:flutter/material.dart';
22

33
class MenuScreen extends StatefulWidget {
4-
MenuScreen({Key key}) : super(key: key);
4+
const MenuScreen({Key? key}) : super(key: key);
55

66
@override
7-
_MenuScreenState createState() => _MenuScreenState();
7+
State<MenuScreen> createState() => _MenuScreenState();
88
}
99

1010
class _MenuScreenState extends State<MenuScreen> {
1111
bool enableAR = true;
1212

1313
List<_MenuListItem> menus = [
14-
new _MenuListItem(
14+
_MenuListItem(
1515
description: 'Simple demonstration of unity flutter library',
1616
route: '/simple',
1717
title: 'Simple Unity Demo',
1818
enableAR: false,
1919
),
20-
new _MenuListItem(
20+
_MenuListItem(
2121
description: 'No interaction of unity flutter library',
2222
route: '/none',
2323
title: 'No Interaction Unity Demo',
2424
enableAR: false,
2525
),
26-
new _MenuListItem(
26+
_MenuListItem(
2727
description: 'Unity load and unload unity demo',
2828
route: '/loader',
2929
title: 'Safe mode Demo',
3030
enableAR: false,
3131
),
32-
new _MenuListItem(
32+
_MenuListItem(
3333
description:
3434
'This example shows various native API exposed by the library',
3535
route: '/api',
3636
title: 'Native exposed API demo',
3737
enableAR: false,
3838
),
39-
new _MenuListItem(
39+
_MenuListItem(
4040
title: 'Test Orientation',
4141
route: '/orientation',
4242
description: 'test orientation change',
4343
enableAR: false,
4444
),
45-
new _MenuListItem(
45+
_MenuListItem(
4646
description: 'Unity native activity demo',
4747
route: '/activity',
4848
title: 'Native Activity Demo ',
@@ -54,17 +54,19 @@ class _MenuScreenState extends State<MenuScreen> {
5454
Widget build(BuildContext context) {
5555
return Scaffold(
5656
appBar: AppBar(
57-
title: Text('Menu List'),
57+
title: const Text('Menu List'),
5858
actions: [
5959
Row(
6060
children: [
61-
Text("Enable AR"),
61+
const Text("Enable AR"),
6262
Checkbox(
6363
value: enableAR,
6464
onChanged: (changed) {
65-
setState(() {
66-
enableAR = changed;
67-
});
65+
if (changed != null) {
66+
setState(() {
67+
enableAR = changed;
68+
});
69+
}
6870
},
6971
),
7072
],
@@ -97,5 +99,10 @@ class _MenuListItem {
9799
final String route;
98100
final bool enableAR;
99101

100-
_MenuListItem({this.title, this.description, this.route, this.enableAR});
102+
_MenuListItem({
103+
required this.title,
104+
required this.description,
105+
required this.route,
106+
required this.enableAR,
107+
});
101108
}

0 commit comments

Comments
 (0)