Gallery/lib/home_page.dart

127 lines
4.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:gallery/data_provider/data_provider.dart';
import 'package:gallery/image_tile.dart';
import 'data_provider/local_data_provider.dart';
import 'full_screen_image_view.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<Folder>? folder;
final DataProvider dprovider = LocalDataProvider("/home");
final ScrollController _controller = ScrollController();
_MyHomePageState() {
folder = dprovider.listOfFiles();
}
@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder(
future: folder,
builder: (context, snapshot) {
if (snapshot.hasData) {
final data = snapshot.data!;
return GridView.builder(
controller: _controller,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: 0,
mainAxisSpacing: 0,
crossAxisCount: width ~/ 300,
),
itemCount: data.items.length + 1,
itemBuilder: (context, index) {
if (index == 0) {
return ImageTile(
child: const Icon(Icons.arrow_back, size: 142),
onClick: () {
setState(() {
folder = dprovider.listOfFiles(uri: data.parent);
});
},
);
}
final elem = data.items[index - 1];
return ImageTile(
dtaProvider: dprovider,
imageUri: elem.isFolder ? null : elem.uri,
onClick: () {
if (elem.isFolder) {
setState(() {
folder = 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: dprovider,
items: d,
idx: newidx,
);
}),
);
}
},
child: elem.isFolder
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.folder_open, size: 142),
Text(elem.name)
],
)
: null);
},
);
} else if (snapshot.hasError) {
return const Text("Error loading files");
}
return const CircularProgressIndicator(
strokeWidth: 3,
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_controller.animateTo(.0,
duration: const Duration(milliseconds: 400),
curve: Curves.easeInOutQuad);
},
tooltip: 'Increment',
child: const Icon(Icons.arrow_upward),
),
);
}
}