import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:flutter/material.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/utils/dark_theme_util.dart';

class MyCartNum extends StatefulWidget {
  final int initialNum;
  final ValueChanged<int> onChange; // 添加回调函数

  MyCartNum({Key? key, this.initialNum = 0, required this.onChange}) : super(key: key);

  @override
  _MyCartNumState createState() => _MyCartNumState();
}

class _MyCartNumState extends State<MyCartNum> {
  late int _num;

  @override
  void initState() {
    super.initState();
    _num = widget.initialNum;
  }

  void _incrementNum() {
    setState(() {
      _num++;
      widget.onChange(_num); // 调用回调函数
    });
  }

  void _decrementNum() {
    setState(() {
      if (_num >= 1) {
        _num--;
        widget.onChange(_num); // 调用回调函数
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        RawMaterialButton(
          constraints: const BoxConstraints.tightFor(
            width: 27.5,
            height: 27.5,
          ),
          elevation: 1,
          shape:  CircleBorder(
            side: BorderSide(width: 1.0, color: context.appColors.textPrimary),
          ),
            fillColor: context.appColors.textPrimary,
            splashColor: context.appColors.textPrimary,
            textStyle: TextStyle(color: DarkThemeUtil.multiColors(context, Colors.white, darkColor: Colors.black54)),
            onPressed: _decrementNum,
            child: const Icon(Icons.remove),
        ),
        MyTextView(
          '$_num',
          fontSize: 15,
          isFontRegular: true,
          textAlign: TextAlign.center,
          boxWidth: 30,
        ),
        RawMaterialButton(
          constraints: const BoxConstraints.tightFor(
            width: 27.5,
            height: 27.5,
          ),
          elevation: 1,
          shape:  CircleBorder(
            side: BorderSide(width: 1.0, color: context.appColors.textPrimary),
          ),
          fillColor: context.appColors.textPrimary,
          splashColor: context.appColors.textPrimary,
          textStyle: TextStyle(color: DarkThemeUtil.multiColors(context, Colors.white, darkColor: Colors.black54)),
          onPressed: _incrementNum,
          child: const Icon(Icons.add),
        ),
      ],
    );
  }
}
