我有一个TextField
,需要输入可以编辑的起始文本,然后根据需要传递回或保存到设备中。
我使用TextEditingController()
作为起始文字,并且正在努力从StatefulWidget
的州内访问此值。
下面是我的代码:
class TextFieldScreen extends StatefulWidget {
final initialText;
const TextFieldScreen({Key key, this.initialText}) : super(key: key);
@override
_TextFieldScreenState createState() => _TextFieldScreenState();
}
class _TextFieldScreenState extends State<TextFieldScreen> {
final _controller = TextEditingController(text: widget.initialText);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
padding: EdgeInsets.all(24.0),
child: TextField(
controller: _controller,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.deepPurple),
borderRadius: BorderRadius.all(Radius.circular(20.0))
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
borderRadius: BorderRadius.all(Radius.circular(20.0))
),
),
),
),
],
);
}
在上面的代码中,final _controller = TextEditingController(text: widget.initialText);
行出现了only static members can be accessed in initializers
错误。
因此,在进行了一些搜索之后,我了解了为什么会发生这种情况,并且需要在initState()
中进行初始化,因此我添加了以下代码:
@override
void initState() {
super.initState();
final _controller = TextEditingController(text: widget.initialText);
}
现在以前的错误消失了,但是现在我的TextField()
无法访问conroller:
,而我却遇到了undefined
错误。
这是解决这个问题的正确方法吗?
我该如何工作?
谢谢
答案 0 :(得分:1)
您可以在下面复制粘贴运行完整代码
您可以在_controller
之外声明initState
代码段
TextEditingController _controller;
@override
void initState() {
super.initState();
_controller = TextEditingController(text: widget.initialText);
}
工作演示
import 'package:flutter/material.dart';
class TextFieldScreen extends StatefulWidget {
final initialText;
const TextFieldScreen({Key key, this.initialText}) : super(key: key);
@override
_TextFieldScreenState createState() => _TextFieldScreenState();
}
class _TextFieldScreenState extends State<TextFieldScreen> {
TextEditingController _controller;
@override
void initState() {
super.initState();
_controller = TextEditingController(text: widget.initialText);
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
padding: EdgeInsets.all(24.0),
child: TextField(
controller: _controller,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.deepPurple),
borderRadius: BorderRadius.all(Radius.circular(20.0))),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
borderRadius: BorderRadius.all(Radius.circular(20.0))),
),
),
),
],
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFieldScreen(initialText: "abc",),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}