flutter_screenutil icon indicating copy to clipboard operation
flutter_screenutil copied to clipboard

How to responsive oriantaion layout

Open ashishoffice opened this issue 5 years ago • 12 comments

Can you added responsive oriantaion layout

ashishoffice avatar Feb 15 '21 18:02 ashishoffice

Sorry, I did not understand what you mean. Can you elaborate

lizhuoyuan avatar Feb 21 '21 02:02 lizhuoyuan

Previously we use to use this plugin inside an OrientationBuilder widget, so that we can change the design size on orientation change but now i think it will not be possible since you are wrapping the whole material app inside the ScreenUtilInit init

Faiyyaz avatar Mar 06 '21 09:03 Faiyyaz

Previously we use to use this plugin inside an OrientationBuilder widget, so that we can change the design size on orientation change but now i think it will not be possible since you are wrapping the whole material app inside the ScreenUtilInit init

In fact, OrientationBuilder is used internally. like this:

OrientationBuilder(
builder:MaterialApp)

lizhuoyuan avatar Mar 08 '21 02:03 lizhuoyuan

Oh okay then can you expose the orientation so that we can know current orientation of the device?

Faiyyaz avatar Mar 08 '21 03:03 Faiyyaz

Can you send the example how to orientation

ashishoffice avatar Mar 08 '21 03:03 ashishoffice

Can you send the example how to orientation

@ashishoffice Sorry i will send an updated sample tomorrow. I am using older version of the lib so i will update it to current version and send you the sample code.

Faiyyaz avatar Mar 08 '21 16:03 Faiyyaz

@ashishoffice you can use the below code

base_stateful_widget.dart

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

abstract class BaseStatefulWidget extends StatefulWidget {
  BaseStatefulWidget({Key key}) : super(key: key);
}

abstract class BaseState<Page extends BaseStatefulWidget> extends State<Page> {}

mixin BasicPage<Page extends BaseStatefulWidget> on BaseState<Page> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return onBackPress();
      },
      child: GestureDetector(
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: getBody(
          context,
          ScreenUtil().orientation,
        ),
      ),
    );
  }

  Widget getBody(BuildContext context, Orientation orientation);

  bool onBackPress();
}

main.dart

import 'package:demo_app/base_stateful_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(375, 812),
      builder: () {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(),
        );
      },
    );
  }
}

class MyHomePage extends BaseStatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends BaseState<MyHomePage> with BasicPage {
  @override
  Widget getBody(BuildContext context, Orientation orientation) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Test'),
      ),
      body: _getContainer(orientation),
    );
  }

  @override
  bool onBackPress() {
    return true;
  }

  _getContainer(Orientation orientation) {
    if (orientation == Orientation.portrait) {
      return Center(
        child: Container(
          height: 200.h,
          width: 200.h,
          color: Colors.green,
          child: Center(
            child: Text(
              'I am Portrait Container on screenSize ${MediaQuery.of(context).size.height} x ${MediaQuery.of(context).size.width}',
              textAlign: TextAlign.center,
              style: TextStyle(
                color: Colors.white,
              ),
            ),
          ),
        ),
      );
    } else {
      return Center(
        child: Container(
          height: 200.h,
          width: 200.h,
          color: Colors.red,
          child: Center(
            child: Text(
              'I am Landscape Container on screenSize ${MediaQuery.of(context).size.height} x ${MediaQuery.of(context).size.width}',
              textAlign: TextAlign.center,
              style: TextStyle(
                color: Colors.white,
              ),
            ),
          ),
        ),
      );
    }
  }
}

Faiyyaz avatar Mar 09 '21 05:03 Faiyyaz

I think your problem should be resolved, if anyone has a problem, you can reopen it

lizhuoyuan avatar Apr 09 '21 11:04 lizhuoyuan

Yes it has been thank you for your contribution

Faiyyaz avatar Apr 10 '21 06:04 Faiyyaz

Hello @lizhuoyuan , i'm using version 5.0.0+2 and this is not working anymore; How can i change the designSize based on orientation?

RicardoLimaDev avatar Aug 09 '21 17:08 RicardoLimaDev

Hi @RicardoLimaDev you can orientationbuilder widget. The design size will just interchange you can't have two different designsize.

Faiyyaz avatar Aug 09 '21 17:08 Faiyyaz

Hello @lizhuoyuan , i'm using version 5.0.0+2 and this is not working anymore; How can i change the designSize based on orientation?

You only need to set the vertical

lizhuoyuan avatar Aug 10 '21 02:08 lizhuoyuan