import 'dart:convert'; import 'dart:io'; import 'package:tangocard_reader/models/yokutango_entry.dart'; import 'package:tangocard_reader/screens/error.dart'; import 'package:tangocard_reader/screens/loading.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; class Home extends StatelessWidget { const Home({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("よく単語"), centerTitle: true, ), body: const TangocardList()); } } class TangocardList extends StatelessWidget { const TangocardList({Key? key}) : super(key: key); Future>> get tangocardFilePaths => rootBundle .loadString('AssetManifest.json') .then( (json) => jsonDecode(json) .keys .where((String key) => key.contains('yokutango/json/') && key.contains('.json')) .toList(), ) .then( (l) async => Map.fromIterables( l.map((f) => File(f)), await Future.wait>( l .map>>( (String t) => rootBundle .loadString(t) .then>((s) => jsonDecode(s) .map( (e) => YokutangoEntry.fromJson(e)) .toList()), ) ), ), ); @override Widget build(BuildContext context) { return FutureBuilder( future: tangocardFilePaths, builder: (context, snapshot) { if (snapshot.hasError) { debugPrint(snapshot.error.toString()); return const ErrorScreen(); } else if (!snapshot.hasData) { return const LoadingScreen(); } return ListView( children: (snapshot.data as Map>) .entries .map( (e) => ListTile( title: Text( "${e.key.uri.pathSegments.last} - ${e.value.length} cards"), onTap: () => Navigator.pushNamed(context, '/tangolist', arguments: e.key), ), ) .toList(), ); }, ); } }