import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../utils/xapp_utils.dart';

/// 页面头部组件
class IAppBar extends StatelessWidget implements PreferredSizeWidget {
  final double _height = 44.rpx;
  final Color color;
  final String? title;
  final Widget? child;
  final double elevation;
  final List<Widget>? actions;
  final bool leading;
  IAppBar(
      {this.child,
      this.title,
      this.actions,
      this.leading = true,
      this.color = Colors.red,
      this.elevation = 0.1});

  @override
  Widget build(BuildContext context) {
    double luminance = color.computeLuminance();
    Color titleColor = luminance < 0.5 ? Colors.white : Colors.black;
    Brightness style = luminance < 0.5 ? Brightness.dark : Brightness.light;
    Widget? leadingWidget;
    if (leading == true) {
      leadingWidget = IconButton(
        color: titleColor,
        icon: Icon(
          Icons.arrow_back_ios,
          size: 18.rpx,
        ),
        onPressed: () {
          Navigator.maybePop(context);
        },
      );
    }
    return AppBar(
      backgroundColor: color,
      elevation: elevation,
      leading: leadingWidget,
      toolbarHeight: _height,
      actions: actions,
      systemOverlayStyle: SystemUiOverlayStyle(statusBarBrightness:style),
      automaticallyImplyLeading: false,
      flexibleSpace: SafeArea(
        bottom: false,
        top: true,
        child: Container(
          alignment: Alignment.center,
          width: double.infinity,
          height: _height,
          child: DefaultTextStyle(
            style: TextStyle(
                color: titleColor,
                fontSize: 17.rpx,
                fontWeight: FontWeight.bold),
            child: child ??
                Text(
                  title!,
                ),
          ),
        ),
      ),
    );
  }

  Size get preferredSize => Size.fromHeight(_height);
}