【问题标题】:Update basket items using textformfield (user input) in the Flutter在 Flutter 中使用 textformfield(用户输入)更新购物篮项目
【发布时间】:2022-06-30 23:39:24
【问题描述】:

我正在尝试增加和减少购物车内的数量值。我尝试了两次按钮点击,效果很好。现在我想通过使用 textformfield 的用户输入来做到这一点。除了按钮(增量和减量),如果用户输入值,比如说 10,数量变为 10,价格也根据 10 个项目。谁能帮助我实现这一目标?或者任何建议也会有所帮助。

       //some increment code....

            childWidget: Container(
                alignment: Alignment.topCenter,
                width: 25.0,
                height: 25.0,
                child: Center(
                  child: TextFormField(
                    initialValue: quantity.toString(),
                    
                   onChanged: (value){},
                  
                    style: const TextStyle(
                        fontSize: 10, fontWeight: FontWeight.bold),
                  ),
                ),
              ),
             // some decrement code.....

【问题讨论】:

    标签: flutter flutter-textformfield


    【解决方案1】:

    您可以为此使用onChanged 函数。该值是您的TextFormField 中当前内容的String

    onChanged: (value) {
      final amount = int.tryParse(value); // this will return null if the current value is not a valid integer
      if(amount != null) {
        // update the amount, probably just like you did with increment/decrement
      }
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      相关资源
      最近更新 更多