Skip to content

Commit cdf1235

Browse files
added torch functionality
1 parent d793adb commit cdf1235

File tree

9 files changed

+262
-17
lines changed

9 files changed

+262
-17
lines changed

android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ android {
2424
applicationId = "com.example.lamp_app"
2525
// You can update the following values to match your application needs.
2626
// For more information, see: https://flutter.dev/to/review-gradle-config.
27-
minSdk = flutter.minSdkVersion
27+
minSdk = 23
2828
targetSdk = flutter.targetSdkVersion
2929
versionCode = flutter.versionCode
3030
versionName = flutter.versionName
Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import 'package:flutter/material.dart';
2+
import 'package:lamp_app/screens/living_room/widgets/lamp.dart';
3+
import 'package:lamp_app/screens/living_room/widgets/lamp_blub.dart';
4+
import 'package:lamp_app/screens/living_room/widgets/lamp_hanger_rope.dart';
5+
import 'package:lamp_app/screens/living_room/widgets/lamp_switch.dart';
6+
import 'package:lamp_app/screens/living_room/widgets/lamp_switch_rope.dart';
7+
import 'package:torch_light/torch_light.dart';
28

39
const darkGray = Color(0xFF232323);
410
const bulbOnColor = Color(0xFFFFE12C);
@@ -13,6 +19,8 @@ class LivingRoomScreen extends StatefulWidget {
1319
}
1420

1521
class _LivingRoomScreenState extends State<LivingRoomScreen> {
22+
bool isOn = false;
23+
1624
@override
1725
Widget build(BuildContext context) {
1826
final size = MediaQuery.of(context).size;
@@ -23,28 +31,55 @@ class _LivingRoomScreenState extends State<LivingRoomScreen> {
2331
fit: StackFit.expand,
2432
children: [
2533
LampHangerRope(height: screenHeight * 0.15),
34+
LampBlub(
35+
isOn: isOn,
36+
screenWidth: screenWidth,
37+
screenHeight: screenHeight,
38+
),
39+
Lamp(
40+
isOn: isOn,
41+
screenHeight: screenHeight,
42+
screenWidth: screenWidth,
43+
),
44+
LampSwitch(
45+
isOn: isOn,
46+
screenHeight: screenHeight,
47+
screenWidth: screenWidth,
48+
onTap: toggleTorch,
49+
),
50+
LampSwitchRope(
51+
isOn: isOn,
52+
screenHeight: screenHeight,
53+
screenWidth: screenWidth,
54+
),
2655
],
2756
),
2857
);
2958
}
30-
}
3159

