Skip to content

Commit

Permalink
(viewer) Split viewer to vertical and horizontal
Browse files Browse the repository at this point in the history
  • Loading branch information
violet-dev committed Jul 17, 2020
1 parent a04822c commit 7631e5e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 115 deletions.
4 changes: 4 additions & 0 deletions lib/component/pixiv/pixiv.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This source code is a part of Project Violet.
// Copyright (C) 2020. violet-team. Licensed under the MIT License.

// Reference https://github.com/rollrat/downloader/blob/master/Koromo_Copy.Framework/Extractor/PixivExtractor.cs
Empty file.
2 changes: 1 addition & 1 deletion lib/pages/test/test_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import 'package:violet/files.dart';
import 'package:violet/other/flare_artboard.dart';
import 'package:violet/pages/database_download/decompress.dart';
import 'package:violet/update_sync.dart';
import 'package:violet/pages/viewer/viewer_widget.dart';
import 'package:violet/pages/viewer/vertical_viewer_widget.dart';
import 'package:flutter_sidekick/flutter_sidekick.dart';
import 'package:flutter/rendering.dart';
import 'package:http/http.dart' as http;
Expand Down
19 changes: 19 additions & 0 deletions lib/pages/viewer/gallery_item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This source code is a part of Project Violet.
// Copyright (C) 2020. violet-team. Licensed under the MIT License.

class GalleryExampleItem {
GalleryExampleItem({
this.id,
this.url,
this.headers,
this.isSvg = false,
this.loaded = false,
});

final String id;
final String url;
final Map<String, String> headers;
final bool isSvg;
double height;
bool loaded;
}
105 changes: 105 additions & 0 deletions lib/pages/viewer/horizontal_viewer_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// This source code is a part of Project Violet.
// Copyright (C) 2020. violet-team. Licensed under the MIT License.

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:violet/settings.dart';
import 'package:violet/pages/viewer/gallery_item.dart';

class GalleryPhotoViewWrapper extends StatefulWidget {
GalleryPhotoViewWrapper({
this.loadingBuilder,
this.backgroundDecoration,
this.initialIndex,
@required this.galleryItems,
this.totalPage,
this.scrollDirection = Axis.horizontal,
}) : pageController = PageController(initialPage: initialIndex);

final LoadingBuilder loadingBuilder;
final Decoration backgroundDecoration;
final int initialIndex;
final PageController pageController;
final List<GalleryExampleItem> galleryItems;
final Axis scrollDirection;
final int totalPage;
int currentIndex;

@override
State<StatefulWidget> createState() {
return _GalleryPhotoViewWrapperState();
}
}

class _GalleryPhotoViewWrapperState extends State<GalleryPhotoViewWrapper> {
@override
void initState() {
widget.currentIndex = widget.initialIndex;
super.initState();
}

void onPageChanged(int index) {
setState(() {
widget.currentIndex = index;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: widget.backgroundDecoration,
constraints: BoxConstraints.expand(
height: MediaQuery.of(context).size.height,
),
child: Stack(
alignment: Alignment.bottomRight,
children: <Widget>[
PhotoViewGallery.builder(
scrollPhysics: const BouncingScrollPhysics(),
builder: _buildItem,
itemCount: widget.galleryItems.length,
loadingBuilder: widget.loadingBuilder,
backgroundDecoration: widget.backgroundDecoration,
pageController: widget.pageController,
onPageChanged: onPageChanged,
scrollDirection: widget.scrollDirection,
reverse: Settings.rightToLeft,
),
Container(
padding: const EdgeInsets.all(8.0),
child: Text(
"${widget.currentIndex + 1}/${widget.totalPage}",
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
decoration: null,
),
),
)
],
),
),
);
}

