@@ -608,34 +608,23 @@ class UnityDemoScreen extends StatefulWidget {
608608}
609609
610610class _UnityDemoScreenState extends State<UnityDemoScreen> {
611- static final GlobalKey<ScaffoldState> _scaffoldKey =
612- GlobalKey<ScaffoldState>();
611+
613612 UnityWidgetController? _unityWidgetController;
614613
615614 @override
616615 Widget build(BuildContext context) {
617616 return Scaffold(
618- key: _scaffoldKey,
619- body: SafeArea(
620- bottom: false,
621- child: WillPopScope(
622- onWillPop: () async {
623- // Pop the category page if Android back button is pressed.
624- return true;
625- },
626- child: Container(
627- color: Colors.yellow,
628- child: UnityWidget(
629- onUnityCreated: onUnityCreated,
630- ),
631- ),
617+ body: Container(
618+ color: Colors.yellow,
619+ child: UnityWidget(
620+ onUnityCreated: onUnityCreated,
632621 ),
633622 ),
634623 );
635624 }
636625
637626 // Callback that connects the created controller to the unity controller
638- void onUnityCreated(controller) {
627+ void onUnityCreated(UnityWidgetController controller) {
639628 _unityWidgetController = controller;
640629 }
641630}
@@ -649,79 +638,71 @@ class _UnityDemoScreenState extends State<UnityDemoScreen> {
649638import 'package:flutter/material.dart';
650639import 'package:flutter_unity_widget/flutter_unity_widget.dart';
651640
652- void main() => runApp(const MyApp());
641+ void main() {
642+ runApp(
643+ const MaterialApp(
644+ home: UnityDemoScreen(),
645+ ),
646+ );
647+ }
653648
654- class MyApp extends StatefulWidget {
655- const MyApp ({Key? key}) : super(key: key);
649+ class UnityDemoScreen extends StatefulWidget {
650+ const UnityDemoScreen ({Key? key}) : super(key: key);
656651
657652 @override
658- State<MyApp > createState() => _MyAppState ();
653+ State<UnityDemoScreen > createState() => _UnityDemoScreenState ();
659654}
660655
661- class _MyAppState extends State<MyApp> {
662- static final GlobalKey<ScaffoldState> _scaffoldKey =
663- GlobalKey<ScaffoldState>();
656+ class _UnityDemoScreenState extends State<UnityDemoScreen> {
664657 UnityWidgetController? _unityWidgetController;
665658 double _sliderValue = 0.0;
666659
667- @override
668- void initState() {
669- super.initState();
670- }
671-
672660 @override
673661 Widget build(BuildContext context) {
674- return MaterialApp(
675- home: Scaffold(
676- key: _scaffoldKey,
677- appBar: AppBar(
678- title: const Text('Unity Flutter Demo'),
679- ),
680- body: Card(
681- margin: const EdgeInsets.all(8),
682- clipBehavior: Clip.antiAlias,
683- shape: RoundedRectangleBorder(
684- borderRadius: BorderRadius.circular(20.0),
662+ return Scaffold(
663+ appBar: AppBar(
664+ title: const Text('Unity Flutter Demo'),
665+ ),
666+ body: Stack(
667+ children: <Widget>[
668+ UnityWidget(
669+ onUnityCreated: onUnityCreated,
670+ onUnityMessage: onUnityMessage,
671+ onUnitySceneLoaded: onUnitySceneLoaded,
685672 ),
686- child: Stack(
687- children: <Widget>[
688- UnityWidget(
689- onUnityCreated: onUnityCreated,
690- onUnityMessage: onUnityMessage,
691- onUnitySceneLoaded: onUnitySceneLoaded,
692- fullscreen: false,
693- ),
694- Positioned(
695- bottom: 20,
696- left: 20,
697- right: 20,
698- // <You need a PointerInterceptor here on web>
699- child: Card(
700- elevation: 10,
701- child: Column(
702- children: <Widget>[
703- const Padding(
704- padding: EdgeInsets.only(top: 20),
705- child: Text("Rotation speed:"),
706- ),
707- Slider(
708- onChanged: (value) {
709- setState(() {
710- _sliderValue = value;
711- });
712- setRotationSpeed(value.toString());
713- },
714- value: _sliderValue,
715- min: 0,
716- max: 20,
717- ),
718- ],
719- ),
673+
674+ // Flutter UI Stacked on top of Unity to demo Flutter -> Unity interactions.
675+ // On web this requires a PointerInterceptor widget.
676+ Positioned(
677+ bottom: 0,
678+ // <You need a PointerInterceptor here on web>
679+ child: SafeArea(
680+ child: Card(
681+ elevation: 10,
682+ child: Column(
683+ children: <Widget>[
684+ const Padding(
685+ padding: EdgeInsets.only(top: 20),
686+ child: Text("Rotation speed:"),
687+ ),
688+ Slider(
689+ onChanged: (value) {
690+ setState(() {
691+ _sliderValue = value;
692+ });
693+ // Send value to Unity
694+ setRotationSpeed(value.toString());
695+ },
696+ value: _sliderValue,
697+ min: 0.0,
698+ max: 1.0,
699+ ),
700+ ],
720701 ),
721702 ),
722- ] ,
703+ ) ,
723704 ),
724- ) ,
705+ ] ,
725706 ),
726707 );
727708 }
@@ -735,16 +716,16 @@ class _MyAppState extends State<MyApp> {
735716 );
736717 }
737718
738- // Communication from Unity to Flutter
739- void onUnityMessage(message) {
740- print('Received message from unity: ${message.toString()}');
741- }
742-
743719 // Callback that connects the created controller to the unity controller
744- void onUnityCreated(controller) {
720+ void onUnityCreated(UnityWidgetController controller) {
745721 _unityWidgetController = controller;
746722 }
747723
724+ // Communication from Unity to Flutter
725+ void onUnityMessage(dynamic message) {
726+ print('Received message from unity: ${message.toString()}');
727+ }
728+
748729 // Communication from Unity when new scene is loaded to Flutter
749730 void onUnitySceneLoaded(SceneLoaded? sceneInfo) {
750731 if (sceneInfo != null) {
@@ -754,7 +735,6 @@ class _MyAppState extends State<MyApp> {
754735 }
755736 }
756737}
757-
758738```
759739
760740## Props
0 commit comments