-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll_page.dart
133 lines (120 loc) · 3.66 KB
/
scroll_page.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_mediator_lite/mediator.dart';
//* Step1: import the var.dart
import '../var.dart' show opacityValue;
class CustomAppBar extends StatelessWidget {
const CustomAppBar({required this.header, Key? key}) : super(key: key);
final Widget header;
@override
Widget build(BuildContext context) {
//* Step3: Create a consume widget with
//* `globalConsume` or `watchedVar.consume` to register the
//* watched variable to the host to rebuild it when updating.
return globalConsume(
() => Container(
padding: const EdgeInsets.symmetric(
vertical: 10.0,
horizontal: 24.0,
),
color: Colors.black.withOpacity(opacityValue.value),
child: header,
),
);
}
}
class ScrollPage extends StatefulWidget {
const ScrollPage({Key? key}) : super(key: key);
@override
_ScrollPageState createState() => _ScrollPageState();
}
class _ScrollPageState extends State<ScrollPage> {
final _scrollController = ScrollController();
@override
void initState() {
_scrollController.addListener(() {
//* Step4: Make an update to the watched variable.
opacityValue.value =
(_scrollController.offset / 350).clamp(0, 1).toDouble();
});
super.initState();
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
appBar: PreferredSize(
preferredSize: Size(MediaQuery.of(context).size.width, 150),
child: CustomAppBar(
header: SafeArea(
child: Text(
'ScrollOffset Demo',
style: TextStyle(
color: Colors.lightBlue.shade400,
backgroundColor: Colors.transparent,
fontSize: 22,
),
),
),
),
),
body: CustomScrollView(
controller: _scrollController,
key: const PageStorageKey('ScrollView'),
slivers: [
SliverToBoxAdapter(
child: Container(
decoration: const BoxDecoration(
color: Colors.orangeAccent,
image: DecorationImage(
image: AssetImage('assets/trees.png'),
fit: BoxFit.fitWidth,
alignment: Alignment.topCenter,
),
),
alignment: Alignment.center,
height: 250,
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return ListTile(
leading: Icon(
Icons.people_outline_rounded,
color: Colors.blue.shade400,
),
title: Text(
data[index].name,
style: TextStyle(color: Colors.blue.shade400),
),
subtitle: Text(
'Price: ${data[index].price}',
style: TextStyle(color: Colors.blue.shade400),
),
);
},
childCount: data.length,
),
),
],
),
);
}
}
class Product {
const Product({required this.name, required this.price});
final String name;
final int price;
}
final data = List<Product>.generate(100, (i) {
return Product(
name: 'Description $i',
price: Random().nextInt(100) + 1,
);
});