// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/global/global_state/global_state.dart';
import 'package:qadirneyriz/utils/enums/status.dart';
import 'package:qadirneyriz/widgets/custom_netimage.dart';

class CustomAppbar extends StatefulWidget {
  final String? title;

  const CustomAppbar({
    Key? key,
    this.title,
  }) : super(key: key);

  @override
  State<CustomAppbar> createState() => _CustomAppbarState();
}

class _CustomAppbarState extends State<CustomAppbar> {
  @override
  Widget build(BuildContext context) {
    return Consumer<GlobalState>(
      builder: (context, globalState, child) {
        final String image = globalState.logoImagesStatus == Status.ready
            ? globalState.logoImagesModel!.data!
                    .firstWhere(
                      (element) => element.key == 'logo',
                    )
                    .value ??
                ''
            : '';
        final String ImageUrl = '${config.network.imageUrl}${image}';
        return SliverAppBar(
          title: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              const SizedBox(),
              Text(
                this.widget.title == null ? '' : this.widget.title ?? '',
                style: const TextStyle(
                  fontSize: 12,
                  color: Colors.black,
                ),
              ),
              globalState.logoImagesStatus == Status.ready
                  ? CustomImage(
                      logo: true,
                      image: ImageUrl,
                      width: 50,
                      height: 50,
                    )
                  : Container()
            ],
          ),
          backgroundColor: config.ui.backGroundColor,
        );
      },
    );
  }
}
