-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy path59. flutter_parallax_effect.dart
82 lines (74 loc) · 2.26 KB
/
59. flutter_parallax_effect.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:sensors_plus/sensors_plus.dart';
class FlutterParallaxCardEffectView extends StatelessWidget {
const FlutterParallaxCardEffectView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(body: ParallaxCardWidget());
}
}
class ParallaxCardWidget extends StatefulWidget {
const ParallaxCardWidget({Key? key}) : super(key: key);
@override
State<ParallaxCardWidget> createState() => _ParallaxCardWidgetState();
}
class _ParallaxCardWidgetState extends State<ParallaxCardWidget> {
double? _accelerometerXAxis;
late final StreamSubscription<dynamic> _streamSubscription;
@override
void initState() {
super.initState();
_streamSubscription = accelerometerEvents.listen(
(AccelerometerEvent event) {
setState(() {
_accelerometerXAxis = event.x;
});
},
);
}
@override
void dispose() {
super.dispose();
_streamSubscription.cancel();
}
@override
Widget build(BuildContext context) {
return Builder(
builder: (BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Stack(
alignment: Alignment.center,
children: [
AnimatedPositioned(
curve: Curves.easeIn,
duration: const Duration(milliseconds: 100),
left: 0,
right: _accelerometerXAxis != null
? (-320 + _accelerometerXAxis! * 30)
: -320,
child: Image.asset(
'assets/space4.jpg',
fit: BoxFit.contain,
)),
const Center(
child: SizedBox(
height: 200,
width: 200,
child: CircleAvatar(
backgroundImage: AssetImage('assets/profile.png')),
),
),
const Positioned(
top: 250,
child: Text("Hey there!",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 40)))
],
),
);
},
);
}
}