我需要知道用户在文本字段中输入了多少行。有没有这样做的语法?这是我的代码。出现“只能在初始化程序中访问静态成员”
class MyCustomForm extends StatefulWidget {
@override
Page1 createState() => Page1();
}
class Page1 extends State<MyCustomForm> {
final TextEditingController myController = TextEditingController();
String change = '';
final numLines = '\n'.allMatches(change).length + 1; //appears Only static members can be accessed in initializer
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new ListView(
children: <Widget>[
new Container(
padding: new EdgeInsets.all(10.0),
child: new Column(
children: <Widget>[
new TextField(
keyboardType: TextInputType.multiline,
maxLines: 26,
controller: myController,
onChanged: (String e){
setState(() {
change = e;
});
},
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(20.0))),
)
],
),
),
],
),
);
}
}
答案 0 :(得分:1)
您可以在这里找到答案 flutter-how-to-get-the-number-of-text-lines
下次,请确保在创建重复项之前对以前的问题进行了一些搜索。谢谢
编辑
似乎您是新手。您不能在那里使用该功能
class MyCustomForm extends StatefulWidget {
@override
Page1 createState() => Page1();
}
class Page1 extends State<MyCustomForm> {
final TextEditingController myController = TextEditingController();
int numLines = 0;
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new ListView(
children: <Widget>[
new Container(
padding: new EdgeInsets.all(10.0),
child: new Column(
children: <Widget>[
new TextField(
keyboardType: TextInputType.multiline,
maxLines: 26,
controller: myController,
onChanged: (String e){
setState((){
numLines = '\n'.allMatches(e).length + 1;
})
},
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(20.0))),
)
],
),
),
],
),
);
}
}