0

I want to add text field in flutter and when user add value quatity value should be updated you can see in that below item image there is quantity I want to add a text field against it so user can edit quantity as much as he wanted to add you can ask anything you want to ask enter image description here

here is the quantity selection

class QuantitySelection extends StatelessWidget {
  final int limitSelectQuantity;
  final int value;
  final double width;
  final double height;
  final Function onChanged;
  final Color color;

  QuantitySelection(
      {@required this.value,
      this.width = 40.0,
      this.height = 42.0,
      this.limitSelectQuantity = 100,
      @required this.color,
      this.onChanged});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (onChanged != null) {
          showOptions(context);
        }
      },
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(width: 1.0, color: kGrey200),
          borderRadius: BorderRadius.circular(3),
        ),
        height: height,
        width: width,
        child: Padding(
          padding: EdgeInsets.symmetric(
              vertical: 2.0, horizontal: (onChanged != null) ? 5.0 : 10.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Expanded(
                child: Center(
                  child: Text(
                    value.toString(),
                    style: TextStyle(fontSize: 14, color: color),
                  ),
                ),
              ),
              if (onChanged != null)
                const SizedBox(
                  width: 5.0,
                ),
              if (onChanged != null)
                Icon(Icons.keyboard_arrow_down,
                    size: 14, color: Theme.of(context).accentColor)
            ],
          ),
        ),
      ),
    );
  }

  void showOptions(context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      for (int option = 1;
                          option <= limitSelectQuantity;
                          option++)
                        ListTile(
                            onTap: () {
                              onChanged(option);
                              Navigator.pop(context);
                            },
                            title: Text(
                              option.toString(),
                              textAlign: TextAlign.center,
                            )),
                    ],
                  ),
                ),
              ),
              Container(
                height: 1,
                decoration: BoxDecoration(color: kGrey200),
              ),
              ListTile(
                title: Text(
                  S.of(context).selectTheQuantity,
                  textAlign: TextAlign.center,
                ),
              ),
            ],
          );
        });
  }
}

here is shopping cart

    class ShoppingCartRow extends StatelessWidget {
  ShoppingCartRow(
      {@required this.product,
      @required this.quantity,
      this.onRemove,
      this.onChangeQuantity,
      this.variation});

  final Product product;
  final ProductVariation variation;
  final int quantity;
  final Function onChangeQuantity;
  final VoidCallback onRemove;

  @override
  Widget build(BuildContext context) {
    String currency = Provider.of<AppModel>(context).currency;
    final currencyRate = Provider.of<AppModel>(context).currencyRate;

    final price = Services()
        .widget
        .getPriceItemInCart(product, variation, currencyRate, currency);
    final imageFeature = variation != null && variation.imageFeature != null
        ? variation.imageFeature
        : product.imageFeature;
    int maxQuantity = kCartDetail['maxAllowQuantity'] ?? 100;
    int totalQuantity = variation != null
        ? (variation.stockQuantity ?? maxQuantity)
        : (product.stockQuantity ?? maxQuantity);
    int limitQuantity =
        totalQuantity > maxQuantity ? maxQuantity : totalQuantity;

    ThemeData theme = Theme.of(context);

    return LayoutBuilder(
      builder: (context, constraints) {
        return Column(children: [
          Row(
            key: ValueKey(product.id),
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              if (onRemove != null)
                IconButton(
                  icon: Icon(Icons.remove_circle_outline),
                  onPressed: onRemove,
                ),
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.only(right: 16.0),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Stack(children: <Widget>[
                        Container(
                            width: constraints.maxWidth * 0.25,
                            height: constraints.maxWidth * 0.3,
                            child: Tools.image(url: imageFeature)),
                        Positioned(
                          bottom: 0,
                          right: 0,
                          child: Container(
                            decoration: BoxDecoration(
                              border: Border.all(width: 1.0, color: kGrey200),
                              color: Theme.of(context).backgroundColor,
                              borderRadius: BorderRadius.only(
                                  topLeft: Radius.circular(2.0)),
                            ),
                            child: QuantitySelection(
                              width: 60,
                              height: 32,
                              color: Theme.of(context).accentColor,
                              limitSelectQuantity: limitQuantity,
                              value: quantity,
                              onChanged: onChangeQuantity,
                            ),
                          ),
                        )
                      ]),
                      SizedBox(width: 16.0),
                      Expanded(
                        child: Container(

                      ),
                      ),
                      Expanded(
                        child: Container(
                          height: constraints.maxWidth * 0.3,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                product.name,
                                style: TextStyle(
                                  color: theme.accentColor,
                                ),
                                maxLines: 4,
                                overflow: TextOverflow.ellipsis,
                              ),
                              SizedBox(height: 7),
                              Text(
                                price,
                                style: TextStyle(
                                    color: theme.accentColor, fontSize: 14),
                              ),
                              SizedBox(height: 10),
                              variation != null
                                  ? Services()
                                      .widget
                                      .renderVariantCartItem(variation)
                                  : Container(),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 10.0),
          Divider(color: kGrey200, height: 1),
          SizedBox(height: 10.0),
        ]);
      },
    );
  }
}
  • 1
    Please be more specific, Your question is unclear, Which textfield you want to update and when. – Mukul Dec 28 '20 at 11:49
  • you can see in that below item image there is quantity I want to add a text field against it so user can edit quantity as much as he wanted to add –  Dec 28 '20 at 11:59
  • You can use the `TextField` widget to create a text field (see https://api.flutter.dev/flutter/material/TextField-class.html). Can you make your question more specific? – spkersten Dec 28 '20 at 13:15
  • This might be the answer you are asking: [Flutter dropdown text field](https://stackoverflow.com/questions/49780858/flutter-dropdown-text-field/56170945) – Jim Dec 29 '20 at 02:19
  • @spkersten I know about the text field but I want to add text field and button to the right side of dropdown how to achieve it –  Dec 29 '20 at 07:53

1 Answers1

0

I think having an editable number field with plus and minus button would be a nice and user-friendly solution. There's a number of ways to do this with flutter - check out the solutions in this thread for ideas.

rgisi
  • 858
  • 6
  • 21