Pressure on the button and change of view



  • Tell me what the mistake is. введите сюда описание изображения

    class AddButton extends StatefulWidget {
      AddButton({required this.product, required this.count, Key? key})
          : super(key: key);
    
      final Product product;
      int count;
    
      @override
      _AddButton createState() => _AddButton(product: product,count: count);
    
    }
    
    
    class _AddButton extends State<AddButton>{
      _AddButton({required this.product, required this.count});
    
      final Product product;
      int count;
      bool showWdiget = false;
    
      void _incrementCounterPlus(){
        setState((){
          count++;
          if(count<=0) {
            showWdiget = true;
          }
        });
      }
      void _incrementCounterMinus(){
        setState((){
          count--;
          if(count == 0){
            showWdiget = false;
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: showWdiget ? DefaultBtn(product: product) : CounterBtn(product: this.product,count: this.count),
        );
      }
    }
    
    
    
    class DefaultBtn extends _AddButton{
      DefaultBtn({required this.product, Key? key})
          : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Row(
          children: [
            CupertinoButton(
              padding: EdgeInsets.zero,
              onPressed: () {
                final model = Provider.of<AppStateModel>(context, listen: false);
                model.addProductToCart(product.id);
                _incrementCounterPlus();
                // _AddButton(product: product,count:-1);
              },
              child: const Text(
                'Add',
              ),
            ),
          ],
        );
      }
    }
    
    
    class CounterBtn extends _AddButton {
      CounterBtn({required this.product, required this.count, Key? key})
          : super(key: key);
    
      final Product product;
      int count;
      @override
      Widget build(BuildContext context) {
        return Row(
          children: [
            CupertinoButton(
              padding: EdgeInsets.zero,
              onPressed: () {
                final model = Provider.of<AppStateModel>(context, listen: false);
                model.removeItemFromCart(product.id);
                _incrementCounterMinus();
                // _AddButton(product: product,count:-1);
              },
              child: const Icon(
                CupertinoIcons.minus_square,
                semanticLabel: 'Minus',
              ),
            ),
            Text(
              this.count.toString(),
            ),
            CupertinoButton(
              padding: EdgeInsets.zero,
              onPressed: () {
                final model = Provider.of<AppStateModel>(context, listen: false);
                model.addProductToCart(product.id);
                _incrementCounterPlus();
              },
              child: const Icon(
                CupertinoIcons.plus_circled,
                semanticLabel: 'Add',
              ),
            ),
          ],
        );
      }
    }
    



  • The problem is:

    class DefaultBtn extends _AddButton{
    class CounterBtn extends _AddButton {
    

    You inherit from _AddButton, in turn he inherits from State<AddButton>but State It's not. Widget


    If you need a common interface with logic, you can do it somehow:

    mixin ProductMixin<T extends StatefulWidget> on State<T> {
      late final Product _product;
      Product get product => _product;
    

    late int _count;
    int get count => _count;

    bool _showWdiget = false;
    bool get _showWdiget => showWdiget ;

    void init(Product p, int c) {
    _product = p;
    _count = c;
    }

    void incrementCounterPlus(){
    setState((){
    _count++;
    if(count<=0) {
    _showWdiget = true;
    }
    });
    }
    void incrementCounterMinus(){
    setState((){
    _count--;
    if(count == 0){
    _showWdiget = false;
    }
    });
    }
    }

    class DefaultBtn extends StatefulWidget {
    DefaultBtn({required this.product, required this.count, Key? key})
    : super(key: key);

    final Product product;
    int count;

    @override
    State createState() => _DefaultBtnState();
    }

    class _DefaultBtnState extends State<DefaultBtn> with ProductMixin {
    @override
    initState() {
    init(widget.product, widget.count);
    super.initState();
    }

    @override
    Widget build(BuildContext context) {
    return Row(
    children: [
    CupertinoButton(
    padding: EdgeInsets.zero,
    onPressed: () {
    final model = Provider.of<AppStateModel>(context, listen: false);
    model.addProductToCart(widget.product.id);
    incrementCounterPlus();
    },
    child: const Text(
    'Add',
    ),
    ),
    ],
    );
    }
    }

    class CounterBtn extends StatefulWidget {
    CounterBtn({required this.product, required this.count, Key? key})
    : super(key: key);

    final Product product;
    int count;

    @override
    State createState() => _CounterBtnState();
    }

    class _CounterBtnState extends State<CounterBtn> with ProductMixin {
    @override
    initState() {
    init(widget.product, widget.count);
    super.initState();
    }

    @override
    Widget build(BuildContext context) {
    return Row(
    children: [
    CupertinoButton(
    padding: EdgeInsets.zero,
    onPressed: () {
    final model = Provider.of<AppStateModel>(context, listen: false);
    model.removeItemFromCart(widget.product.id);
    incrementCounterMinus();
    },
    child: const Icon(
    CupertinoIcons.minus_square,
    semanticLabel: 'Minus',
    ),
    ),
    Text(
    count.toString(),
    ),
    CupertinoButton(
    padding: EdgeInsets.zero,
    onPressed: () {
    final model = Provider.of<AppStateModel>(context, listen: false);
    model.addProductToCart(widget.product.id);
    incrementCounterPlus();
    },
    child: const Icon(
    CupertinoIcons.plus_circled,
    semanticLabel: 'Add',
    ),
    ),
    ],
    );
    }
    }


    State You don't have to make a designer for the transmission of parameters, because they can be contacted through the ghetter. widget♪ An example is also in the code.

    P.S. wrote everything without an editor, it's possible to make mistakes.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2