Untitled

 avatar
user_9393521917
dart
2 years ago
4.1 kB
2
Indexable
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:televendas/constants.dart';

class CountProductWidget extends StatefulWidget {
  final Function increment;
  final Function decrement;
  final int count;
  final int value;
  final int width;
  final double iconSize;
  final double fontSize;
  final Function(String v) onChange;
  // final Function(TapDownDetails) onTapDown;
  // final Function(TapUpDetails) onTapUp;
  final bool canEditManual;

  const CountProductWidget({
    Key key,
    @required this.increment,
    @required this.decrement,
    @required this.count,
    @required this.value,
    this.width,
    this.iconSize = 24.0,
    this.fontSize,
    this.canEditManual = false,
    this.onChange,
  }) : super(key: key);

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

class _CountProductWidgetState extends State<CountProductWidget> {
  TextEditingController textEditingController = TextEditingController();

  int maxValue;

  @override
  void initState() {
    maxValue = widget.value;
    textEditingController.text = widget.count.toString();
    print(textEditingController.text);

    super.initState();
  }

  change() {}

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width * .35,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          widget.count != 0
              ? GestureDetector(
                  onTap: () {
                    int val = widget.count - 1;
                    textEditingController.text = val.toString();
                    widget.decrement();
                  },
                  child: new Icon(
                    Icons.remove,
                    color: Colors.red,
                  ),
                )
              : new Icon(
                  Icons.remove,
                  color: Colors.grey,
                ),
          // new Text(
          //   widget.count.toString(),
          //   style: TextStyle(fontSize: widget.fontSize),
          // ),
          Expanded(
            child: TextField(
              textAlign: TextAlign.center,
              keyboardType: TextInputType.number,
              controller: textEditingController,
              onChanged: widget.onChange,
              inputFormatters: [
                FilteringTextInputFormatter.digitsOnly,
                CustomRangeTextInputFormatter(widget.value),
              ],
              decoration: new InputDecoration(
                border: InputBorder.none,
                focusedBorder: InputBorder.none,
                enabledBorder: InputBorder.none,
                errorBorder: InputBorder.none,
                disabledBorder: InputBorder.none,
              ),
            ),
          ),
          widget.count < maxValue
              ? GestureDetector(
                  onTap: () {
                    int val = widget.count + 1;
                    textEditingController.text = val.toString();
                    widget.increment();
                  },
                  child: new Icon(
                    Icons.add,
                    color: kBlueColor,
                  ),
                )
              : new Icon(
                  Icons.add,
                  color: Colors.grey,
                ),
        ],
      ),
    );
  }
}

class CustomRangeTextInputFormatter extends TextInputFormatter {
  final int maxValue;

  CustomRangeTextInputFormatter(this.maxValue);

  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    if (newValue.text == '')
      return TextEditingValue();
    else if (int.parse(newValue.text) < 1)
      return TextEditingValue().copyWith(text: '1');

    return int.parse(newValue.text) > maxValue
        ? TextEditingValue().copyWith(text: maxValue.toString())
        : newValue;
  }
}
Editor is loading...