-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.dart
67 lines (58 loc) · 1.77 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import 'package:flutter/material.dart';
import 'package:lamp/lamp.dart';
import 'dart:async';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _hasFlash = false;
bool _isOn = false;
double _intensity = 1.0;
@override
initState() {
super.initState();
initPlatformState();
}
initPlatformState() async {
bool hasFlash = await Lamp.hasLamp;
print("Device has flash ? $hasFlash");
setState(() { _hasFlash = hasFlash; });
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(primarySwatch: Colors.pink),
home: new Scaffold(
appBar: new AppBar(title: new Text('Lamp plugin example')),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text('Device has flash: $_hasFlash\n Flash is on: $_isOn'),
new Slider(value: _intensity, onChanged: _isOn ? _intensityChanged : null),
new RaisedButton(onPressed: () async => await Lamp.flash(new Duration(seconds: 2)), child: new Text("Flash for 2 seconds"))
]),
),
floatingActionButton: new FloatingActionButton(
child: new Icon(_isOn ? Icons.flash_off : Icons.flash_on),
onPressed: _turnFlash),
),
);
}
Future _turnFlash() async {
_isOn ? Lamp.turnOff() : Lamp.turnOn(intensity: _intensity);
var f = await Lamp.hasLamp;
setState((){
_hasFlash = f;
_isOn = !_isOn;
});
}
_intensityChanged(double intensity) {
Lamp.turnOn(intensity : intensity);
setState((){
_intensity = intensity;
});
}
}