Refactor zoom button

This commit is contained in:
Ming Ming 2021-07-25 16:52:24 +08:00
parent 942f61d90d
commit 6a0374d6c0
5 changed files with 118 additions and 103 deletions

View file

@ -9,8 +9,8 @@ import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/widget/album_viewer_app_bar.dart';
import 'package:nc_photos/widget/popup_menu_zoom.dart';
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
import 'package:nc_photos/widget/zoom_menu_button.dart';
mixin AlbumViewerMixin<T extends StatefulWidget>
on SelectableItemStreamListMixin<T> {
@ -44,23 +44,17 @@ mixin AlbumViewerMixin<T extends StatefulWidget>
album: album,
coverPreviewUrl: _coverPreviewUrl,
actions: [
PopupMenuButton(
icon: const Icon(Icons.photo_size_select_large),
tooltip: L10n.of(context).zoomTooltip,
itemBuilder: (context) => [
PopupMenuZoom(
initialValue: _thumbZoomLevel,
minValue: 0,
maxValue: 2,
onChanged: (value) {
ZoomMenuButton(
initialZoom: _thumbZoomLevel,
minZoom: 0,
maxZoom: 2,
onZoomChanged: (value) {
setState(() {
_thumbZoomLevel = value.round();
});
Pref.inst().setAlbumViewerZoomLevel(_thumbZoomLevel);
},
),
],
),
...(actions ?? []),
PopupMenuButton<int>(
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,

View file

@ -19,9 +19,9 @@ import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/use_case/update_property.dart';
import 'package:nc_photos/widget/photo_list_item.dart';
import 'package:nc_photos/widget/popup_menu_zoom.dart';
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
import 'package:nc_photos/widget/viewer.dart';
import 'package:nc_photos/widget/zoom_menu_button.dart';
class ArchiveViewerArguments {
ArchiveViewerArguments(this.account);
@ -168,15 +168,11 @@ class _ArchiveViewerState extends State<ArchiveViewer>
title: Text(L10n.of(context).albumArchiveLabel),
floating: true,
actions: [
PopupMenuButton(
icon: const Icon(Icons.photo_size_select_large),
tooltip: L10n.of(context).zoomTooltip,
itemBuilder: (context) => [
PopupMenuZoom(
initialValue: _thumbZoomLevel,
minValue: 0,
maxValue: 2,
onChanged: (value) {
ZoomMenuButton(
initialZoom: _thumbZoomLevel,
minZoom: 0,
maxZoom: 2,
onZoomChanged: (value) {
setState(() {
_thumbZoomLevel = value.round();
});
@ -184,8 +180,6 @@ class _ArchiveViewerState extends State<ArchiveViewer>
},
),
],
),
],
);
}

View file

@ -35,9 +35,9 @@ import 'package:nc_photos/widget/home_app_bar.dart';
import 'package:nc_photos/widget/measure.dart';
import 'package:nc_photos/widget/page_visibility_mixin.dart';
import 'package:nc_photos/widget/photo_list_item.dart';
import 'package:nc_photos/widget/popup_menu_zoom.dart';
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
import 'package:nc_photos/widget/viewer.dart';
import 'package:nc_photos/widget/zoom_menu_button.dart';
class HomePhotos extends StatefulWidget {
HomePhotos({
@ -209,15 +209,11 @@ class _HomePhotosState extends State<HomePhotos>
child: HomeSliverAppBar(
account: widget.account,
actions: [
PopupMenuButton(
icon: const Icon(Icons.photo_size_select_large),
tooltip: L10n.of(context).zoomTooltip,
itemBuilder: (context) => [
PopupMenuZoom(
initialValue: _thumbZoomLevel,
minValue: -1,
maxValue: 2,
onChanged: (value) {
ZoomMenuButton(
initialZoom: _thumbZoomLevel,
minZoom: -1,
maxZoom: 2,
onZoomChanged: (value) {
setState(() {
_setThumbZoomLevel(value.round());
});
@ -225,8 +221,6 @@ class _HomePhotosState extends State<HomePhotos>
},
),
],
),
],
menuActions: [
PopupMenuItem(
value: _menuValueRefresh,

View file

@ -1,52 +0,0 @@
import 'package:flutter/material.dart';
class PopupMenuZoom extends PopupMenuEntry<void> {
PopupMenuZoom({
Key? key,
required this.initialValue,
required this.minValue,
required this.maxValue,
this.onChanged,
}) : super(key: key);
@override
represents(void value) => false;
@override
createState() => _PopupMenuZoomState();
@override
// this value doesn't seems to do anything?
final double height = 48.0;
final int initialValue;
final double minValue;
final double maxValue;
final void Function(double)? onChanged;
}
class _PopupMenuZoomState extends State<PopupMenuZoom> {
@override
initState() {
super.initState();
_value = widget.initialValue.toDouble();
}
@override
build(BuildContext context) {
return Slider(
value: _value,
min: widget.minValue,
max: widget.maxValue,
divisions: (widget.maxValue - widget.minValue).round(),
onChanged: (value) {
setState(() {
_value = value;
});
widget.onChanged?.call(value);
},
);
}
var _value = 0.0;
}

View file

@ -0,0 +1,85 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:nc_photos/app_localizations.dart';
class ZoomMenuButton extends StatelessWidget {
ZoomMenuButton({
Key? key,
required this.initialZoom,
required this.minZoom,
required this.maxZoom,
this.onZoomChanged,
}) : super(key: key);
@override
build(BuildContext context) {
return PopupMenuButton(
icon: const Icon(Icons.photo_size_select_large),
tooltip: L10n.of(context).zoomTooltip,
itemBuilder: (context) => [
_PopupMenuZoom(
initialValue: initialZoom,
minValue: minZoom,
maxValue: maxZoom,
onChanged: onZoomChanged,
),
],
);
}
final int initialZoom;
final int minZoom;
final int maxZoom;
final ValueChanged<int>? onZoomChanged;
}
class _PopupMenuZoom extends PopupMenuEntry<void> {
_PopupMenuZoom({
Key? key,
required this.initialValue,
required this.minValue,
required this.maxValue,
this.onChanged,
}) : super(key: key);
@override
represents(void value) => false;
@override
createState() => _PopupMenuZoomState();
@override
// this value doesn't seems to do anything?
final double height = 48.0;
final int initialValue;
final int minValue;
final int maxValue;
final ValueChanged<int>? onChanged;
}
class _PopupMenuZoomState extends State<_PopupMenuZoom> {
@override
initState() {
super.initState();
_value = widget.initialValue;
}
@override
build(BuildContext context) {
return Slider(
value: _value.toDouble(),
min: widget.minValue.toDouble(),
max: widget.maxValue.toDouble(),
divisions: (widget.maxValue - widget.minValue).round(),
onChanged: (value) {
setState(() {
_value = value.round();
});
widget.onChanged?.call(_value);
},
);
}
late int _value;
}