-
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Better product image view (#1122)
* feat: Better product image view * Refactor * Update product_image_gallery_view.dart
- Loading branch information
Showing
5 changed files
with
153 additions
and
86 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
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
15 changes: 15 additions & 0 deletions
15
packages/smooth_app/lib/data_models/product_image_data.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,15 @@ | ||
import 'package:openfoodfacts/model/ProductImage.dart'; | ||
|
||
class ProductImageData { | ||
const ProductImageData({ | ||
required this.imageField, | ||
required this.title, | ||
required this.buttonText, | ||
this.imageUrl, | ||
}); | ||
|
||
final ImageField imageField; | ||
final String title; | ||
final String buttonText; | ||
final String? imageUrl; | ||
} |
106 changes: 106 additions & 0 deletions
106
packages/smooth_app/lib/pages/product/product_image_gallery_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,106 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
import 'package:photo_view/photo_view.dart'; | ||
import 'package:photo_view/photo_view_gallery.dart'; | ||
import 'package:smooth_app/data_models/product_image_data.dart'; | ||
import 'package:smooth_app/generic_lib/widgets/smooth_gauge.dart'; | ||
|
||
class ProductImageGalleryView extends StatefulWidget { | ||
const ProductImageGalleryView({ | ||
required this.title, | ||
required this.productImageData, | ||
required this.allProductImagesData, | ||
}); | ||
|
||
final String title; | ||
final ProductImageData productImageData; | ||
final List<ProductImageData> allProductImagesData; | ||
|
||
@override | ||
State<ProductImageGalleryView> createState() => | ||
_ProductImageGalleryViewState(); | ||
} | ||
|
||
class _ProductImageGalleryViewState extends State<ProductImageGalleryView> { | ||
late final PageController _controller; | ||
late final List<ProductImageData> images = <ProductImageData>[]; | ||
late String title; | ||
|
||
@override | ||
void initState() { | ||
title = widget.title; | ||
|
||
for (final ProductImageData element in widget.allProductImagesData) { | ||
if (element.imageUrl != null) { | ||
images.add(element); | ||
} | ||
} | ||
|
||
_controller = PageController( | ||
initialPage: widget.allProductImagesData.indexOf( | ||
images.firstWhere((ProductImageData element) => | ||
element.imageUrl == widget.productImageData.imageUrl), | ||
), | ||
); | ||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final AppLocalizations appLocalizations = AppLocalizations.of(context)!; | ||
|
||
//When all are empty there shouldn't be a way to access this page | ||
if (images.isEmpty) { | ||
return Scaffold( | ||
body: Center( | ||
child: Text(appLocalizations.error), | ||
), | ||
); | ||
} | ||
return Scaffold( | ||
extendBodyBehindAppBar: true, | ||
appBar: AppBar( | ||
backgroundColor: Colors.transparent, | ||
elevation: 0, | ||
title: Text(title), | ||
), | ||
backgroundColor: Colors.black, | ||
body: PhotoViewGallery.builder( | ||
pageController: _controller, | ||
scrollPhysics: const BouncingScrollPhysics(), | ||
builder: (BuildContext context, int index) { | ||
return PhotoViewGalleryPageOptions( | ||
imageProvider: NetworkImage(images[index].imageUrl!), | ||
initialScale: PhotoViewComputedScale.contained * 0.8, | ||
minScale: PhotoViewComputedScale.contained * 0.8, | ||
maxScale: PhotoViewComputedScale.covered * 1.1, | ||
heroAttributes: | ||
PhotoViewHeroAttributes(tag: images[index].imageUrl!), | ||
); | ||
}, | ||
itemCount: images.length, | ||
loadingBuilder: | ||
(final BuildContext context, final ImageChunkEvent? event) { | ||
return Center( | ||
child: SmoothGauge( | ||
color: Theme.of(context).colorScheme.onBackground, | ||
value: event == null || | ||
event.expectedTotalBytes == null || | ||
event.expectedTotalBytes == 0 | ||
? 0 | ||
: event.cumulativeBytesLoaded / event.expectedTotalBytes!, | ||
), | ||
); | ||
}, | ||
backgroundDecoration: const BoxDecoration( | ||
color: Colors.black, | ||
), | ||
onPageChanged: (int index) { | ||
setState(() { | ||
title = images[index].title; | ||
}); | ||
}, | ||
), | ||
); | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
packages/smooth_app/lib/pages/product/product_image_page.dart
This file was deleted.
Oops, something went wrong.