together_mobile/lib/screens/avatar_view/avatar_view_screen.dart

108 lines
3.3 KiB
Dart

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<AvatarViewScreen> createState() => _AvatarViewScreenState();
}
class _AvatarViewScreenState extends State<AvatarViewScreen> {
late int currentIndex = 0;
// void onPageChanged(int index) {
// setState(() {
// currentIndex = index;
// });
// }
@override
Widget build(BuildContext context) {
getIt.get<RouteState>().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: <Widget>[
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),
);
}
}