diff --git a/example/lib/main.dart b/example/lib/main.dart index bf59225..5cc6a86 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,11 +1,11 @@ import 'package:flutter/material.dart'; import 'package:screen/screen.dart'; -void main() => runApp(new MyApp()); +void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override - _MyAppState createState() => new _MyAppState(); + _MyAppState createState() => _MyAppState(); } class _MyAppState extends State { @@ -13,45 +13,57 @@ class _MyAppState extends State { double _brightness = 1.0; @override - initState() { + void initState() { super.initState(); initPlatformState(); } - initPlatformState() async { - bool keptOn = await Screen.isKeptOn; - double brightness = await Screen.brightness; - setState((){ - _isKeptOn = keptOn; - _brightness = brightness; + Future initPlatformState() async { + bool? keptOn = await Screen.isKeptOn; + double? brightness = await Screen.brightness; + setState(() { + _isKeptOn = keptOn ?? false; + _brightness = brightness ?? 1.0; }); } @override Widget build(BuildContext context) { - return new MaterialApp( - home: new Scaffold( - appBar: new AppBar(title: new Text('Screen plugin example')), - body: new Center( - child: new Column( + return MaterialApp( + home: Scaffold( + appBar: AppBar(title: Text('Screen plugin example')), + body: Center( + child: Column( + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.center, children: [ - new Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - new Text("Screen is kept on ? "), - new Checkbox(value: _isKeptOn, onChanged: (bool b){ + Text("Screen is kept on ? "), + Checkbox( + value: _isKeptOn, + onChanged: (bool? b) { + if (b != null) { Screen.keepOn(b); - setState((){_isKeptOn = b; }); - }) - ] - ), - new Text("Brightness :"), - new Slider(value : _brightness, onChanged : (double b){ - setState((){_brightness = b;}); - Screen.setBrightness(b); - }) - ] - ) + setState(() { + _isKeptOn = b; + }); + } + }, + ) + ], + ), + Text("Brightness :"), + Slider( + value: _brightness, + onChanged: (double b) { + setState(() { + _brightness = b; + }); + Screen.setBrightness(b); + }, + ) + ], + ), ), ), ); diff --git a/lib/screen.dart b/lib/screen.dart index 4dbc0d1..45d8189 100644 --- a/lib/screen.dart +++ b/lib/screen.dart @@ -1,12 +1,18 @@ -import 'dart:async'; - import 'package:flutter/services.dart'; class Screen { - static const MethodChannel _channel = const MethodChannel('github.com/clovisnicolas/flutter_screen'); + static const MethodChannel _channel = + const MethodChannel('github.com/clovisnicolas/flutter_screen'); + + static Future get brightness async => + (await _channel.invokeMethod('brightness')) as double?; + + static Future setBrightness(double brightness) => + _channel.invokeMethod('setBrightness', {"brightness": brightness}); + + static Future get isKeptOn async => + (await _channel.invokeMethod('isKeptOn')) as bool?; - static Future get brightness async => (await _channel.invokeMethod('brightness')) as double; - static Future setBrightness(double brightness) =>_channel.invokeMethod('setBrightness',{"brightness" : brightness}); - static Future get isKeptOn async => (await _channel.invokeMethod('isKeptOn')) as bool; - static Future keepOn(bool on) => _channel.invokeMethod('keepOn', {"on" : on}); -} + static Future keepOn(bool on) => + _channel.invokeMethod('keepOn', {"on": on}); +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index c3531d0..eec30e5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ author: Clovis Nicolas homepage: https://github.com/clovisnicolas/flutter_screen environment: - sdk: '>=2.0.0 <3.0.0' + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: