import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sport/widgets/image.dart';

const titleStyle = TextStyle(fontWeight: FontWeight.w600, fontSize: 18.0, color: Color(0xff333333));

Widget buildSliverAppBar(BuildContext context, String title,
    {List<Widget> actions,
    backgroundColor = Colors.white,
    paddingLeading = true,
    useClose = false,
    canBack = true,
    innerBoxIsScrolled = false,
    height = 110.0,
    brightness = -1,
    textStyle = titleStyle,
    whiteBackButton = false,
    pinned = true,
    EdgeInsets padding = const EdgeInsets.fromLTRB(12.0, 0, 0, 15.0)}) {
  return SliverAppBar(
      pinned: pinned,
      brightness: brightness == -1 ? null : brightness == 1 ? Brightness.dark : Brightness.light,
      expandedHeight: height,
      backgroundColor: backgroundColor,
      forceElevated: innerBoxIsScrolled,
      titleSpacing: 0,
      elevation: 0,
      leading: useClose
          ? IconButton(
              icon: Image.asset("lib/assets/img/topbar_cancel.png"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            )
          : canBack
              ? IconButton(
                  icon: arrowBack(),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                )
              : whiteBackButton
                  ? IconButton(
                      icon: Image.asset("lib/assets/img/topbar_return_white.png"),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    )
                  : Container(),
      actions: actions,
      flexibleSpace: _buildFlexibleSpace(title, paddingLeading: paddingLeading, textStyle: textStyle, padding: padding));
}

Widget _buildFlexibleSpace(String title, {paddingLeading = true, textStyle = titleStyle, EdgeInsets padding}) {
  return LayoutBuilder(builder: (context, box) {
    final FlexibleSpaceBarSettings settings = context.dependOnInheritedWidgetOfExactType<FlexibleSpaceBarSettings>();
    return FlexibleSpaceBar(
      centerTitle: false,
      titlePadding: padding,
      title: paddingLeading
          ? Padding(
              padding: EdgeInsets.only(left: (settings.maxExtent - box.biggest.height) / 3 * 1.6, bottom: .5),
              child: Text(
                title,
                style: textStyle,
              ),
            )
          : Text(
              title,
              style: textStyle,
            ),
    );
  });
}

Widget buildActionButton(String title, VoidCallback onPressed, {Color textColor}) {
  return IconButton(
    icon: Text(
      title,
      style: textColor != null ? TextStyle(fontSize: 16.0, color: textColor) : TextStyle(fontSize: 16),
    ),
    onPressed: onPressed,
  );
}

Widget buildBackButton(BuildContext context) {
  return IconButton(
    icon: arrowBack(),
    onPressed: () {
      Navigator.maybePop(context);
    },
  );
}

Widget buildAppBar(BuildContext context, {String title, List<Widget> actions,Brightness brightness}) {
  return AppBar(
    titleSpacing: -16.0,
    centerTitle: false,
    title: title == null
        ? null
        : Text(
            "$title",
            style: titleStyle,
          ),
    leading: buildBackButton(context),
    actions: actions,
    brightness: brightness,
    // backgroundColor: Colors.black,
  );
}
