import 'dart:io'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'level.dart'; class LevelSelection extends StatefulWidget { const LevelSelection({super.key}); @override State createState() => _LevelSelectionState(); } class _LevelSelectionState extends State { String? stepmaniaCoursesPath; List stepmaniaCoursesFolders = []; @override void initState() { super.initState(); loadFolderPath(); } Future loadFolderPath() async { SharedPreferences prefs = await SharedPreferences.getInstance(); final String? stepmaniaCoursesPathSetting = prefs.getString('stepmania_courses'); if (stepmaniaCoursesPathSetting == null) return; setState(() { stepmaniaCoursesPath = stepmaniaCoursesPathSetting; }); setState(() async { stepmaniaCoursesFolders = await listFilesAndFolders(stepmaniaCoursesPathSetting); }); } Future selectFolder() async { String? selectedFolder = await FilePicker.platform.getDirectoryPath(); if (selectedFolder != null) { // Save the selected folder path SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString('stepmania_courses', selectedFolder); loadFolderPath(); } } Future> listFilesAndFolders( String directoryPath) async { final directory = Directory(directoryPath); try { // List all files and folders in the directory return directory .listSync() .where((entity) => FileSystemEntity.isDirectorySync(entity.path)) .toList(); } catch (e) { print("Error reading directory: $e"); return []; } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Sense the Rhythm')), body: Builder(builder: (context) { if (stepmaniaCoursesPath == null) { return Text('Add a Directory with Stepmania Songs on \'+\''); } else if (stepmaniaCoursesFolders.isEmpty) { return Text( 'Folder empty. Add Stepmania Songs to Folder or select a different folder on \'+\''); } else { return ListView.separated( itemCount: stepmaniaCoursesFolders.length, separatorBuilder: (BuildContext context, int index) => const Divider(), itemBuilder: (context, index) { String thumbnailPath = Directory(stepmaniaCoursesFolders[index].path) .listSync() .firstWhere((file) => file.path.toLowerCase().endsWith('banner.png'), orElse: () => File('')) .path; return ListTile( leading: Image.file(File(thumbnailPath)), trailing: Icon(Icons.play_arrow), title: Text(stepmaniaCoursesFolders[index].path.split('/').last), subtitle: Text('3:45'), onTap: () => Navigator.push( context, MaterialPageRoute( builder: (BuildContext context) => Level())), ); }, ); } }), floatingActionButton: FloatingActionButton( onPressed: () => {selectFolder()}, child: Icon(Icons.add)), ); } }