Skip to content

Refactored and null-safe version of the code #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 42 additions & 30 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,57 +1,69 @@
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<MyApp> {
bool _isKeptOn = false;
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<void> 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: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
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);
},
)
],
),
),
),
);
Expand Down
22 changes: 14 additions & 8 deletions lib/screen.dart
Original file line number Diff line number Diff line change
@@ -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<double?> get brightness async =>
(await _channel.invokeMethod('brightness')) as double?;

static Future<void> setBrightness(double brightness) =>
_channel.invokeMethod('setBrightness', {"brightness": brightness});

static Future<bool?> get isKeptOn async =>
(await _channel.invokeMethod('isKeptOn')) as bool?;

static Future<double> get brightness async => (await _channel.invokeMethod('brightness')) as double;
static Future setBrightness(double brightness) =>_channel.invokeMethod('setBrightness',{"brightness" : brightness});
static Future<bool> get isKeptOn async => (await _channel.invokeMethod('isKeptOn')) as bool;
static Future keepOn(bool on) => _channel.invokeMethod('keepOn', {"on" : on});
}
static Future<void> keepOn(bool on) =>
_channel.invokeMethod('keepOn', {"on": on});
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ author: Clovis Nicolas <[email protected]>
homepage: https://github.com/clovisnicolas/flutter_screen

environment:
sdk: '>=2.0.0 <3.0.0'
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
Expand Down