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}) {
  return SliverAppBar(
      pinned: true,
      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();
                  },
                )
              : Container(),
      actions: actions,
      flexibleSpace: buildFlexibleSpace(title, paddingLeading: paddingLeading));
}

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

Widget buildActionButton(String title, VoidCallback onPressed) {
  return IconButton(
    icon: Text(
      title,
      style: TextStyle(fontSize: 16),
    ),
    onPressed: onPressed,
  );
}

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