Skip to content

Commit

Permalink
Update examples to null safety (#732)
Browse files Browse the repository at this point in the history
* Make ReadMe examples null-safe & fix errors.

* Make example project null safe.

* Fix most linter warnings.
  • Loading branch information
timbotimbo authored May 7, 2023
1 parent 1db8bfe commit 654e66c
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 128 deletions.
62 changes: 34 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,40 +479,41 @@ Unable to find a matching variant of project :unityLibrary:

```dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
void main() {
runApp(MaterialApp(
home: UnityDemoScreen()
));
runApp(
const MaterialApp(
home: UnityDemoScreen(),
),
);
}
class UnityDemoScreen extends StatefulWidget {
UnityDemoScreen({Key key}) : super(key: key);
const UnityDemoScreen({Key? key}) : super(key: key);
@override
_UnityDemoScreenState createState() => _UnityDemoScreenState();
State<UnityDemoScreen> createState() => _UnityDemoScreenState();
}
class _UnityDemoScreenState extends State<UnityDemoScreen>{
class _UnityDemoScreenState extends State<UnityDemoScreen> {
static final GlobalKey<ScaffoldState> _scaffoldKey =
GlobalKey<ScaffoldState>();
UnityWidgetController _unityWidgetController;
UnityWidgetController? _unityWidgetController;
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: SafeArea(
bottom: false,
child: WillPopScope(
onWillPop: () {
onWillPop: () async {
// Pop the category page if Android back button is pressed.
return true;
},
child: Container(
color: colorYellow,
color: Colors.yellow,
child: UnityWidget(
onUnityCreated: onUnityCreated,
),
Expand All @@ -524,9 +525,10 @@ class _UnityDemoScreenState extends State<UnityDemoScreen>{
// Callback that connects the created controller to the unity controller
void onUnityCreated(controller) {
this._unityWidgetController = controller;
_unityWidgetController = controller;
}
}
```
<br />

Expand All @@ -536,17 +538,19 @@ class _UnityDemoScreenState extends State<UnityDemoScreen>{
import 'package:flutter/material.dart';
import 'package:flutter_unity_widget/flutter_unity_widget.dart';
void main() => runApp(MyApp());
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static final GlobalKey<ScaffoldState> _scaffoldKey =
GlobalKey<ScaffoldState>();
UnityWidgetController _unityWidgetController;
UnityWidgetController? _unityWidgetController;
double _sliderValue = 0.0;
@override
Expand All @@ -571,10 +575,10 @@ class _MyAppState extends State<MyApp> {
child: Stack(
children: <Widget>[
UnityWidget(
onUnityCreated: onUnityCreated,
onUnityMessage: onUnityMessage,
onUnitySceneLoaded: onUnitySceneLoaded,
fullscreen: false,
onUnityCreated: onUnityCreated,
onUnityMessage: onUnityMessage,
onUnitySceneLoaded: onUnitySceneLoaded,
fullscreen: false,
),
Positioned(
bottom: 20,
Expand All @@ -584,8 +588,8 @@ class _MyAppState extends State<MyApp> {
elevation: 10,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20),
const Padding(
padding: EdgeInsets.only(top: 20),
child: Text("Rotation speed:"),
),
Slider(
Expand All @@ -612,7 +616,7 @@ class _MyAppState extends State<MyApp> {
// Communcation from Flutter to Unity
void setRotationSpeed(String speed) {
_unityWidgetController.postMessage(
_unityWidgetController?.postMessage(
'Cube',
'SetRotationSpeed',
speed,
Expand All @@ -626,15 +630,17 @@ class _MyAppState extends State<MyApp> {
// Callback that connects the created controller to the unity controller
void onUnityCreated(controller) {
this._unityWidgetController = controller;
_unityWidgetController = controller;
}
// Communication from Unity when new scene is loaded to Flutter
void onUnitySceneLoaded(SceneLoaded sceneInfo) {
print('Received scene loaded from unity: ${sceneInfo.name}');
print('Received scene loaded from unity buildIndex: ${sceneInfo.buildIndex}');
void onUnitySceneLoaded(SceneLoaded? sceneInfo) {
if (sceneInfo != null) {
print('Received scene loaded from unity: ${sceneInfo.name}');
print(
'Received scene loaded from unity buildIndex: ${sceneInfo.buildIndex}');
}
}
}
```
Expand Down
16 changes: 9 additions & 7 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import 'screens/loader_screen.dart';
import 'screens/simple_screen.dart';

void main() {
runApp(MyApp());
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
Expand All @@ -23,12 +25,12 @@ class MyApp extends StatelessWidget {
),
initialRoute: '/',
routes: {
'/': (context) => MenuScreen(),
'/simple': (context) => SimpleScreen(),
'/loader': (context) => LoaderScreen(),
'/orientation': (context) => OrientationScreen(),
'/api': (context) => ApiScreen(),
'/none': (context) => NoInteractionScreen(),
'/': (context) => const MenuScreen(),
'/simple': (context) => const SimpleScreen(),
'/loader': (context) => const LoaderScreen(),
'/orientation': (context) => const OrientationScreen(),
'/api': (context) => const ApiScreen(),
'/none': (context) => const NoInteractionScreen(),
},
);
}
Expand Down
35 changes: 21 additions & 14 deletions example/lib/menu_screen.dart
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import 'package:flutter/material.dart';

class MenuScreen extends StatefulWidget {
MenuScreen({Key key}) : super(key: key);
const MenuScreen({Key? key}) : super(key: key);

@override
_MenuScreenState createState() => _MenuScreenState();
State<MenuScreen> createState() => _MenuScreenState();
}

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

List<_MenuListItem> menus = [
new _MenuListItem(
_MenuListItem(
description: 'Simple demonstration of unity flutter library',
route: '/simple',
title: 'Simple Unity Demo',
enableAR: false,
),
new _MenuListItem(
_MenuListItem(
description: 'No interaction of unity flutter library',
route: '/none',
title: 'No Interaction Unity Demo',
enableAR: false,
),
new _MenuListItem(
_MenuListItem(
description: 'Unity load and unload unity demo',
route: '/loader',
title: 'Safe mode Demo',
enableAR: false,
),
new _MenuListItem(
_MenuListItem(
description:
'This example shows various native API exposed by the library',
route: '/api',
title: 'Native exposed API demo',
enableAR: false,
),
new _MenuListItem(
_MenuListItem(
title: 'Test Orientation',
route: '/orientation',
description: 'test orientation change',
enableAR: false,
),
new _MenuListItem(
_MenuListItem(
description: 'Unity native activity demo',
route: '/activity',
title: 'Native Activity Demo ',
Expand All @@ -54,17 +54,19 @@ class _MenuScreenState extends State<MenuScreen> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Menu List'),
title: const Text('Menu List'),
actions: [
Row(
children: [
Text("Enable AR"),
const Text("Enable AR"),
Checkbox(
value: enableAR,
onChanged: (changed) {
setState(() {
enableAR = changed;
});
if (changed != null) {
setState(() {
enableAR = changed;
});
}
},
),
],
Expand Down Expand Up @@ -97,5 +99,10 @@ class _MenuListItem {
final String route;
final bool enableAR;

_MenuListItem({this.title, this.description, this.route, this.enableAR});
_MenuListItem({
required this.title,
required this.description,
required this.route,
required this.enableAR,
});
}
Loading

0 comments on commit 654e66c

Please sign in to comment.