You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
flutter_cocos_view/example/lib/screens/orientation_screen.dart

108 lines
3.4 KiB

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_cocos_view/flutter_cocos_view.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';
class OrientationScreen extends StatefulWidget {
const OrientationScreen({Key? key}) : super(key: key);
@override
State<OrientationScreen> createState() => _OrientationScreenState();
}
class _OrientationScreenState extends State<OrientationScreen> {
CocosWidgetController? _cocosWidgetController;
double _sliderValue = 0.0;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Orientation Screen'),
),
body: Card(
margin: const EdgeInsets.all(8),
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
child: Stack(
children: <Widget>[
CocosWidget(
onCocosCreated: onCocosCreated,
onCocosMessage: onCocosMessage,
useAndroidViewSurface: true,
),
Positioned(
bottom: 20,
left: 20,
right: 20,
child: PointerInterceptor(
child: Card(
elevation: 10,
child: Column(
children: <Widget>[
ElevatedButton(
onPressed: () {
if (MediaQuery.of(context).orientation ==
Orientation.portrait) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight
]);
} else if (MediaQuery.of(context).orientation ==
Orientation.landscape) {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp]);
}
},
child: const Text("Change Orientation"),
),
const Padding(
padding: EdgeInsets.only(top: 20),
child: Text("Rotation speed:"),
),
Slider(
onChanged: (value) {
setState(() {
_sliderValue = value;
});
setRotationSpeed(value.toString());
},
value: _sliderValue,
min: 0,
max: 20,
),
],
),
),
),
),
],
),
),
);
}
void setRotationSpeed(String speed) {
_cocosWidgetController?.postMessage(
'Cube',
'SetRotationSpeed',
speed,
);
}
void onCocosMessage(message) {
print('Received message from cocos: ${message.toString()}');
}
// Callback that connects the created controller to the cocos controller
void onCocosCreated(controller) {
_cocosWidgetController = controller;
}
}