2

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

th_lo
  • 461
  • 3
  • 18
  • https://stackoverflow.com/a/50815039/16907012 – princesanjivy Sep 02 '22 at 09:45
  • Thanks @princesanjivy It is not possible to import any flutter packages into a flutter driver test https://stackoverflow.com/questions/52462646, so the linked post is not answering my question – th_lo Sep 02 '22 at 09:57

3 Answers3

2

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

Zeeshan Ayaz
  • 858
  • 6
  • 11
0

SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

kk web
  • 231
  • 2
  • 11
  • Thanks. It is not possible to import any flutter packages into a flutter driver test https://stackoverflow.com/a/52576882/11999287, this is not answering my question – th_lo Sep 02 '22 at 09:58
0

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 \
th_lo
  • 461
  • 3
  • 18