123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:flutter_cocktail/model/config.dart';
- import 'package:flutter_cocktail/pages/drink_detail.dart';
- import 'package:http/http.dart' as http;
- /// Description: home page
- /// Time : 11/06/2023 Monday
- /// Author : liuyuqi.gov@msn.cn
- class HomePage extends StatefulWidget {
- const HomePage({super.key});
- @override
- _HomePageState createState() => _HomePageState();
- }
- class _HomePageState extends State<HomePage> {
- String api =
- "https://www.thecocktaildb.com/api/json/v1/1/filter.php?c=Cocktail";
- var res, drinks;
- @override
- void initState() {
- super.initState();
- fetchData();
- }
- fetchData() async {
- res = await http.get(Uri.parse(api));
- drinks = jsonDecode(res.body)["drinks"];
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- decoration: const BoxDecoration(
- gradient: LinearGradient(colors: [
- myColor,
- Colors.orange,
- ])),
- child: Scaffold(
- backgroundColor: Colors.transparent,
- appBar: AppBar(
- title: const Text("Cocktail App"),
- elevation: 0.0,
- backgroundColor: Colors.transparent,
- ),
- body: Center(
- child: res != null
- ? ListView.builder(
- itemCount: drinks.length,
- itemBuilder: (context, index) {
- var drink = drinks[index];
- return ListTile(
- leading: Hero(
- tag: drink["idDrink"],
- child: CircleAvatar(
- backgroundImage: NetworkImage(
- drink["strDrinkThumb"] ??
- "http://www.4motiondarlington.org/wp-content/uploads/2013/06/No-image-found.jpg",
- ),
- ),
- ),
- title: Text(
- "${drink["strDrink"]}",
- style: const TextStyle(
- fontSize: 22,
- color: Colors.white,
- ),
- ),
- subtitle: Text(
- "${drink["idDrink"]}",
- style: const TextStyle(
- color: Colors.white,
- ),
- ),
- onTap: () {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => DrinkDetail(drink: drink),
- ),
- );
- },
- );
- },
- )
- : const CircularProgressIndicator(backgroundColor: Colors.white),
- ),
- ),
- );
- }
- }
|