enter image description here

如何通过拖拽和点击按钮来增加/减少TextField中的maxLines个数?

推荐答案

Screenshot:

enter image description here


Full code:

class YourPage extends StatefulWidget {
  @override
  _YourPageState createState() => _YourPageState();
}

class _YourPageState extends State<YourPage> {
  double _maxHeight = 200, _minHeight = 44, _height = 44, _dividerHeight = 56, _offset = 19;
  int _maxLines = 1;
  static final Duration _fixDuration = Duration(milliseconds: 500);
  Duration _duration = _fixDuration;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Padding(
        padding: const EdgeInsets.all(20),
        child: SizedBox(
          height: _maxHeight,
          child: Column(
            children: <Widget>[
              AnimatedContainer(
                duration: _duration,
                height: _height,
                child: TextField(
                  decoration: InputDecoration(hintText: "Enter a message"),
                  maxLines: _maxLines,
                ),
              ),
              Container(
                height: _dividerHeight,
                width: 200,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    IconButton(
                      icon: Icon(Icons.arrow_downward),
                      onPressed: () {
                        if (_height <= _maxHeight - _offset - _dividerHeight) {
                          setState(() {
                            _duration = _fixDuration;
                            _height += _offset;
                            _maxLines++;
                          });
                        }
                      },
                    ),
                    GestureDetector(
                      child: Icon(Icons.drag_handle),
                      onPanUpdate: (details) {
                        setState(() {
                          _height += details.delta.dy;
                          _duration = Duration.zero;

                          // prevent overflow if height is more/less than available space
                          var maxLimit = _maxHeight - _dividerHeight;
                          var minLimit = 44.0;

                          if (_height > maxLimit)
                            _height = maxLimit;
                          else if (_height < minLimit) _height = minLimit;

                          _maxLines = 100;
                        });
                      },
                    ),
                    IconButton(
                      icon: Icon(Icons.arrow_upward),
                      onPressed: () {
                        if (_height >= _minHeight + _offset) {
                          setState(() {
                            _duration = _fixDuration;
                            _height -= _offset;
                          });
                        }
                      },
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Dart相关问答推荐

我们应该将多少代码放入构造函数中?

Dart 3.3接口字段类型升级不起作用

将数据库提供程序导入另一个文件提供程序 dart

构造函数:将预处理的参数存储在传递给最终字段的辅助变量中

什么时候在 Dart 中使用interfaces接口?

GPS是否激活

Flutter:是否可以有多个 Futurebuilder 或一个 Futurebuilder 用于多个 Future 方法?

VSCode Flutter Dart 启动慢的建议

如何为 spawnUri 动态构建 Dart 脚本?

Flutter:合并两张图片,作为单张图片存储在本地存储中

NoSuchMethodError:在 null 上调用了方法validate

如何在Dart中创建空Map

使用Flutter/dart的NTLM身份验证

如何在dart中获取 map 的键列表?

在 Dart 中编写单元测试的最佳方式是什么?

谷歌的 Dart 编程语言的作用是什么?

列表的反向迭代器?

我应该更喜欢迭代 Map.entries 还是 Map.values?

用一个 catch 表达式在 Dart 中捕获多种特定的异常类型

如何在 Dart 中定义接口?