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
              Container(
                color: AppColors.background,
                child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.fromLTRB(20, 0, 0, 0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('Tin Tuc',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
                        TextButton(
                            child: const Text('Xem Them>>'),
                            onPressed: () {
                              // Navigator.push(
                              //     context,
                              //     MaterialPageRoute(
                              //       builder: (_) => SearchWidget(),
                              //
                              //     )).then((value) {
                              //   if (value != null && value is bool && value) {}
                              // });
                            }
                        )
                      ],
                    ),
                  ),
                  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(
                              padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
                                decoration: const BoxDecoration(

                                  borderRadius: BorderRadius.all(Radius.circular(10)),
                                  // boxShadow: [
                                  //   BoxShadow(
                                  //     //color: AppColors.primary_light,
                                  //     spreadRadius: 1,
                                  //   ),
                                  // ],
                                ),
                                height: checkLandscape(context)?getHeight(context)*0.5:getHeight(context)*0.35,
                                width: double.maxFinite,
                                child:
                              ListView.builder(
                                scrollDirection: Axis.horizontal,
                                itemCount: state.newModel.data!.length,
                                itemBuilder: (context, index) {
                                  return
                                    Container(
                                    padding: const EdgeInsets.all(10),
                                    decoration: const BoxDecoration(
                                      color: AppColors.background,
                                    ),
                                      height: checkLandscape(context)?getHeight(context)*0.5:getHeight(context)*0.3,
                                      width: checkLandscape(context)?getWidth(context)*0.3:getWidth(context)*0.6,
                                      child:
                                        Container(
                                          decoration: const BoxDecoration(
                                            color: AppColors.white,
                                            borderRadius: BorderRadius.all(Radius.circular(10)),
                                            boxShadow: [
                                              BoxShadow(
                                                color: AppColors.primary,
                                                spreadRadius: 0.5,
                                              ),
                                            ],
                                          ),
                                          child: Column(
                                            //load_err
                                            children: [
                                              ClipRRect(
                                                borderRadius: BorderRadius.circular(8.0),
                                                child:
                                                  Image.network(
                                                    state.newModel.data![index].urlImageRepresent!,
                                                    fit: BoxFit.fill,height:checkLandscape(context)?getHeight(context)*0.3:getHeight(context)*0.2,width: double.maxFinite,
                                                    errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
                                                      return Image.asset(Images.load_err,fit: BoxFit.fill,height: checkLandscape(context)?getHeight(context)*0.3:getHeight(context)*0.2,width: double.maxFinite,);
                                                    },
                                                  ),
                                              ),
                                              Padding(
                                                padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
                                                child: Text(state.newModel.data![index].title!),
                                              ),
                                              Padding(
                                                padding: const EdgeInsets.fromLTRB(0, 5, 0, 0),
                                                child: Text(state.newModel.data![index].title!),
                                              ),
                                            ],
                                          ),
                                        ),
                                  );
                                },
                              ),
                            );

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