forked from fossasia/pslab-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_scaffold_widget.dart
More file actions
202 lines (192 loc) · 6.01 KB
/
Copy pathmain_scaffold_widget.dart
File metadata and controls
202 lines (192 loc) · 6.01 KB
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
201
202
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:pslab/providers/board_state_provider.dart';
import 'navigation_drawer.dart';
class MainScaffold extends StatefulWidget {
final String title;
final Widget body;
final Key? scaffoldKey;
final int index;
final List<Widget>? actions;
final String icUsbDisconnected = 'assets/icons/ic_usb_disconnected.png';
final String icUsbConnected = 'assets/icons/ic_usb_connected.png';
final String icWiFiConnected = 'assets/icons/ic_wifi_connected.png';
final bool showSearch;
final Function(String)? onSearchChanged;
final String? searchHint;
const MainScaffold({
super.key,
required this.body,
required this.title,
this.scaffoldKey,
this.actions,
required this.index,
this.showSearch = false,
this.onSearchChanged,
this.searchHint,
});
@override
State<StatefulWidget> createState() => _MainScaffoldState();
}
class _MainScaffoldState extends State<MainScaffold>
with SingleTickerProviderStateMixin {
bool _isSearching = false;
final TextEditingController _searchController = TextEditingController();
late AnimationController _animationController;
@override
void initState() {
super.initState();
_animationController = AnimationController(
duration: const Duration(milliseconds: 300),
vsync: this,
);
}
@override
void dispose() {
_searchController.dispose();
_animationController.dispose();
super.dispose();
}
void _toggleSearch() {
setState(() {
if (_isSearching) {
_isSearching = false;
_animationController.reverse();
_searchController.clear();
if (widget.onSearchChanged != null) {
widget.onSearchChanged!('');
}
} else {
_isSearching = true;
_animationController.forward();
}
});
}
void _onSearchChanged(String query) {
if (widget.onSearchChanged != null) {
widget.onSearchChanged!(query);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.surface,
resizeToAvoidBottomInset: true,
appBar: AppBar(
systemOverlayStyle:
const SystemUiOverlayStyle(statusBarColor: Color(0xFFD32F2F)),
leading: Builder(builder: (context) {
return IconButton(
onPressed: () {
Scaffold.of(context).openDrawer();
},
icon: const Icon(
Icons.menu,
color: Colors.white,
),
);
}),
backgroundColor: const Color(0xFFD32F2F),
title: AnimatedSwitcher(
duration: const Duration(milliseconds: 0),
transitionBuilder: (Widget child, Animation<double> animation) {
return FadeTransition(
opacity: animation,
child: child,
);
},
child: _isSearching
? TextField(
key: const ValueKey('search_field'),
controller: _searchController,
onChanged: _onSearchChanged,
autofocus: true,
style: const TextStyle(
color: Colors.white,
fontSize: 18,
),
decoration: InputDecoration(
hintText: widget.searchHint,
hintStyle: const TextStyle(
color: Colors.white70,
fontSize: 18,
),
border: InputBorder.none,
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
contentPadding: EdgeInsets.zero,
),
cursorColor: Colors.white,
)
: Text(
key: ValueKey('title_${widget.title}'),
widget.title,
style: const TextStyle(
color: Colors.white,
fontSize: 18,
),
),
),
actions: _isSearching
? [
IconButton(
icon: const Icon(
Icons.clear,
color: Colors.white,
),
onPressed: () {
if (_searchController.text.isNotEmpty) {
_searchController.clear();
_onSearchChanged('');
} else {
_toggleSearch();
}
},
),
]
: [
if (widget.showSearch)
IconButton(
icon: const Icon(
Icons.search,
color: Colors.white,
),
onPressed: _toggleSearch,
),
Consumer<BoardStateProvider>(
builder: (context, provider, _) {
return IconButton(
icon: Image.asset(
provider.pslabIsConnected
? (provider.scienceLabCommon.isWiFiConnected()
? widget.icWiFiConnected
: widget.icUsbConnected)
: widget.icUsbDisconnected,
width: 24,
height: 24,
),
onPressed: () {
/**/
},
);
},
),
IconButton(
icon: const Icon(
Icons.more_vert,
color: Colors.white,
),
onPressed: () {
/**/
},
),
],
),
body: widget.body,
drawer: NavDrawer(
selectedIndex: widget.index,
),
);
}
}