Reputation: 41
Edit: I have a solution that worked for me.
I had to change the androidmanifest
from:
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
to:
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
This change seems to prevent the reset.
I have to change the orientation in a specified page to DeviceOrientation.landscapeLeft.
But when i do this on the page the orientation changes and when the ui
is rendered it immediately changes to reset the activity.
And then the app go's back to the first page.
It does not seem to mater wat i return in the build it always redirects.
I get no error.
This is the code of the landscape screen and the main screen.
class _ConsumptionExpandedScreenState extends State<ConsumptionExpandedScreen>
with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
WidgetsFlutterBinding.ensureInitialized();
// Lock orientation to landscape left
_setLandscapeOrientation();
}
@override
void dispose() {
logger.d("In the dispose function of the expanded screen");
// Reset orientation when leaving the screen
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_setLandscapeOrientation();
}
}
void _setLandscapeOrientation() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
]);
}
@override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
double screenHeight = MediaQuery.of(context).size.height;
logger.d(
"Expanded consumption the current size(width : height) is $screenWidth : $screenHeight");
...
class _MainAppState extends State<MainApp> with WidgetsBindingObserver {
Locale myLocale = const Locale('en', 'US');
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// Handle app lifecycle changes if needed
}
@override
void didChangeMetrics() {
final newSize = WidgetsBinding.instance.window.physicalSize;
logger.d("New size: ${newSize.width} : ${newSize.height}");
// Handle orientation changes if needed
setState(() {}); // Trigger rebuild to adjust layout
}
@override
void initState() {
super.initState();
_loadData();
}
void _loadData() async {
final prefs = await SharedPreferences.getInstance();
indexLanguage = prefs.getInt(langIndexKey);
// print("The number of the index $indexLanguage");
if (indexLanguage != null) {}
setState(() {});
}
@override
void dispose() {
// _adapterStateStateSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<ValueChangeProvider>(
create: (context) => ValueChangeProvider(),
child: Consumer<ValueChangeProvider>(
builder: (context, valueChangeProvider, _) {
logger.d("The main screen ");
double screenWidth = MediaQuery.of(context).size.width;
double screenHeight = MediaQuery.of(context).size.height;
logger.d(
"The current size(width : height) is $screenWidth : $screenHeight");
updatePaddingValues(context);
return PaddingConfig(
sidePadding: globalPaddingSide,
topPadding: paddingTop,
topHeaderPadding: paddingHeaderTop,
child: MaterialApp(
color: AppColors.backgroundColor,
navigatorObservers: [
BluetoothAdapterStateObserver(valueChangeProvider),
routeObserver
],
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
locale: valueChangeProvider.currentLocale,
theme: lightTheme,
debugShowCheckedModeBanner: false,
initialRoute: initialAppRoute,
routes: AppRoutes.routes,
),
);
},
),
);
}
}
I have tried to change the rotation before entering this page but it does not matter on wat page it is, it always redirects to the mainApp page
this is the log of what happens when i push this page with the navigation.
I/flutter (18518): [D] Expanded consumption the current size(width : height) is 360.0 : 708.0
I/flutter (18518): [D] The main screen
I/flutter (18518): [D] The current size(width : height) is 672.0 : 360.0
I/flutter (18518): [D] In the pairing list screen
I/flutter (18518): [D] Expanded consumption the current size(width : height) is 672.0 : 360.0
I/flutter (18518): [D] Going to the pairing list
I/flutter (18518): [D] In the pairing list screen
I/flutter (18518): [D] Expanded consumption the current size(width : height) is 672.0 : 360.0
I/flutter (18518): [D] The main screen
I/flutter (18518): [D] The current size(width : height) is 360.0 : 708.0
I/flutter (18518): [D] In the pairing list screen
I/flutter (18518): [D] In the pairing list screen
I/flutter (18518): [D] Expanded consumption the current size(width : height) is 360.0 : 708.0
I/flutter (18518): [D] Going to the pairing list
I/flutter (18518): [D] In the pairing list screen
I/flutter (18518): [D] In the pairing list screen
Upvotes: 0
Views: 52
Reputation: 1
You can force landscape view for this screen this way:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class ConsumptionExpandedScreen extends StatefulWidget {
const ConsumptionExpandedScreen({super.key});
@override
ConsumptionExpandedScreenState createState() => ConsumptionExpandedScreenState();
}
class ConsumptionExpandedScreenState extends State<ConsumptionExpandedScreen> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_setLandscapeOrientation();
}
@override
void dispose() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
void _setLandscapeOrientation() {
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
}
@override
Widget build(BuildContext context) {
// Your landscape layout
}
}
Upvotes: 0