artists.dart 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:http/http.dart' as http;
  4. import '../utils/url.dart';
  5. import './spinner.dart';
  6. Future<List<String>> fetchArtists(String apiUrl) async {
  7. final response = await http.get(formattedUrl(apiUrl, '/artists'));
  8. if (response.statusCode == 200) {
  9. return List<String>.from(jsonDecode(response.body)['artists']);
  10. } else {
  11. throw Exception('Failed to load artists');
  12. }
  13. }
  14. class Artist extends StatelessWidget {
  15. final String artist;
  16. final void Function(String) onSelect;
  17. Artist({
  18. @required this.artist,
  19. @required this.onSelect,
  20. });
  21. @override
  22. Widget build(BuildContext context) {
  23. return InkWell(
  24. onTap: () {
  25. onSelect(artist);
  26. },
  27. child: SizedBox(
  28. height: 40,
  29. width: MediaQuery.of(context).size.width,
  30. child: Container(
  31. child: Align(
  32. alignment: Alignment.centerLeft,
  33. child: Text(
  34. artist.length == 0 ? 'Unknown artist' : artist,
  35. style: TextStyle(
  36. fontSize: 18,
  37. ),
  38. ),
  39. ),
  40. ),
  41. ),
  42. );
  43. }
  44. }