12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import 'package:cs_resources/theme/app_colors_theme.dart';
- import 'package:flutter/material.dart';
- import 'package:widgets/my_text_view.dart';
- class MyCartNum extends StatefulWidget {
- final int initialNum;
- final ValueChanged<int> onChange; // 添加回调函数
- MyCartNum({Key? key, this.initialNum = 1, 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: Colors.white),
- 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: Colors.white),
- onPressed: _incrementNum,
- child: const Icon(Icons.add),
- ),
- ],
- );
- }
- }
|