flutter 设置 TextField 的值

           String _newContent = "";
           GlobalKey _newContentKey = new GlobalKey();
                  ......

           Container(
                    padding: EdgeInsets.fromLTRB(0.00, 5.00, 0.00, 0.00),
                    child: new TextField(
                      key: _newContentKey,
                      controller: new TextEditingController(),
                      style: TextStyle(fontSize: 9.00, color: Colors.black),
                      autocorrect: false,
                      maxLines: 2,
                      decoration: new InputDecoration(
                          labelText: '内容',
                          border: new OutlineInputBorder(
                              borderRadius: new BorderRadius.circular(3.0))),
                      onChanged: (val) {
                        if (val.length > 100) {
                          _newContent = val.substring(0, 100);
                          (_newContentKey.currentWidget as TextField)
                              .controller
                              .text = _newContent;
                        } else {
                          _newContent = val;
                        }
                      },
                    )..controller.text = _newContent,
                  ),

  主要需要的是 controller

posted on 2018-12-21 00:54  --LP--  阅读(2897)  评论(0)    收藏  举报

导航