【问题标题】:How can I add a container widget by pressing the button?如何通过按下按钮添加容器小部件?
【发布时间】:2021-10-29 02:49:42
【问题描述】:

如下图所示,有一个带有图像和文本字段的容器小部件,我想在单击按钮时创建一个额外的容器。
我不知道如何实现这一点。
你能建议一种方法来实现它吗?

      Container _productForm() {
    return Container(
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              _productImage(),
              _productImage(),
            ],
          ),
          _heightPadding(15),
          _inputText("input text", _controller2),
          _heightPadding(15),
          _inputText("input text", _controller3),
        ],
      ),
    );
  }
  
     Widget _inputText(String hint, TextEditingController textEditingController) {
        return Container(
          width: double.infinity,
          // height: 200,
          child: TextField(
            controller: textEditingController,
            //엔터키 이벤트
            onSubmitted: (value) {},
            //높이를 부모 위젯의 높이로 설정 (컨테이너 전체를 텍스트필드로 사용)
            // expands: true,
            maxLines: null,
            decoration: InputDecoration(
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(5),
              ),
              hintText: hint,
            ),
          ),
        );
      }

   Widget _addButton() {
return Container(
              width: 200,
              height: 65,
              child: ElevatedButton(
                onPressed: () {
                  //상품썸네일 아래에 상품썸네일 하나 더 추가
                },
                child: Text(
                  'Add Container',
                  style: TextStyle(
                      fontSize: 17, fontWeight: FontWeight.bold),
                ),
                style: ElevatedButton.styleFrom(
                  primary: Colors.blue, // background
                  onPrimary: Colors.white, // foregro// und
                ),
              ),
            );
  }

【问题讨论】:

  • 您要创建的附加容器是什么?您是否必须添加另一个带有其 texfield 的 pdf 图标或究竟是什么?

标签: android ios flutter


【解决方案1】:

尝试加入列表:

List<Container> _containers = [_productForm(),_productForm(),];

                onPressed: () {
                  //상품썸네일 아래에 상품썸네일 하나 더 추가
                  _containers.add(_productForm());
                  setState((){});
                },

然后使用 ListView 加载您的 _containers 列表

【讨论】:

    【解决方案2】:

    创建小部件列表

    List<Container> _containers = [_productForm(),_productForm(),];
    

    在行中设置动态子项

    Row(
      children: _containers
          .map((_) => _productForm())
          .toList(),
    )
    

    按下按钮

    onPressed: () {
        _containers.add(_productForm());
        setState((){});            
    },
    

    【讨论】:

      猜你喜欢
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多