ente/lib/ui/gallery.dart

87 lines
2.4 KiB
Dart
Raw Normal View History

2020-03-28 18:18:27 +00:00
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
2020-03-30 14:28:46 +00:00
import 'package:myapp/models/photo.dart';
2020-03-28 18:18:27 +00:00
import 'package:myapp/photo_loader.dart';
import 'package:myapp/ui/image_widget.dart';
import 'package:provider/provider.dart';
2020-03-30 10:57:04 +00:00
import 'package:toast/toast.dart';
2020-03-28 18:18:27 +00:00
import 'change_notifier_builder.dart';
import 'detail_page.dart';
class Gallery extends StatefulWidget {
@override
_GalleryState createState() {
return _GalleryState();
}
}
class _GalleryState extends State<Gallery> {
Logger _logger = Logger();
2020-03-30 10:57:04 +00:00
int _crossAxisCount = 4;
2020-03-28 18:18:27 +00:00
PhotoLoader get photoLoader => Provider.of<PhotoLoader>(context);
2020-03-30 10:57:04 +00:00
2020-03-28 18:18:27 +00:00
@override
Widget build(BuildContext context) {
2020-03-30 10:57:04 +00:00
_logger.i("Build with _crossAxisCount: " + _crossAxisCount.toString());
return GestureDetector(
onScaleUpdate: (ScaleUpdateDetails details) {
_logger.i("Scale update: " + details.horizontalScale.toString());
setState(() {
if (details.horizontalScale < 1) {
_crossAxisCount = 8;
} else if (details.horizontalScale < 2) {
_crossAxisCount = 5;
} else if (details.horizontalScale < 4) {
_crossAxisCount = 4;
} else if (details.horizontalScale < 8) {
_crossAxisCount = 2;
} else {
_crossAxisCount = 1;
}
});
2020-03-28 18:18:27 +00:00
},
2020-03-30 10:57:04 +00:00
child: ChangeNotifierBuilder(
value: photoLoader,
builder: (_, __) {
return GridView.builder(
itemBuilder: _buildItem,
itemCount: photoLoader.getPhotos().length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: _crossAxisCount,
));
},
),
2020-03-28 18:18:27 +00:00
);
}
Widget _buildItem(BuildContext context, int index) {
2020-03-30 10:57:04 +00:00
var photo = photoLoader.getPhotos()[index];
2020-03-28 18:18:27 +00:00
return GestureDetector(
2020-04-05 14:00:44 +00:00
onTap: () {
2020-03-30 14:28:46 +00:00
routeToDetailPage(photo, context);
2020-03-30 10:57:04 +00:00
},
onLongPress: () {
2020-03-30 14:28:46 +00:00
Toast.show(photo.localPath, context);
2020-03-28 18:18:27 +00:00
},
2020-03-30 14:28:46 +00:00
child: Padding(
2020-04-11 22:11:33 +00:00
padding: const EdgeInsets.all(2.0),
2020-04-11 21:29:07 +00:00
child: ImageWidget(photo),
2020-03-28 18:18:27 +00:00
),
);
}
2020-04-05 14:00:44 +00:00
void routeToDetailPage(Photo photo, BuildContext context) {
2020-04-05 14:45:04 +00:00
final page = DetailPage(photo);
2020-03-28 18:18:27 +00:00
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return page;
},
),
);
}
}