32-
class LampHangerRope extends StatelessWidget {
33-
const LampHangerRope({
34-
super.key,
35-
required this.height,
36-
});
60+
void toggleTorch() async {
61+
try {
62+
final hasTorch = await TorchLight.isTorchAvailable();
63+
if (!hasTorch) {
64+
showSnackBar('Torch is not available!');
65+
return;
66+
}
3767

38-
final double height;
68+
isOn ? await TorchLight.disableTorch() : await TorchLight.enableTorch();
3969

40-
@override
41-
Widget build(BuildContext context) {
42-
return Positioned(
43-
top: 0,
44-
left: 80,
45-
width: 12,
46-
height: height,
47-
child: const ColoredBox(color: darkGray),
48-
);
70+
setState(() {
71+
isOn = !isOn;
72+
});
73+
} catch (e) {
74+
print(e.toString());
75+
showSnackBar('Something went!');
76+
}
77+
}
78+
79+
void showSnackBar(String message) {
80+
ScaffoldMessenger.of(context).hideCurrentSnackBar();
81+
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
82+
content: Text(message),
83+
));
4984
}
5085
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:lamp_app/screens/living_room/living_room.dart';
3+
4+
class Lamp extends StatelessWidget {
5+
const Lamp({
6+
super.key,
7+
required this.screenHeight,
8+
required this.screenWidth,
9+
required this.isOn,
10+
});
11+
12+
final double screenHeight;
13+
final double screenWidth;
14+
final bool isOn;
15+
16+
@override
17+
Widget build(BuildContext context) {
18+
return Positioned(
19+
top: screenHeight * 0.15,
20+
width: screenWidth,
21+
child: ClipPath(
22+
clipper: TrapzoidClipper(),
23+
child: Column(
24+
children: [
25+
Container(
26+
height: screenHeight * 0.25,
27+
decoration: const BoxDecoration(color: darkGray),
28+
),
29+
AnimatedContainer(
30+
duration: animationDuration,
31+
height: screenHeight * 0.75,
32+
decoration: BoxDecoration(
33+
gradient: isOn
34+
? LinearGradient(
35+
begin: Alignment.topCenter,
36+
end: Alignment.bottomCenter,
37+
colors: [
38+
bulbOnColor.withOpacity(0.4),
39+
bulbOnColor.withOpacity(0.1)
40+
])
41+
: null,
42+
),
43+
),
44+
],
45+
),
46+
),
47+
);
48+
}
49+
}
50+
51+
class TrapzoidClipper extends CustomClipper<Path> {
52+
@override
53+
getClip(Size size) {
54+
final width = size.width;
55+
final hieght = size.height;
56+
final path = Path();
57+
58+
path.moveTo(0, 0);
59+
path.lineTo(0, hieght);
60+
path.lineTo(width, hieght);
61+
path.lineTo(width, hieght * 0.75);
62+
path.lineTo(width * 0.5, 0);
63+
path.close();
64+
65+
return path;
66+
}
67+
68+
@override
69+
bool shouldReclip(covariant CustomClipper oldClipper) {
70+
return false;
71+
}
72+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:lamp_app/screens/living_room/living_room.dart';
3+
4+
class LampBlub extends StatelessWidget {
5+
const LampBlub({
6+
super.key,
7+
required this.screenWidth,
8+
required this.screenHeight,
9+
required this.isOn,
10+
});
11+
12+
final double screenWidth;
13+
final double screenHeight;
14+
final bool isOn;
15+
16+
@override
17+
Widget build(BuildContext context) {
18+
return Positioned(
19+
left: screenWidth * 0.1,
20+
top: screenHeight * 0.34,
21+
width: 100,
22+
height: 100,
23+
child: AnimatedContainer(
24+
duration: const Duration(milliseconds: 250),
25+
clipBehavior: Clip.hardEdge,
26+
decoration: BoxDecoration(
27+
color: isOn ? bulbOnColor : bulbOffColor, shape: BoxShape.circle),
28+
));
29+
}
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:lamp_app/screens/living_room/living_room.dart';
3+
4+
class LampHangerRope extends StatelessWidget {
5+
const LampHangerRope({
6+
super.key,
7+
required this.height,
8+
});
9+
10+
final double height;
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return Positioned(
15+
top: 0,
16+
left: 80,
17+
width: 12,
18+
height: height,
19+
child: const ColoredBox(color: darkGray),
20+
);
21+
}
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:lamp_app/screens/living_room/living_room.dart';
3+
4+
class LampSwitch extends StatelessWidget {
5+
const LampSwitch(
6+
{super.key,
7+
required this.screenHeight,
8+
required this.screenWidth,
9+
required this.isOn,
10+
this.onTap});
11+
12+
final double screenHeight;
13+
final double screenWidth;
14+
final bool isOn;
15+
final VoidCallback? onTap;
16+
17+
@override
18+
Widget build(BuildContext context) {
19+
return Positioned(
20+
top: screenHeight * 0.61,
21+
left: screenWidth * 0.5 - 14,
22+
height: 70,
23+
width: 30,
24+
child: GestureDetector(
25+
onTap: onTap,
26+
child: AnimatedContainer(
27+
duration: animationDuration,
28+
curve: Curves.fastEaseInToSlowEaseOut,
29+
padding: const EdgeInsets.all(4),
30+
decoration: BoxDecoration(
31+
color: isOn ? bulbOnColor : bulbOffColor,
32+
borderRadius: BorderRadius.circular(15),
33+
),
34+
alignment: isOn ? Alignment.bottomCenter : Alignment.topCenter,
35+
child: Container(
36+
width: 24,
37+
height: 24,
38+
decoration: const BoxDecoration(
39+
color: darkGray,
40+
shape: BoxShape.circle,
41+
),
42+
),
43+
),
44+
),
45+
);
46+
}
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:lamp_app/screens/living_room/living_room.dart';
3+
4+
class LampSwitchRope extends StatelessWidget {
5+
const LampSwitchRope({
6+
super.key,
7+
required this.screenHeight,
8+
required this.screenWidth,
9+
required this.isOn,
10+
});
11+
12+
final double screenHeight;
13+
final double screenWidth;
14+
final bool isOn;
15+
16+
@override
17+
Widget build(BuildContext context) {
18+
return AnimatedPositioned(
19+
duration: animationDuration,
20+
curve: Curves.fastEaseInToSlowEaseOut,
21+
width: 2,
22+
top: screenHeight * 0.4,
23+
bottom: screenHeight * (isOn ? 0.32 : 0.38),
24+
left: screenWidth * 0.5,
25+
child: Container(
26+
color: darkGray,
27+
),
28+
);
29+
}
30+
}

pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ packages:
192192
url: "https://pub.dev"
193193
source: hosted
194194
version: "0.7.2"
195+
torch_light:
196+
dependency: "direct main"
197+
description:
198+
name: torch_light
199+
sha256: a1397443a375c6991151547cb77361085df6cf8aa59999292e683db7385a0d15
200+
url: "https://pub.dev"
201+
source: hosted
202+
version: "1.1.0"
195203
vector_math:
196204
dependency: transitive
197205
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies:
3535
# The following adds the Cupertino Icons font to your application.
3636
# Use with the CupertinoIcons class for iOS style icons.
3737
cupertino_icons: ^1.0.8
38+
torch_light: ^1.1.0
3839

3940
dev_dependencies:
4041
flutter_test:

0 commit comments

Comments
 (0)