import 'package:flutter/Material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 顶部栏
class TopBar extends StatelessWidget {
  /// 左边的组件，例如返回按钮
  final Widget? leftWidget;

  /// 中间的组件，例如标题
  final Widget? centerWidget;

  /// 右边的组件
  final Widget? rightWidget;

  const TopBar({
    super.key,
    this.leftWidget,
    this.centerWidget,
    this.rightWidget,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      // 宽度撑满父组件
      width: double.infinity,
      height: kToolbarHeight,
      // 背景颜色
      color: Colors.transparent,
      child: ConstrainedBox(
        // 设置宽度为无限大，撑满父组件，否则Stack获取不到高度，会报错
        constraints: BoxConstraints(minWidth: double.infinity),
        child: Stack(
          alignment: Alignment.center,
          children: [
            // 左边按钮
            Positioned(
              left: 16.w,
              child: leftWidget != null ? leftWidget! : SizedBox.shrink(),
            ),
            // 中间的标题
            Positioned(
              left: 0,
              right: 0,
              child: centerWidget != null ? centerWidget! : SizedBox.shrink(),
            ),
            // 右边的按钮
            Positioned(
              right: 16.w,
              child: rightWidget != null ? rightWidget! : SizedBox.shrink(),
            ),
          ],
        ),
      ),
    );
  }
}
