mirror of
https://github.com/wgh136/pixes.git
synced 2025-09-27 04:57:23 +00:00
load illust with id; context menu
This commit is contained in:
@@ -2,6 +2,8 @@ import 'package:fluent_ui/fluent_ui.dart';
|
||||
import 'package:pixes/components/animated_image.dart';
|
||||
import 'package:pixes/foundation/app.dart';
|
||||
import 'package:pixes/foundation/image_provider.dart';
|
||||
import 'package:pixes/network/download.dart';
|
||||
import 'package:pixes/utils/translation.dart';
|
||||
|
||||
import '../network/network.dart';
|
||||
import '../pages/illust_page.dart';
|
||||
@@ -19,74 +21,118 @@ class IllustWidget extends StatefulWidget {
|
||||
class _IllustWidgetState extends State<IllustWidget> {
|
||||
bool isBookmarking = false;
|
||||
|
||||
final contextController = FlyoutController();
|
||||
final contextAttachKey = GlobalKey();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return LayoutBuilder(builder: (context, constrains) {
|
||||
final width = constrains.maxWidth;
|
||||
final height = widget.illust.height * width / widget.illust.width;
|
||||
return SizedBox(
|
||||
width: width,
|
||||
height: height,
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned.fill(child: Container(
|
||||
width: width,
|
||||
height: height,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 8.0),
|
||||
child: Card(
|
||||
padding: EdgeInsets.zero,
|
||||
margin: EdgeInsets.zero,
|
||||
child: GestureDetector(
|
||||
onTap: (){
|
||||
context.to(() => IllustPage(widget.illust, favoriteCallback: (v) {
|
||||
setState(() {
|
||||
widget.illust.isBookmarked = v;
|
||||
});
|
||||
},));
|
||||
},
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(4.0),
|
||||
child: AnimatedImage(
|
||||
image: CachedImageProvider(widget.illust.images.first.medium),
|
||||
fit: BoxFit.cover,
|
||||
width: width-16.0,
|
||||
height: height-16.0,
|
||||
return FlyoutTarget(
|
||||
controller: contextController,
|
||||
child: SizedBox(
|
||||
key: contextAttachKey,
|
||||
width: width,
|
||||
height: height,
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned.fill(child: Container(
|
||||
width: width,
|
||||
height: height,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 8.0),
|
||||
child: Card(
|
||||
padding: EdgeInsets.zero,
|
||||
margin: EdgeInsets.zero,
|
||||
child: GestureDetector(
|
||||
onTap: (){
|
||||
context.to(() => IllustPage(widget.illust, favoriteCallback: (v) {
|
||||
setState(() {
|
||||
widget.illust.isBookmarked = v;
|
||||
});
|
||||
},));
|
||||
},
|
||||
onSecondaryTapUp: showMenu,
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(4.0),
|
||||
child: AnimatedImage(
|
||||
image: CachedImageProvider(widget.illust.images.first.medium),
|
||||
fit: BoxFit.cover,
|
||||
width: width-16.0,
|
||||
height: height-16.0,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)),
|
||||
Positioned(
|
||||
top: 16,
|
||||
right: 16,
|
||||
child: buildButton(),
|
||||
)
|
||||
],
|
||||
)),
|
||||
Positioned(
|
||||
top: 16,
|
||||
right: 16,
|
||||
child: buildButton(),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Widget buildButton() {
|
||||
void favorite() async{
|
||||
if(isBookmarking) return;
|
||||
setState(() {
|
||||
isBookmarking = true;
|
||||
});
|
||||
var method = widget.illust.isBookmarked ? "delete" : "add";
|
||||
var res = await Network().addBookmark(widget.illust.id.toString(), method);
|
||||
if(res.error) {
|
||||
if(mounted) {
|
||||
context.showToast(message: "Network Error");
|
||||
}
|
||||
} else {
|
||||
widget.illust.isBookmarked = !widget.illust.isBookmarked;
|
||||
}
|
||||
setState(() {
|
||||
isBookmarking = false;
|
||||
});
|
||||
}
|
||||
void showMenu(TapUpDetails details) {
|
||||
// This calculates the position of the flyout according to the parent navigator
|
||||
final targetContext = contextAttachKey.currentContext;
|
||||
if (targetContext == null) return;
|
||||
final box = targetContext.findRenderObject() as RenderBox;
|
||||
final position = box.localToGlobal(
|
||||
details.localPosition,
|
||||
ancestor: Navigator.of(context).context.findRenderObject(),
|
||||
);
|
||||
|
||||
contextController.showFlyout(
|
||||
barrierColor: Colors.transparent,
|
||||
position: position,
|
||||
builder: (context) {
|
||||
return MenuFlyout(
|
||||
items: [
|
||||
MenuFlyoutItem(text: Text("View".tl), onPressed: (){
|
||||
context.to(() => IllustPage(widget.illust, favoriteCallback: (v) {
|
||||
setState(() {
|
||||
widget.illust.isBookmarked = v;
|
||||
});
|
||||
},));
|
||||
}),
|
||||
MenuFlyoutItem(text: Text("Private Favorite".tl), onPressed: (){
|
||||
favorite("private");
|
||||
}),
|
||||
MenuFlyoutItem(text: Text("Download".tl), onPressed: (){
|
||||
context.showToast(message: "Added");
|
||||
DownloadManager().addDownloadingTask(widget.illust);
|
||||
}),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void favorite([String type = "public"]) async{
|
||||
if(isBookmarking) return;
|
||||
setState(() {
|
||||
isBookmarking = true;
|
||||
});
|
||||
var method = widget.illust.isBookmarked ? "delete" : "add";
|
||||
var res = await Network().addBookmark(widget.illust.id.toString(), method, type);
|
||||
if(res.error) {
|
||||
if(mounted) {
|
||||
context.showToast(message: "Network Error");
|
||||
}
|
||||
} else {
|
||||
widget.illust.isBookmarked = !widget.illust.isBookmarked;
|
||||
}
|
||||
setState(() {
|
||||
isBookmarking = false;
|
||||
});
|
||||
}
|
||||
|
||||
Widget buildButton() {
|
||||
Widget child;
|
||||
if(isBookmarking) {
|
||||
child = const SizedBox(
|
||||
|
Reference in New Issue
Block a user