2022-09-27 12:43:34 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-29 19:01:28 +00:00
|
|
|
import 'package:gallery/utils/tile_size.dart';
|
2022-09-27 12:43:34 +00:00
|
|
|
import 'data_provider/data_provider.dart';
|
|
|
|
|
|
|
|
import 'full_screen_image_view.dart';
|
|
|
|
import 'image_tile.dart';
|
|
|
|
|
|
|
|
class ImageGrid extends StatefulWidget {
|
|
|
|
const ImageGrid({Key? key, required this.dProvider}) : super(key: key);
|
|
|
|
|
|
|
|
final DataProvider dProvider;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ImageGrid> createState() => _ImageGridState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ImageGridState extends State<ImageGrid> {
|
|
|
|
final ScrollController _controller = ScrollController();
|
|
|
|
late Future<Folder> folder = widget.dProvider.listOfFiles();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didUpdateWidget(ImageGrid oldWidget) {
|
|
|
|
super.didUpdateWidget(oldWidget);
|
|
|
|
if (oldWidget.dProvider != widget.dProvider) {
|
|
|
|
folder = widget.dProvider.listOfFiles();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final width = MediaQuery.of(context).size.width;
|
|
|
|
|
|
|
|
return FutureBuilder(
|
2022-09-27 15:17:15 +00:00
|
|
|
future: folder,
|
2022-09-27 12:43:34 +00:00
|
|
|
builder: (context, AsyncSnapshot<Folder> snapshot) {
|
|
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
|
|
if (snapshot.hasData && snapshot.data != null) {
|
|
|
|
final Folder data = snapshot.data!;
|
|
|
|
|
|
|
|
return GridView.builder(
|
|
|
|
controller: _controller,
|
|
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
|
|
crossAxisSpacing: 0,
|
|
|
|
mainAxisSpacing: 0,
|
2022-09-29 19:01:28 +00:00
|
|
|
crossAxisCount: width ~/ calcTileMaxWidth(),
|
2022-09-27 12:43:34 +00:00
|
|
|
),
|
|
|
|
itemCount: data.items.length + 1,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
if (index == 0) {
|
|
|
|
return ImageTile(
|
2022-09-29 19:01:28 +00:00
|
|
|
child: Icon(Icons.arrow_back, size: calcTileMaxWidth() / 2),
|
2022-09-27 12:43:34 +00:00
|
|
|
onClick: () {
|
|
|
|
setState(() {
|
|
|
|
folder = widget.dProvider.listOfFiles(uri: data.parent);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
final elem = data.items[index - 1];
|
|
|
|
return ImageTile(
|
|
|
|
dtaProvider: widget.dProvider,
|
|
|
|
imageUri: elem.isFolder ? null : elem.uri,
|
|
|
|
onClick: () {
|
|
|
|
if (elem.isFolder) {
|
|
|
|
setState(() {
|
|
|
|
folder = widget.dProvider.listOfFiles(uri: elem.uri);
|
|
|
|
});
|
|
|
|
_controller.jumpTo(.0);
|
|
|
|
} else {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(builder: (context) {
|
|
|
|
final d = data.items
|
|
|
|
.where((element) => !element.isFolder)
|
|
|
|
.toList(growable: false);
|
|
|
|
// check how many folders are before index and subtract it
|
|
|
|
final newidx = index -
|
|
|
|
data.items
|
|
|
|
.getRange(0, index)
|
|
|
|
.where((element) => element.isFolder)
|
|
|
|
.length;
|
|
|
|
|
|
|
|
return FullScreenImageView(
|
|
|
|
provider: widget.dProvider,
|
|
|
|
items: d,
|
|
|
|
idx: newidx,
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: elem.isFolder
|
|
|
|
? Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
2022-09-28 13:31:24 +00:00
|
|
|
Icon(Icons.folder_open,
|
2022-09-29 19:01:28 +00:00
|
|
|
size: calcTileMaxWidth() / 2),
|
2022-09-27 12:43:34 +00:00
|
|
|
Text(elem.name)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
: null);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else if (snapshot.hasError) {
|
2022-09-29 18:32:52 +00:00
|
|
|
return Text("Error loading files: ${snapshot.error}");
|
2022-09-27 12:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 10:38:53 +00:00
|
|
|
return const Center(child: CircularProgressIndicator());
|
2022-09-27 12:43:34 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|