Line data Source code
1 : import 'dart:async'; 2 : 3 : import 'package:flutter/services.dart'; 4 : import 'package:screen_brightness_platform_interface/extension/num_extension.dart'; 5 : import 'package:screen_brightness_platform_interface/screen_brightness_platform_interface.dart'; 6 : 7 : import 'constant/brightness.dart'; 8 : import 'constant/method_name.dart'; 9 : import 'constant/plugin_channel.dart'; 10 : 11 : /// Implementation of screen brightness platform interface 12 : class MethodChannelScreenBrightness extends ScreenBrightnessPlatform { 13 : /// Private stream which is listened to event channel for preventing 14 : Stream<double>? _onCurrentBrightnessChanged; 15 : 16 : /// Returns system screen brightness which is set when application is started. 17 : /// 18 : /// The value should be within 0.0 - 1.0. Otherwise, [RangeError.range] will 19 : /// be throw. 20 : /// 21 : /// This parameter is useful for user to get screen brightness value after 22 : /// calling [resetScreenBrightness] 23 : /// 24 : /// When [_channel.invokeMethod] fails to get current brightness, it throws 25 : /// [PlatformException] with code and message: 26 : /// 27 : /// Code: -9, Message: Brightness value returns null 28 : @override 29 1 : Future<double> get system async { 30 1 : final systemBrightness = await pluginMethodChannel 31 1 : .invokeMethod<double>(methodNameGetSystemScreenBrightness); 32 : if (systemBrightness == null) { 33 1 : throw PlatformException( 34 : code: "-9", message: "Brightness value returns null"); 35 : } 36 : 37 1 : if (!systemBrightness.isInRange(minBrightness, maxBrightness)) { 38 1 : throw RangeError.range(systemBrightness, minBrightness, maxBrightness); 39 : } 40 : 41 : return systemBrightness; 42 : } 43 : 44 : /// Returns current screen brightness which is current screen brightness value. 45 : /// 46 : /// The value should be within 0.0 - 1.0. Otherwise, [RangeError.range] will 47 : /// be throw. 48 : /// 49 : /// This parameter is useful for user to get screen brightness value after 50 : /// calling [setScreenBrightness] 51 : /// 52 : /// Calling this method after calling [resetScreenBrightness] may return wrong 53 : /// value in iOS because UIScreen.main.brightness returns old brightness value 54 : /// 55 : /// When [_channel.invokeMethod] fails to get current brightness, it throws 56 : /// [PlatformException] with code and message: 57 : /// 58 : /// Code: -9, Message: Brightness value returns null 59 : /// 60 : /// (Android only) Code: -10, Message: Unexpected error on activity binding 61 : /// Unexpected error when getting activity, activity may be null 62 : /// 63 : /// (Android only) Code: -11, Message: Could not found system setting screen 64 : /// brightness value 65 : /// Unexpected error when getting brightness from Setting using 66 : /// Settings.System.SCREEN_BRIGHTNESS 67 : @override 68 1 : Future<double> get current async { 69 1 : final currentBrightness = await pluginMethodChannel 70 1 : .invokeMethod<double>(methodNameGetScreenBrightness); 71 : if (currentBrightness == null) { 72 1 : throw PlatformException( 73 : code: "-9", message: "Brightness value returns null"); 74 : } 75 : 76 1 : if (!currentBrightness.isInRange(minBrightness, maxBrightness)) { 77 1 : throw RangeError.range(currentBrightness, minBrightness, maxBrightness); 78 : } 79 : 80 : return currentBrightness; 81 : } 82 : 83 : /// Set screen brightness with double value. 84 : /// 85 : /// The value should be within 0.0 - 1.0. Otherwise, [RangeError.range] will 86 : /// be throw. 87 : /// 88 : /// This method is useful for user to change screen brightness. 89 : /// 90 : /// When [_channel.invokeMethod] fails to get current brightness, it throws 91 : /// [PlatformException] with code and message: 92 : /// 93 : /// Code: -2, Message: Unexpected error on null brightness 94 : /// Cannot read parameter from method channel map, or parameter is null 95 : /// 96 : /// Code: -1, Message: Unable to change screen brightness 97 : /// Compare changed value with set value fail 98 : /// 99 : /// Code: -10, Message: Unexpected error on activity binding 100 : /// Unexpected error when getting activity, activity may be null 101 : @override 102 1 : Future<void> setScreenBrightness(double brightness) async { 103 1 : if (!brightness.isInRange(minBrightness, maxBrightness)) { 104 1 : throw RangeError.range(brightness, minBrightness, maxBrightness); 105 : } 106 : 107 2 : await pluginMethodChannel.invokeMethod( 108 1 : methodNameSetScreenBrightness, {"brightness": brightness}); 109 : } 110 : 111 : /// Reset screen brightness with (Android)-1 or (iOS)system brightness value. 112 : /// 113 : /// This method is useful for user to reset screen brightness when user leave 114 : /// the page which has change the brightness value. 115 : /// 116 : /// When [_channel.invokeMethod] fails to get current brightness, it throws 117 : /// [PlatformException] with code and message: 118 : /// 119 : /// Code: -2, Message: Unexpected error on null brightness 120 : /// System brightness in plugin is null 121 : /// 122 : /// Code: -1, Message: Unable to change screen brightness 123 : /// Compare changed value with set value fail 124 : /// 125 : /// Code: -10, Message: Unexpected error on activity binding 126 : /// Unexpected error when getting activity, activity may be null 127 : @override 128 1 : Future<void> resetScreenBrightness() async { 129 2 : await pluginMethodChannel.invokeMethod(methodNameResetScreenBrightness); 130 : } 131 : 132 : /// A stream return with screen brightness changes including 133 : /// [ScreenBrightness.setScreenBrightness], 134 : /// [ScreenBrightness.resetScreenBrightness], system control center or system 135 : /// setting. 136 : /// 137 : /// This stream is useful for user to listen to brightness changes. 138 1 : @override 139 : Stream<double> get onCurrentBrightnessChanged { 140 1 : _onCurrentBrightnessChanged ??= pluginEventChannelCurrentBrightnessChange 141 1 : .receiveBroadcastStream() 142 1 : .cast<double>(); 143 1 : return _onCurrentBrightnessChanged!; 144 : } 145 : }