mirrored_repositories/Flutter-Mobile-Application-Development/widgits_practice/lib
// ignore_for_file: prefer_const_constructors, unnecessary_null_comparison, avoid_unnecessary_containers, prefer_const_literals_to_create_immutables

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:velocity_x/velocity_x.dart';
import 'package:widgits_practice/models/catalog.dart';
import 'package:widgits_practice/widgits/drawer.dart';
import 'package:widgits_practice/widgits/item_widgit.dart';
import 'package:widgits_practice/widgits/themes.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    loadData();
  }

  void loadData() async {
    final catalogJson =
        await rootBundle.loadString("assets/files/catalog.json");
    final decodeData = jsonDecode(catalogJson);
    var productData = decodeData["products"];
    // print(productData);
    CatalogModels.items = List.from(productData)
        .map<ItemModel>((item) => ItemModel.fromMap(item))
        .toList();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          padding: Vx.m32,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CatalogHeader(),
              if(CatalogModels.items != null && CatalogModels.items.isNotEmpty)
                 CatalogList()
              else
                 Center(
                   child: CircularProgressIndicator(),
                 )
            ],
          ),
        ),
      ),
    );
  }
}

class CatalogHeader extends StatelessWidget {
  const CatalogHeader({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        "Catalog App".text.xl5.bold.color(AppTheme.darkBluishColor).make(),
        "Treanding Products".text.xl2.make(),
      ],
    );
  }
}


class CatalogList extends StatelessWidget {
  const CatalogList({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: CatalogModels.items.length,
      itemBuilder: (context, index) {
        final catalog = CatalogModels.items[index];
        return Catalogitem(catalog);
      },
    );
  }
}