I encountered a similar issue on disabling fullscreen mode on my app. To enable fullscreen, I force the app to be on landscape by using SystemChrome.setPreferredOrientations(). I then disable fullscreen by setting the preferred orientation to portrait.
void _enableFullscreen(bool fullscreen) {
isFullscreen = fullscreen;
if (fullscreen) {
// Force landscape orientation for fullscreen
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
} else {
// Force portrait
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
}
The app detects the device's orientation using OrientationBuilder.
OrientationBuilder(builder: (context, orientation) {
debugPrint('Device orientation: $orientation');
isFullscreen = orientation == Orientation.landscape;
return isFullscreen
/// Landscape Layout
? Container()
/// Portrait Layout
: Column();
})
The issue that I encountered here was the widget doesn't rebuild base on the device's orientation (i.e. auto-rotate) after setting a preferred orientation. To solve this issue I set an empty list on setPreferredOrientations() to instruct the Flutter app to defer to the operating system default (orientation) again as mentioned in the docs.
Here's a complete sample. I'm using Flutter 2.5
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isFullscreen = false;
void _enableFullscreen(bool fullscreen) {
isFullscreen = fullscreen;
if (fullscreen) {
// Force landscape orientation for fullscreen
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
} else {
// Force portrait
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
// Set preferred orientation to device default
// Empty list causes the application to defer to the operating system default.
// See: https://api.flutter.dev/flutter/services/SystemChrome/setPreferredOrientations.html
SystemChrome.setPreferredOrientations([]);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: OrientationBuilder(builder: (context, orientation) {
debugPrint('Device orientation: $orientation');
isFullscreen = orientation == Orientation.landscape;
return isFullscreen
/// Landscape Layout
? Container(color: Colors.green)
/// Portrait Layout
: Column(
children: [
Expanded(
flex: 1,
child: Container(color: Colors.green),
),
Expanded(
flex: 3,
child: Container(color: Colors.lightBlueAccent),
)
],
);
}),
floatingActionButton: FloatingActionButton(
onPressed: () {
_enableFullscreen(!isFullscreen);
},
tooltip: 'Fullscreen',
child: const Icon(Icons.fullscreen),
),
);
}
}
