Flutter setting orientation to landscappe redirects page to main app

  Kiến thức lập trình

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 the main page.
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 

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT