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

View file

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

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/measure.dart';
import 'package:nc_photos/widget/page_visibility_mixin.dart'; import 'package:nc_photos/widget/page_visibility_mixin.dart';
import 'package:nc_photos/widget/photo_list_item.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/selectable_item_stream_list_mixin.dart';
import 'package:nc_photos/widget/viewer.dart'; import 'package:nc_photos/widget/viewer.dart';
import 'package:nc_photos/widget/zoom_menu_button.dart';
class HomePhotos extends StatefulWidget { class HomePhotos extends StatefulWidget {
HomePhotos({ HomePhotos({
@ -209,22 +209,16 @@ class _HomePhotosState extends State<HomePhotos>
child: HomeSliverAppBar( child: HomeSliverAppBar(
account: widget.account, account: widget.account,
actions: [ actions: [
PopupMenuButton( ZoomMenuButton(
icon: const Icon(Icons.photo_size_select_large), initialZoom: _thumbZoomLevel,
tooltip: L10n.of(context).zoomTooltip, minZoom: -1,
itemBuilder: (context) => [ maxZoom: 2,
PopupMenuZoom( onZoomChanged: (value) {
initialValue: _thumbZoomLevel, setState(() {
minValue: -1, _setThumbZoomLevel(value.round());
maxValue: 2, });
onChanged: (value) { Pref.inst().setHomePhotosZoomLevel(_thumbZoomLevel);
setState(() { },
_setThumbZoomLevel(value.round());
});
Pref.inst().setHomePhotosZoomLevel(_thumbZoomLevel);
},
),
],
), ),
], ],
menuActions: [ menuActions: [

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;
}