Image attachments

This commit is contained in:
LittleSheep 2024-03-24 00:13:52 +08:00
parent c19bb3730e
commit 048b8c0894
4 changed files with 85 additions and 1 deletions

View File

@ -1,19 +1,26 @@
import 'package:flutter/material.dart';
import 'package:flutter_carousel_widget/flutter_carousel_widget.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:solaragent/models/feed.dart';
import 'package:solaragent/widgets/image.dart';
class FeedItem extends StatelessWidget {
final Feed item;
const FeedItem({super.key, required this.item});
bool hasAttachments() =>
item.attachments != null && item.attachments!.isNotEmpty;
String getDescription(String desc) =>
desc.isEmpty ? "No description yet." : desc;
String getFileUrl(String fileId) =>
'https://co.solsynth.dev/api/attachments/o/$fileId';
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: Column(
children: [
Container(
@ -36,6 +43,45 @@ class FeedItem extends StatelessWidget {
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
),
hasAttachments()
? Container(
decoration: const BoxDecoration(
border: Border(
top: BorderSide(width: 0.3, color: Color(0xffdedede))),
),
child: FlutterCarousel(
options: CarouselOptions(
height: 240.0,
showIndicator: true,
slideIndicator: const CircularSlideIndicator(),
),
items: item.attachments?.map((x) {
return Builder(
builder: (BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
margin: const EdgeInsets.symmetric(horizontal: 5.0),
child: InkWell(
child: Image.network(
getFileUrl(x.fileId),
fit: BoxFit.cover,
),
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (_) {
return ImageLightbox(
url: getFileUrl(x.fileId),
);
}));
},
),
);
},
);
}).toList(),
),
)
: Container(),
],
),
);

29
lib/widgets/image.dart Normal file
View File

@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
class ImageLightbox extends StatelessWidget {
final String url;
const ImageLightbox({super.key, required this.url});
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
child: Center(
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: InteractiveViewer(
boundaryMargin: const EdgeInsets.all(128),
minScale: 0.1,
maxScale: 16.0,
child: Image.network(url, fit: BoxFit.contain),
),
),
),
onTap: () {
Navigator.pop(context);
},
),
);
}
}

View File

@ -126,6 +126,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_carousel_widget:
dependency: "direct main"
description:
name: flutter_carousel_widget
sha256: "37b9e55e4cafffe358152b016db24153e756152aa07c4214cfe6ee902cd69a01"
url: "https://pub.dev"
source: hosted
version: "2.2.0"
flutter_launcher_icons:
dependency: "direct dev"
description:

View File

@ -45,6 +45,7 @@ dependencies:
shared_preferences: ^2.2.2
flutter_markdown: ^0.6.22
infinite_scroll_pagination: ^4.0.0
flutter_carousel_widget: ^2.2.0
dev_dependencies:
flutter_test: