Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples to null safety #732

Merged
merged 3 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,40 +434,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 @@ -479,9 +480,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 @@ -491,17 +493,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 @@ -526,10 +530,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 @@ -539,8 +543,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 @@ -567,7 +571,7 @@ class _MyAppState extends State<MyApp> {

// Communcation from Flutter to Unity
void setRotationSpeed(String speed) {
_unityWidgetController.postMessage(
_unityWidgetController?.postMessage(
'Cube',
'SetRotationSpeed',
speed,
Expand All @@ -581,15 +585,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