PhotoViewGalleryPageOptions _buildItem(BuildContext context, int index) {
final GalleryExampleItem item = widget.galleryItems[index];
return PhotoViewGalleryPageOptions(
imageProvider: CachedNetworkImageProvider(
item.url,
headers: item.headers,
//(context, url) => Image.file(File('assets/images/loading.gif')),
),
// NetworkImage(item.url, headers: item.headers),
initialScale: PhotoViewComputedScale.contained,
//minScale: PhotoViewComputedScale.contained * (0.5 + index / 10),
//maxScale: PhotoViewComputedScale.covered * 1.1,
minScale: PhotoViewComputedScale.contained * 1.0,
maxScale: PhotoViewComputedScale.contained * 5.0,
heroAttributes: PhotoViewHeroAttributes(tag: item.id),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import 'package:violet/database/user/record.dart';
import 'package:violet/dialogs.dart';
import 'package:violet/settings.dart';
import 'package:violet/locale.dart';
import 'package:violet/pages/viewer/gallery_item.dart';
import 'package:violet/pages/viewer/horizontal_viewer_widget.dart';

class ViewerWidget extends StatelessWidget {
final List<String> urls;
Expand Down Expand Up @@ -215,23 +217,6 @@ class ViewerWidget extends StatelessWidget {
String latestLabel = '';
}

class GalleryExampleItem {
GalleryExampleItem({
this.id,
this.url,
this.headers,
this.isSvg = false,
this.loaded = false,
});

final String id;
final String url;
final Map<String, String> headers;
final bool isSvg;
double height;
bool loaded;
}

class GalleryExampleItemThumbnail extends StatelessWidget {
GalleryExampleItemThumbnail({
Key key,
Expand Down Expand Up @@ -332,99 +317,3 @@ class GalleryExampleItemThumbnail extends StatelessWidget {
return completer.future;
}
}

class GalleryPhotoViewWrapper extends StatefulWidget {
GalleryPhotoViewWrapper({
this.loadingBuilder,
this.backgroundDecoration,
this.initialIndex,
@required this.galleryItems,
this.totalPage,
this.scrollDirection = Axis.horizontal,
}) : pageController = PageController(initialPage: initialIndex);

final LoadingBuilder loadingBuilder;
final Decoration backgroundDecoration;
final int initialIndex;
final PageController pageController;
final List<GalleryExampleItem> galleryItems;
final Axis scrollDirection;
final int totalPage;
int currentIndex;

@override
State<StatefulWidget> createState() {
return _GalleryPhotoViewWrapperState();
}
}

class _GalleryPhotoViewWrapperState extends State<GalleryPhotoViewWrapper> {
@override
void initState() {
widget.currentIndex = widget.initialIndex;
super.initState();
}

void onPageChanged(int index) {
setState(() {
widget.currentIndex = index;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: widget.backgroundDecoration,
constraints: BoxConstraints.expand(
height: MediaQuery.of(context).size.height,
),
child: Stack(
alignment: Alignment.bottomRight,
children: <Widget>[
PhotoViewGallery.builder(
scrollPhysics: const BouncingScrollPhysics(),
builder: _buildItem,
itemCount: widget.galleryItems.length,
loadingBuilder: widget.loadingBuilder,
backgroundDecoration: widget.backgroundDecoration,
pageController: widget.pageController,
onPageChanged: onPageChanged,
scrollDirection: widget.scrollDirection,
reverse: Settings.rightToLeft,
),
Container(
padding: const EdgeInsets.all(8.0),
child: Text(
"${widget.currentIndex + 1}/${widget.totalPage}",
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
decoration: null,
),
),
)
],
),
),
);
}

PhotoViewGalleryPageOptions _buildItem(BuildContext context, int index) {
final GalleryExampleItem item = widget.galleryItems[index];
return PhotoViewGalleryPageOptions(
imageProvider: CachedNetworkImageProvider(
item.url,
headers: item.headers,
//(context, url) => Image.file(File('assets/images/loading.gif')),
),
// NetworkImage(item.url, headers: item.headers),
initialScale: PhotoViewComputedScale.contained,
//minScale: PhotoViewComputedScale.contained * (0.5 + index / 10),
//maxScale: PhotoViewComputedScale.covered * 1.1,
minScale: PhotoViewComputedScale.contained * 1.0,
maxScale: PhotoViewComputedScale.contained * 5.0,
heroAttributes: PhotoViewHeroAttributes(tag: item.id),
);
}
}
2 changes: 1 addition & 1 deletion lib/pages/viewer/viewer_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (C) 2020. violet-team. Licensed under the MIT License.

import 'package:flutter/material.dart';
import 'package:violet/pages/viewer/viewer_widget.dart';
import 'package:violet/pages/viewer/vertical_viewer_widget.dart';

class ViewerPage extends StatefulWidget {
final List<String> images;
Expand Down

0 comments on commit 7631e5e

Please sign in to comment.