th_lo
th_lo

Reputation: 575

Flutter Driver: How to check device orientation?

How can I check the device orientation inside a flutter integration test within a flutter driver test?

SystemChrome.setPreferredOrientations

IS NOT AVAILABLE IN FLUTTER TEST!!! See: https://stackoverflow.com/a/52576882/11999287

Upvotes: 2

Views: 271

Answers (3)

th_lo
th_lo

Reputation: 575

As mentioned in this post it is not possible to run any flutter packages in a driver file with the packages

import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';

imported.

so you have to run a file with in the folder "test_driver" e.g.: "test_driver/test_driver.dart" for screenshots:

import 'package:integration_test/integration_test_driver_extended.dart';

or for responseDataCallback Data:

import 'package:integration_test/integration_test_driver.dart';

and await integrationDriver() in the main() e.g. "test_driver/driver_app.dart"

in the "integration_test/run_tests.dart" folder you can have your test file with your tests and testgroups and import the

import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

and test with a IntegrationTestWidgetsFlutterBinding and a WidgetTester here you can import flutter packages without the error mentioned in https://stackoverflow.com/a/52576882/11999287

to run with test driver you need with the command

flutter drive \
   --driver=test_driver/test_driver.dart \
   --target=integration_test/run_tests.dart \

Upvotes: 0

Zeeshan Ayaz
Zeeshan Ayaz

Reputation: 1038

Create the widget, responsive_widget.dart

import 'package:flutter/material.dart';

typedef ResponsiveBuilder = Widget Function(bool isWide, double width);

class ResponsiveWidget extends StatelessWidget {
  final ResponsiveBuilder builder;

  const ResponsiveWidget({Key? key,required this.builder}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (_, box) => OrientationBuilder(
          builder: (_, orientation) =>
              builder(orientation == Orientation.landscape, box.maxWidth)),
    );
  }
}

Use the above package as the parent where you want to check the device orientation.

For Example:

@override
  Widget build(BuildContext context) {
    return ResponsiveWidget(builder: (isWide, width) {
      return Scaffold(
      appBar: AppBar(
        title: const Text(
          'Check device Orientation',
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(10),
        child: Center(
          child: Text(
              isWide ? 'LandScape' : 'Portrait'
          ),
        ),
      ),
    );
    });
  }

Preview: enter image description here

enter image description here

enter image description here

Upvotes: 2

kk web
kk web

Reputation: 235

SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

Upvotes: 0

Related Questions