12.3 商品大图显示
实现步骤:
第 1 步:安装插件 photo_view
由于 photo_view
插件 github
源码更新快,我们直接引用代码仓
你也可以用 flutter pub add photo_view
pubspec.yaml
1 2 3 4 5
| photo_view: git: url: https://github.com/bluefireteam/photo_view ref: master
|
第 2 步:图片浏览组件
lib/common/components/gallery.dart
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
| import 'package:flutter/material.dart'; import 'package:flutter_woo_commerce_getx_learn/common/index.dart'; import 'package:photo_view/photo_view.dart'; import 'package:photo_view/photo_view_gallery.dart';
class GalleryWidget extends StatelessWidget { final int initialIndex;
final List<String> items;
const GalleryWidget({ Key? key, required this.initialIndex, required this.items, }) : super(key: key);
@override Widget build(BuildContext context) { return GestureDetector( behavior: HitTestBehavior.opaque, onTap: () => Navigator.pop(context), child: Scaffold( extendBodyBehindAppBar: true,
backgroundColor: Colors.black,
appBar: AppBar(),
body: PhotoViewGallery.builder( scrollPhysics: const BouncingScrollPhysics(), builder: (BuildContext context, int index) { return PhotoViewGalleryPageOptions( imageProvider: NetworkImage(items[index]), initialScale: PhotoViewComputedScale.contained, minScale: PhotoViewComputedScale.contained, maxScale: PhotoViewComputedScale.covered * 4, ); },
loadingBuilder: (context, event) => CircularProgressIndicator( backgroundColor: AppColors.highlight, value: event == null ? 0 : event.cumulativeBytesLoaded / (event.expectedTotalBytes ?? 0), ).tightSize(30).center(),
itemCount: items.length,
pageController: PageController(initialPage: initialIndex), ), ), ); } }
|
第 3 步:控制器
lib/pages/goods/product_details/controller.dart
1 2 3 4 5 6 7
| void onGalleryTap(int index, KeyValueModel item) { Get.to(GalleryWidget( initialIndex: index, items: bannerItems.map<String>((e) => e.value!).toList(), )); }
|
第 4 步:视图
lib/pages/goods/product_details/view.dart
1 2 3 4 5 6 7 8 9 10 11 12 13
| Widget _buildBanner() { return GetBuilder<ProductDetailsController>( id: "product_banner", tag: tag, builder: ( _) { return CarouselWidget( onTap: controller.onGalleryTap, ... ); }).backgroundColor(AppColors.surfaceVariant); }
|
提交代码到 git