[mob] perf: avoid unnecessary frame builds and computations when toggling full fullscreen when viewing a file (#1487)

This commit is contained in:
Ashil 2024-04-19 14:17:01 +05:30 committed by GitHub
parent 967ef2e3ea
commit 937267ed72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -53,47 +53,70 @@ class FileAppBar extends StatefulWidget {
class FileAppBarState extends State<FileAppBar> {
final _logger = Logger("FadingAppBar");
final List<Widget> _actions = [];
@override
void didUpdateWidget(FileAppBar oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.file.generatedID != widget.file.generatedID) {
_getActions();
}
}
@override
Widget build(BuildContext context) {
_logger.fine("building app bar ${widget.file.generatedID?.toString()}");
//When the widget is initialized, the actions are not available.
//Cannot call _getActions() in initState.
if (_actions.isEmpty) {
_getActions();
}
final isTrashedFile = widget.file is TrashFile;
final shouldShowActions = widget.shouldShowActions && !isTrashedFile;
return CustomAppBar(
ValueListenableBuilder(
valueListenable: widget.enableFullScreenNotifier,
builder: (context, bool isFullScreen, _) {
builder: (context, bool isFullScreen, child) {
return IgnorePointer(
ignoring: isFullScreen,
child: AnimatedOpacity(
opacity: isFullScreen ? 0 : 1,
duration: const Duration(milliseconds: 150),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.black.withOpacity(0.72),
Colors.black.withOpacity(0.6),
Colors.transparent,
],
stops: const [0, 0.2, 1],
),
),
child: _buildAppBar(),
),
child: child,
),
);
},
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.black.withOpacity(0.72),
Colors.black.withOpacity(0.6),
Colors.transparent,
],
stops: const [0, 0.2, 1],
),
),
child: AppBar(
iconTheme: const IconThemeData(
color: Colors.white,
), //same for both themes
actions: shouldShowActions ? _actions : [],
elevation: 0,
backgroundColor: const Color(0x00000000),
),
),
),
Size.fromHeight(Platform.isAndroid ? 84 : 96),
);
}
AppBar _buildAppBar() {
_logger.fine("building app bar ${widget.file.generatedID?.toString()}");
final List<Widget> actions = [];
final isTrashedFile = widget.file is TrashFile;
final shouldShowActions = widget.shouldShowActions && !isTrashedFile;
List<Widget> _getActions() {
_actions.clear();
final bool isOwnedByUser = widget.file.isOwner;
final bool isFileUploaded = widget.file.isUploaded;
bool isFileHidden = false;
@ -104,7 +127,7 @@ class FileAppBarState extends State<FileAppBar> {
false;
}
if (widget.file.isLiveOrMotionPhoto) {
actions.add(
_actions.add(
IconButton(
icon: const Icon(Icons.album_outlined),
onPressed: () {
@ -118,7 +141,7 @@ class FileAppBarState extends State<FileAppBar> {
}
// only show fav option for files owned by the user
if (isOwnedByUser && !isFileHidden && isFileUploaded) {
actions.add(
_actions.add(
Padding(
padding: const EdgeInsets.all(8),
child: FavoriteWidget(widget.file),
@ -126,7 +149,7 @@ class FileAppBarState extends State<FileAppBar> {
);
}
if (!isFileUploaded) {
actions.add(
_actions.add(
UploadIconWidget(
file: widget.file,
key: ValueKey(widget.file.tag),
@ -241,7 +264,7 @@ class FileAppBarState extends State<FileAppBar> {
}
}
if (items.isNotEmpty) {
actions.add(
_actions.add(
PopupMenuButton(
itemBuilder: (context) {
return items;
@ -262,13 +285,7 @@ class FileAppBarState extends State<FileAppBar> {
),
);
}
return AppBar(
iconTheme:
const IconThemeData(color: Colors.white), //same for both themes
actions: shouldShowActions ? actions : [],
elevation: 0,
backgroundColor: const Color(0x00000000),
);
return _actions;
}
Future<void> _handleHideRequest(BuildContext context) async {