-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.dart
200 lines (186 loc) · 6.24 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import 'package:bottom_sheet_bar/bottom_sheet_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const ExampleApp());
}
class BottomSheetBarPage extends StatefulWidget {
final String title;
const BottomSheetBarPage({Key? key, this.title = ''}) : super(key: key);
@override
BottomSheetBarPageState createState() => BottomSheetBarPageState();
}
class BottomSheetBarPageState extends State<BottomSheetBarPage> {
bool _isLocked = false;
bool _isCollapsed = true;
bool _isExpanded = false;
int _listSize = 5;
final _bsbController = BottomSheetBarController();
final _listSizeController = TextEditingController(text: '5');
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: [
if (_isCollapsed)
IconButton(
icon: const Icon(Icons.open_in_full),
onPressed: _bsbController.expand,
),
if (_isExpanded)
IconButton(
icon: const Icon(Icons.close),
onPressed: _bsbController.collapse,
),
],
),
body: BottomSheetBar(
backdropColor: Colors.green.withOpacity(0.8),
locked: _isLocked,
controller: _bsbController,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(32.0),
topRight: Radius.circular(32.0),
),
borderRadiusExpanded: const BorderRadius.only(
topLeft: Radius.circular(0.0),
topRight: Radius.circular(0.0),
),
boxShadows: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5.0,
blurRadius: 32.0,
offset: const Offset(0, 0), // changes position of shadow
),
],
expandedBuilder: (scrollController) {
final itemList =
List<int>.generate(_listSize, (index) => index + 1);
// Wrapping the returned widget with [Material] for tap effects
return Material(
color: Colors.transparent,
child: CustomScrollView(
controller: scrollController,
shrinkWrap: true,
slivers: [
SliverFixedExtentList(
itemExtent: 56.0, // I'm forcing item heights
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(
title: Text(
itemList[index].toString(),
style: const TextStyle(fontSize: 20.0),
),
onTap: () => showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(
itemList[index].toString(),
),
),
),
),
childCount: _listSize,
),
),
],
),
);
},
collapsed: TextButton(
onPressed: () => _bsbController.expand(),
child: Text(
'Click${_isLocked ? "" : " or swipe"} to expand',
style: const TextStyle(color: Colors.white),
),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('BottomSheetBar is'),
Text(
_isLocked ? 'Locked' : 'Unlocked',
style: Theme.of(context).textTheme.bodyLarge,
),
Text(
_isLocked
? 'Bottom sheet cannot be expanded or collapsed by swiping'
: 'Swipe it to expand or collapse the bottom sheet',
textAlign: TextAlign.center,
),
SizedBox(
width: 250,
child: TextField(
textAlign: TextAlign.center,
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
controller: _listSizeController,
decoration: const InputDecoration(
hintText: 'Number of expanded list-items'),
),
),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _toggleLock,
tooltip: 'Toggle Lock',
child:
_isLocked ? const Icon(Icons.lock) : const Icon(Icons.lock_open),
),
);
@override
void dispose() {
_bsbController.removeListener(_onBsbChanged);
super.dispose();
}
@override
void initState() {
_bsbController.addListener(_onBsbChanged);
_listSizeController.addListener(_onListSizeChanged);
super.initState();
}
void _onBsbChanged() {
if (_bsbController.isCollapsed && !_isCollapsed) {
setState(() {
_isCollapsed = true;
_isExpanded = false;
});
} else if (_bsbController.isExpanded && !_isExpanded) {
setState(() {
_isCollapsed = false;
_isExpanded = true;
});
}
}
void _onListSizeChanged() {
_listSize = int.tryParse(_listSizeController.text) ?? 5;
}
void _toggleLock() {
setState(() {
_isLocked = !_isLocked;
});
}
}
class ExampleApp extends StatelessWidget {
const ExampleApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BottomSheetBar Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
bottomAppBarTheme: const BottomAppBarTheme(
color: Colors.lightBlueAccent,
),
),
home: const BottomSheetBarPage(title: 'BottomSheetBar'),
);
}
}