flutter_displaymode 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 79

Flutter Display Mode #

pub package

A Flutter plugin to set display mode in Android. This library should be used as a temporary fix to #35162 until this API gets added to Flutter engine itself.

Getting Started #

Add package to pubspec.yaml then re-run flutter pub get and restart your app:

dependencies:
  flutter_displaymode: $latest_version

Methods #

Get supported modes #

import 'package:flutter_displaymode/flutter_displaymode.dart';

///...///

try {
  modes = await FlutterDisplayMode.supported;
  modes.forEach(print);

  /// On OnePlus 7 Pro:
  /// #1 1080x2340 @ 60Hz
  /// #2 1080x2340 @ 90Hz
  /// #3 1440x3120 @ 90Hz
  /// #4 1440x3120 @ 60Hz

  /// On OnePlus 8 Pro:
  /// #1 1080x2376 @ 60Hz
  /// #2 1440x3168 @ 120Hz
  /// #3 1440x3168 @ 60Hz
  /// #4 1080x2376 @ 120Hz
} on PlatformException catch (e) {
  /// e.code =>
  /// noAPI - No API support. Only Marshmallow and above.
  /// noActivity - Activity is not available. Probably app is in background
}

Get current mode before any mode selected (acurate before manually set) #

final DisplayMode m = await FlutterDisplayMode.current;

Get currently selected mode #

/// If this is null it means app is in auto mode.
/// Once you have made a selection flag will be enabled.
final DisplayMode m = modes.firstWhere((DisplayMode m) => m.selected, orElse: () => null);

Set the mode to default (worked before manually set) #

await FlutterDisplayMode.setDefaultMode();

Set a mode #

/// This setting is per session. 
/// Please ensure this was placed with `initState` of your root widget.
await FlutterDisplayMode.setMode(modes[0]);

You can check out an example here.

0.1.0 #

  • Support get current mode from platform.
  • Support default mode set according to platform setting.
  • Code stylus improving.

0.0.1 #

  • Initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_displaymode/flutter_displaymode.dart';

void main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<DisplayMode> modes = <DisplayMode>[];
  DisplayMode selected;

  Future<void> fetchModes() async {
    try {
      modes = await FlutterDisplayMode.supported;
      modes.forEach(print);

      /// On OnePlus 7 Pro:
      /// #1 1080x2340 @ 60Hz
      /// #2 1080x2340 @ 90Hz
      /// #3 1440x3120 @ 90Hz
      /// #4 1440x3120 @ 60Hz

      /// On OnePlus 8 Pro:
      /// #1 1080x2376 @ 60Hz
      /// #2 1440x3168 @ 120Hz
      /// #3 1440x3168 @ 60Hz
      /// #4 1080x2376 @ 120Hz
    } on PlatformException catch (e) {
      print(e);

      /// e.code =>
      /// noAPI - No API support. Only Marshmallow and above.
      /// noActivity - Activity is not available. Probably app is in background
    }
    selected =
        modes.firstWhere((DisplayMode m) => m.selected, orElse: () => null);
    if (mounted) {
      setState(() {});
    }
  }

  Future<DisplayMode> getCurrentMode() async {
    return await FlutterDisplayMode.current;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Plugin example app')),
        body: Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                RaisedButton(
                  child: const Text('Fetch modes (Native)'),
                  onPressed: fetchModes,
                ),
                RaisedButton(
                  child: const Text('Current mode (Native)'),
                  onPressed: () async {
                    final DisplayMode mode = await getCurrentMode();
                    print(mode);
                    showDialog<void>(
                      context: context,
                      builder: (_) => AlertDialog(
                        content: Text(mode.toString()),
                      ),
                    );
                  },
                ),
              ],
            ),
            DropdownButton<DisplayMode>(
              value: selected,
              items: modes
                  .map((DisplayMode m) => DropdownMenuItem<DisplayMode>(
                        child: Text(m.toString()),
                        value: m,
                      ))
                  .toList(),
              onChanged: (DisplayMode m) async {
                await FlutterDisplayMode.setMode(m);
                selected = m;
                if (mounted) {
                  setState(() {});
                }
              },
            ),
            Expanded(
              child: ListView.builder(
                itemBuilder: (BuildContext _, int i) {
                  return Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text('Index: $i'),
                  );
                },
              ),
            ),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          tooltip: 'Set default mode',
          onPressed: FlutterDisplayMode.setDeviceDefault,
          child: Icon(Icons.build),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_displaymode: ^0.1.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_displaymode/flutter_displaymode.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
63
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
87
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

We analyzed this package on Jul 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform Linux

Because:

  • package:flutter_displaymode/flutter_displaymode.dart that declares support for platforms: Android

Package does not support Flutter platform Web

Because:

  • package:flutter_displaymode/flutter_displaymode.dart that declares support for platforms: Android

Package does not support Flutter platform Windows

Because:

  • package:flutter_displaymode/flutter_displaymode.dart that declares support for platforms: Android

Package does not support Flutter platform iOS

Because:

  • package:flutter_displaymode/flutter_displaymode.dart that declares support for platforms: Android

Package does not support Flutter platform macOS

Because:

  • package:flutter_displaymode/flutter_displaymode.dart that declares support for platforms: Android

Package not compatible with SDK dart

Because:

  • flutter_displaymode that is a package requiring null.

Maintenance suggestions

The package description is too short. (-13 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test