-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy path24. flutter_expandable_drawer.dart
132 lines (123 loc) · 4.3 KB
/
24. flutter_expandable_drawer.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
import 'package:flutter/material.dart';
import 'package:ig_posts/features/1.flutter_frost_glass.view.dart';
import 'package:ig_posts/features/5.flutter_swiggy_button.dart';
import 'package:provider/provider.dart';
class DrawerNotifier extends ChangeNotifier {
double drawerWidth = 300;
bool isHalfExpanded = false;
void setExpansion({required bool isHalf}) {
isHalfExpanded = !isHalfExpanded;
if (isHalf) {
drawerWidth = 250;
notifyListeners();
} else {
drawerWidth = 100;
notifyListeners();
}
}
}
class FlutterExpandableDrawer extends StatelessWidget {
const FlutterExpandableDrawer({super.key});
@override
Widget build(BuildContext context) {
DrawerNotifier drawerNotifier({required bool renderUI}) =>
Provider.of<DrawerNotifier>(context, listen: renderUI);
bool isHalfExpanded = drawerNotifier(renderUI: true).isHalfExpanded;
double drawerWidth = drawerNotifier(renderUI: true).drawerWidth;
Widget buildExpansionButton() {
if (isHalfExpanded) {
return CircleAvatar(
backgroundColor: bgColor,
child: IconButton(
onPressed: () {
drawerNotifier(renderUI: false).setExpansion(isHalf: false);
},
icon: const Icon(Icons.close, color: Colors.white)),
);
} else {
return CircleAvatar(
backgroundColor: bgColor,
child: IconButton(
onPressed: () {
drawerNotifier(renderUI: false).setExpansion(isHalf: true);
},
icon: const Icon(Icons.open_in_browser, color: Colors.white)),
);
}
}
Widget shell(
{required double width,
required IconData iconData,
required String title,
required bool isHalfExpanded}) {
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Container(
width: width,
height: 50,
decoration: BoxDecoration(
color: bgColor, borderRadius: BorderRadius.circular(25)),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(iconData, color: Colors.white),
if (isHalfExpanded) const SizedBox(width: 20),
if (isHalfExpanded) Text(title, style: regulerText),
],
),
),
);
}
Widget buildSecondaryButton(
{required IconData iconData, required String title}) {
if (isHalfExpanded) {
return shell(
isHalfExpanded: isHalfExpanded,
width: 200,
iconData: iconData,
title: title);
} else {
return shell(
isHalfExpanded: isHalfExpanded,
width: 50,
iconData: iconData,
title: title);
}
}
return Scaffold(
appBar: AppBar(
title: Text("Flutter Expandable Drawer", style: boldText()),
backgroundColor: bgColorFaint),
backgroundColor: bgColor,
drawer: Drawer(
width: drawerWidth,
backgroundColor: Colors.transparent,
child: Container(
decoration: BoxDecoration(
color: bgColorFaint, borderRadius: BorderRadius.circular(50)),
child: Column(
children: [
const SizedBox(height: 100),
buildExpansionButton(),
const SizedBox(height: 20),
buildSecondaryButton(iconData: Icons.home, title: "Home"),
buildSecondaryButton(iconData: Icons.info, title: "Info"),
buildSecondaryButton(iconData: Icons.data_array, title: "Data"),
buildSecondaryButton(
iconData: Icons.meeting_room, title: "Meeting"),
buildSecondaryButton(
iconData: Icons.severe_cold, title: "Weather"),
buildSecondaryButton(
iconData: Icons.perm_device_info, title: "Device"),
buildSecondaryButton(
iconData: Icons.fullscreen, title: "Webview"),
buildSecondaryButton(
iconData: Icons.settings, title: "Settings"),
buildSecondaryButton(iconData: Icons.logout, title: "Logout"),
],
)),
),
body: Container(),
);
}
}