import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:photo_view/photo_view.dart'; import 'package:photo_view/photo_view_gallery.dart'; import '/models/init_get_it.dart'; import '/models/route_state_model.dart'; import '/request/server.dart'; enum AvatarType { groupChat, user, } // https://github.com/bluefireteam/photo_view/blob/main/example/lib/screens/examples/gallery/gallery_example.dart class AvatarViewScreen extends StatefulWidget { AvatarViewScreen({ super.key, this.loadingBuilder, required this.isMyself, required this.avatarType, required this.avatar, this.scrollDirection = Axis.horizontal, }) : pageController = PageController(initialPage: 0); final LoadingBuilder? loadingBuilder; final PageController pageController; final Axis scrollDirection; final bool isMyself; final AvatarType avatarType; final String avatar; @override State createState() => _AvatarViewScreenState(); } class _AvatarViewScreenState extends State { late int currentIndex = 0; // void onPageChanged(int index) { // setState(() { // currentIndex = index; // }); // } @override Widget build(BuildContext context) { getIt.get().changeRoute('AvatarView'); return Scaffold( body: GestureDetector( onTap: () { context.pop(); }, child: Container( decoration: const BoxDecoration(color: Colors.black), constraints: BoxConstraints.expand( height: MediaQuery.of(context).size.height, ), child: Stack( alignment: Alignment.bottomCenter, children: [ PhotoViewGallery.builder( scrollPhysics: const BouncingScrollPhysics(), builder: _buildItem, itemCount: 1, loadingBuilder: widget.loadingBuilder, backgroundDecoration: const BoxDecoration(color: Colors.black), pageController: widget.pageController, scrollDirection: widget.scrollDirection, ), if (widget.isMyself) FilledButton( onPressed: () { context.pushReplacementNamed('ChangeMyAvatar'); }, style: FilledButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), fixedSize: const Size(300, 30), ), child: const Text( '更换头像', style: TextStyle(fontSize: 16), ), ), ], ), ), ), ); } PhotoViewGalleryPageOptions _buildItem(BuildContext context, int index) { return PhotoViewGalleryPageOptions( imageProvider: CachedNetworkImageProvider( '$userAvatarsUrl/${widget.avatar}', ), initialScale: PhotoViewComputedScale.contained, minScale: PhotoViewComputedScale.contained * 0.5, maxScale: PhotoViewComputedScale.covered * 4.1, // heroAttributes: PhotoViewHeroAttributes(tag: widget.heroTag), ); } }