【问题标题】:Flutter/Dart: Pass Parameters to a Stateful Widget?Flutter/Dart:将参数传递给有状态的小部件?
【发布时间】:2020-12-01 05:00:57
【问题描述】:

我需要调用将我的title 和oldtitle 参数传递给我的EditPage 有状态小部件。但如果我这样做;

 class EditPage extends StatefulWidget {
   String title;  
   String oldtitle;
   EditPage({this.title, this.oldtitle})

除非我将它们称为widget.title 和widget.oldtitle,否则这些字符串对构建不可用。

但我在表单中使用textfield,如果我使用这些小部件似乎无法正常工作。

这是表单代码:

      Container(                   
                child: TextField(
                    decoration: new InputDecoration(
                    hintText: widget.oldtitle,
                    contentPadding: new EdgeInsets.all(1.0),
                    border: InputBorder.none,
                    filled: true,
                    fillColor: Colors.grey[300],
                  ),
                  keyboardType: TextInputType.text,
                  autocorrect: false,
                  onChanged: (titleText) {
                    setState(() {
                       widget.title= titleText;
                    });
                  },
                ),
              ),

但是如果我这样做;

class _EditPageState extends State<EditPage> {
   String title;  
   String oldtitle;  
   EditPage({this.title, this.oldtitle})

我无法从另一个屏幕将标题参数传递给它。即:

`EditPage(title:mytitle, oldtitle:myoldtitle);`

那么将参数传递给有状态小部件的正确方法是什么?

【问题讨论】:

  • thistitle 从未声明过。您的意思可能是this.title。静态分析应该为此显示错误。
  • 谢谢,这个错误是我为 Stackoverflow 编辑的。完整的代码相当丰富。我已经对其进行了编辑,现在可以阅读 this.title。
  • widget. 是从状态访问小部件参数的正确方法。请澄清此方法的实际问题。

标签: flutter dart widget parameter-passing stateful


【解决方案1】:

你不应该直接将变量传递给状态,因为它不能保证当状态更新时小部件会被重建。您应该通过有状态小部件接受参数,并通过 widget.variable 从状态本身访问它们。

例子:

class TestWidget extends StatefulWidget {
  final String variable;

  TestWidget({Key key, @required this.variable}) : super(key: key);

  @override
  _TestWidgetState createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        // Accessing the variables passed into the StatefulWidget.
        child: Text(widget.variable),
      ),
    );
  }
}

【讨论】:

  • 如果是这种情况,我猜我的问题出在表单而不是参数传递上。我尝试了您的代码,但我的表单仍然无法正常工作。我将编辑以包含表单的代码;
【解决方案2】:

看起来解决方案是将标题与旧标题分开;

  class EditPage extends StatefulWidget {
     String oldtitle;
     EditPage({this.oldtitle})

然后;

 class _EditPageState extends State<EditPage> {
  String title;     

现在是表格;

Container(                   
            child: TextField(
                decoration: new InputDecoration(
                hintText: widget.oldtitle,
                contentPadding: new EdgeInsets.all(1.0),
                border: InputBorder.none,
                filled: true,
                fillColor: Colors.grey[300],
              ),
              keyboardType: TextInputType.text,
              autocorrect: false,
              onChanged: (titleText) {
                setState(() {
                   title= titleText;
                });
              },
            ),
          ),

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 2019-08-03
    • 2019-08-29
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 2021-10-17
    相关资源
    最近更新 更多