import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:ongbut_ntmanh/app/module/new/model/new_model.dart';

import '../../../../const/colors.dart';
import '../../../../res/images/images.dart';
import '../../../../widgets/widgets_util.dart';
import '../bloc/new_bloc.dart';

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

  NewModel? newModel;
  @override
  Widget build(BuildContext context) {
    BlocProvider.of<NewBloc>(context).add(
        RequestNew(context: context));
    return
      BlocConsumer<NewBloc, NewState>(
          listener: (context, state) async {},
          builder: (context, state) {
            return
              Column(
              children: [
                Padding(
                  padding: const EdgeInsets.fromLTRB(10, 10, 0, 0),
                  child: Row(
                    children: [
                      const Text('Tin Tuc',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
                      const SizedBox(width: 100),
                      TextButton(
                          child: Text('Xem Them  >>'),
                          onPressed: () {
                            print('Pressed');
                          }
                      )
                    ],
                  ),
                ),
                BlocBuilder<NewBloc, NewState>(
                    builder: (context, state) {
                      if(state is NewInitial){
                        return const Text('NewInitial');
                      }
                      if(state is NewFailed){
                        //const CircularProgressIndicator();
                        return Text('NewInitial');
                      }
                      if(state is NewLoading){
                        return const Text('NewInitial');
                      }
                      if(state is NewSuccess){
                        return
                          Container(
                              decoration: const BoxDecoration(
                                color: AppColors.white,
                                borderRadius: BorderRadius.all(Radius.circular(10)),
                                boxShadow: [
                                  BoxShadow(
                                    color: AppColors.primary_light,
                                    spreadRadius: 1,
                                  ),
                                ],
                              ),
                            height: 250,
                              width: double.maxFinite,
                              child:
                            ListView.builder(
                              scrollDirection: Axis.horizontal,
                              itemCount: state.newModel!.data!.length,
                              itemBuilder: (context, index) {
                                return Container(

                                  decoration: BoxDecoration(
                                    color: AppColors.white,
                                    borderRadius: const BorderRadius.all(Radius.circular(10)),
                                    boxShadow: [
                                      BoxShadow(
                                        color: AppColors.lightGrey.withOpacity(1),
                                        spreadRadius: 1,
                                        offset: const Offset(0, 0), // changes position of shadow
                                      ),
                                    ],
                                  ),
                                    height: 250,
                                    width: 250,
                                    child:
                                      Column(
                                        //load_err
                                        children: [
                                          ClipRRect(
                                            borderRadius: BorderRadius.circular(8.0),
                                            child:
                                              Image.network(state.newModel!.data![index].urlImageRepresent!=null?state.newModel!.data![index].urlImageRepresent!:'https://oss.myitsol.com/imes-public/2022/12/11/neymar_145527553.PNG',
                                                fit: BoxFit.fill,height: 150,width: double.maxFinite,
                                                errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
                                                  return const Text('😢');
                                                },
                                              ),
                                          ),
                                          Padding(
                                            padding: const EdgeInsets.all(10.0),
                                            child:
                                            Text(state.newModel!.data![index].title!),
                                            //Text('abc'),
                                          ),
                                          Padding(
                                            padding: const EdgeInsets.all(10.0),
                                            child:
                                            Text(state.newModel!.data![index].title!),
                                            //Text('123'),
                                          ),
                                        ],
                                      ),
                                );
                              },
                            ),
                          );

                      }
                      return const Text('NewFailed');
                    }
                )
              ],
            );
          }
      );
  }
}
