dart - 如何在 dart 中自动增长文本区域元素?

标签 dart

我想要一个 textarea 元素,可以根据 textarea 元素的内容动态调整其高度。

我如何在 Dart 中实现这个?

最佳答案

仅使用纯 dart:html:

var textArea = querySelector('textarea');
textArea.onInput.listen((_) {
  // shrink the textarea when needed
  textArea.style.height = 'auto';

  // set the height to scrollHeight plus some correction
  var correction = textArea.offsetHeight - textArea.clientHeight;
  textArea.style.height = '${textArea.scrollHeight - correction}px';
});

您还可以创建一个 angular2 指令:

@Directive(
    selector: 'textarea[autogrow]',
    host: const {
      '(input)': 'onInput(\$event.target)'
    }
)
class AutogrowDirective {

  onInput(TextAreaElement textArea) {
    // shrink the textarea when needed
    textArea.style.height = 'auto';

    // set the height to scrollHeight plus some correction
    var correction = textArea.offsetHeight - textArea.clientHeight;
    textArea.style.height = '${textArea.scrollHeight - correction}px';
  }
}

关于dart - 如何在 dart 中自动增长文本区域元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33269718/

相关文章:

flutter - Flutter中如何去除TextFormField中的错误信息

flutter - 一个构建函数返回null,有问题的小部件是:flutter_bloc包中的BlocBuilder <NotificationBloc,NotificationState>

dart - Polymer.dart 数据绑定(bind) : updating model values without updating view

dart - Flutter StatefulWidget - 状态类继承?

Flutter - 如何在多个地方重用一些代码

flutter - 如何在Flutter UI中创建第二行和第三行?

firebase - 异常 “type ' String'不是 'int'的 'index'类型的子类型”

dart - Flutter - 遍历对象键和值

android - Flutter:如何在 Flutter 中使图像的某些区域透明?

flutter - 使Zara像产品页面一样 flutter 朔迷离