-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ui_storage): add StorageGridView widget #11206
Merged
+201
−24
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/firebase_ui_storage/example/lib/src/grid_view_app.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023, the Chromium project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:convert'; | ||
|
||
import 'package:firebase_storage/firebase_storage.dart'; | ||
import 'package:firebase_ui_storage/firebase_ui_storage.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'apps.dart'; | ||
|
||
class StorageGridViewApp extends StatelessWidget implements App { | ||
const StorageGridViewApp({super.key}); | ||
|
||
@override | ||
String get name => 'StorageGridView'; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return StorageGridView( | ||
ref: FirebaseStorage.instance.ref('list'), | ||
itemBuilder: (context, ref) { | ||
return Card( | ||
child: Center( | ||
child: FutureBuilder( | ||
future: ref.getData(), | ||
builder: (context, snapshot) { | ||
if (snapshot.hasError) { | ||
return Text(snapshot.error.toString()); | ||
} | ||
if (snapshot.hasData) { | ||
return Text(utf8.decode(snapshot.data!)); | ||
} | ||
|
||
return const CircularProgressIndicator(); | ||
}, | ||
), | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/firebase_ui_storage/lib/src/widgets/default_loading_indicator.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2023, the Chromium project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:firebase_ui_shared/firebase_ui_shared.dart'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. license header |
||
import 'package:flutter/widgets.dart'; | ||
|
||
class DefaultLoadingIndicator extends StatelessWidget { | ||
const DefaultLoadingIndicator({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const Center( | ||
child: LoadingIndicator( | ||
size: 32, | ||
borderWidth: 2, | ||
), | ||
); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
packages/firebase_ui_storage/lib/src/widgets/grid_view.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2023, the Chromium project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:firebase_storage/firebase_storage.dart'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. license header |
||
import 'package:firebase_ui_storage/firebase_ui_storage.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
|
||
import 'default_loading_indicator.dart'; | ||
|
||
Widget _defaultLoadingBuilder(BuildContext context) { | ||
return const DefaultLoadingIndicator(); | ||
} | ||
|
||
class StorageGridView extends StatefulWidget { | ||
/// The [Reference] to list items from. | ||
/// If not provided, a [loadingController] must be created and passed. | ||
final Reference? ref; | ||
|
||
final PaginatedLoadingController? loadingController; | ||
|
||
/// The number of items to load per page. | ||
/// Defaults to 50. | ||
final int pageSize; | ||
|
||
/// A builder that is called for the first page load. | ||
final Widget Function(BuildContext context) loadingBuilder; | ||
|
||
/// A builder that is called when an error occurs during page loading. | ||
final Widget Function( | ||
BuildContext context, | ||
Object? error, | ||
PaginatedLoadingController controller, | ||
)? errorBuilder; | ||
|
||
/// A builder that is called for each item in the list. | ||
final Widget Function(BuildContext context, Reference ref) itemBuilder; | ||
|
||
/// See [SliverGridDelegate]. | ||
final SliverGridDelegate gridDelegate; | ||
|
||
const StorageGridView({ | ||
super.key, | ||
this.ref, | ||
this.loadingController, | ||
this.pageSize = 50, | ||
this.loadingBuilder = _defaultLoadingBuilder, | ||
this.errorBuilder, | ||
this.gridDelegate = const SliverGridDelegateWithFixedCrossAxisCount( | ||
crossAxisCount: 3, | ||
), | ||
required this.itemBuilder, | ||
}) : assert( | ||
ref != null || loadingController != null, | ||
'ref or loadingController must be provided', | ||
); | ||
|
||
@override | ||
State<StorageGridView> createState() => _StorageGridViewState(); | ||
} | ||
|
||
class _StorageGridViewState extends State<StorageGridView> { | ||
late PaginatedLoadingController ctrl = widget.loadingController ?? | ||
PaginatedLoadingController( | ||
ref: widget.ref!, | ||
pageSize: widget.pageSize, | ||
); | ||
|
||
Widget gridBuilder(BuildContext context, List<Reference> items) { | ||
return GridView.builder( | ||
gridDelegate: widget.gridDelegate, | ||
itemCount: items.length, | ||
itemBuilder: (context, index) { | ||
if (ctrl.shouldLoadNextPage(index)) { | ||
ctrl.load(); | ||
} | ||
|
||
return widget.itemBuilder(context, items[index]); | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return AnimatedBuilder( | ||
animation: ctrl, | ||
builder: (context, _) { | ||
return switch (ctrl.state) { | ||
InitialPageLoading() => widget.loadingBuilder(context), | ||
PageLoadError( | ||
error: final error, | ||
items: final items, | ||
) => | ||
widget.errorBuilder != null | ||
? widget.errorBuilder!(context, error, ctrl) | ||
: gridBuilder(context, items ?? []), | ||
PageLoading(items: final items) => gridBuilder(context, items), | ||
PageLoadComplete(items: final items) => gridBuilder(context, items), | ||
}; | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always used that as a workaround. Do we have example of addPostCallback being used in official libs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure why is this a workaround, to me it's a valid usage :)
flutter itself uses it internally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, not sure why I always thought it was a "HACK"