homepage.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_cocktail/model/config.dart';
  4. import 'package:flutter_cocktail/pages/drink_detail.dart';
  5. import 'package:http/http.dart' as http;
  6. /// Description: home page
  7. /// Time : 11/06/2023 Monday
  8. /// Author : liuyuqi.gov@msn.cn
  9. class HomePage extends StatefulWidget {
  10. const HomePage({super.key});
  11. @override
  12. _HomePageState createState() => _HomePageState();
  13. }
  14. class _HomePageState extends State<HomePage> {
  15. String api =
  16. "https://www.thecocktaildb.com/api/json/v1/1/filter.php?c=Cocktail";
  17. var res, drinks;
  18. @override
  19. void initState() {
  20. super.initState();
  21. fetchData();
  22. }
  23. fetchData() async {
  24. res = await http.get(Uri.parse(api));
  25. drinks = jsonDecode(res.body)["drinks"];
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return Container(
  30. decoration: const BoxDecoration(
  31. gradient: LinearGradient(colors: [
  32. myColor,
  33. Colors.orange,
  34. ])),
  35. child: Scaffold(
  36. backgroundColor: Colors.transparent,
  37. appBar: AppBar(
  38. title: const Text("Cocktail App"),
  39. elevation: 0.0,
  40. backgroundColor: Colors.transparent,
  41. ),
  42. body: Center(
  43. child: res != null
  44. ? ListView.builder(
  45. itemCount: drinks.length,
  46. itemBuilder: (context, index) {
  47. var drink = drinks[index];
  48. return ListTile(
  49. leading: Hero(
  50. tag: drink["idDrink"],
  51. child: CircleAvatar(
  52. backgroundImage: NetworkImage(
  53. drink["strDrinkThumb"] ??
  54. "http://www.4motiondarlington.org/wp-content/uploads/2013/06/No-image-found.jpg",
  55. ),
  56. ),
  57. ),
  58. title: Text(
  59. "${drink["strDrink"]}",
  60. style: const TextStyle(
  61. fontSize: 22,
  62. color: Colors.white,
  63. ),
  64. ),
  65. subtitle: Text(
  66. "${drink["idDrink"]}",
  67. style: const TextStyle(
  68. color: Colors.white,
  69. ),
  70. ),
  71. onTap: () {
  72. Navigator.push(
  73. context,
  74. MaterialPageRoute(
  75. builder: (context) => DrinkDetail(drink: drink),
  76. ),
  77. );
  78. },
  79. );
  80. },
  81. )
  82. : const CircularProgressIndicator(backgroundColor: Colors.white),
  83. ),
  84. ),
  85. );
  86. }
  87. }