import 'package:flutter/cupertino.dart'; import 'package:provider/provider.dart'; import '../types/abstracts.dart'; import '../types/credential.dart'; import '../widgets/tappable_text_list.dart'; class Home extends StatefulWidget { @override State createState() => _HomeState(); } class _HomeState extends State { CredentialsRepo _client; Future> _metadatas; @override didChangeDependencies() { super.didChangeDependencies(); _client = Provider.of(context); } @override Widget build(BuildContext context) { if (_metadatas == null) { _metadatas = _client.getAllMetadata('').toList(); } return CupertinoPageScaffold( child: FutureBuilder>( future: _metadatas, builder: (BuildContext context, AsyncSnapshot> snapshot) => (snapshot.connectionState == ConnectionState.done) ? TappableTextList( tappableText: _buildTappableText(context, snapshot.data)) : Container(), ), navigationBar: CupertinoNavigationBar(), ); } Map _buildTappableText( BuildContext context, List metadatas, ) { final Map> metaMap = {}; metadatas.sort((a, b) => a.id.compareTo(b.id)); for (var metadata in metadatas) { final source = metadata.sourceHost; if (metaMap[source] == null) { metaMap[source] = [metadata]; } else { metaMap[source].add(metadata); } } final handleOnTap = (List metadatas) => () => Navigator.of(context).pushNamed('/credentials', arguments: metadatas); final Map tappableText = {}; metaMap.forEach((String key, List value) => tappableText[key] = handleOnTap(value)); return tappableText; } }