-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui_storage): add StorageGridView widget (#11206)
Co-authored-by: Russell Wheatley <russellwheatley85@gmail.com>
- Loading branch information
1 parent
fd832fb
commit 48a3a39
Showing
8 changed files
with
201 additions
and
24 deletions.
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'; | ||
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'; | ||
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