首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在StatefulWidget中更改StatelessWidget状态?

如何在StatefulWidget中更改StatelessWidget状态?
EN

Stack Overflow用户
提问于 2018-10-22 13:25:14
回答 3查看 8.1K关注 0票数 6

只是在测试颤振。下面的代码示例是一个非常简单的颤振应用程序。问题是,我不知道如何在setState类中调用TestTextState ()函数,以便每次按下“更改”按钮时更改文本。

代码语言:javascript
复制
import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Test app',
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text("Test"),
        ),
        body: new Test(),
      ),
    );
  }
}

class Test extends StatelessWidget {

  final TestText testText = new TestText();

  void change() {
    testText.text == "original" ? testText.set("changed") : testText.set("original");
  }

  @override
  Widget build(BuildContext context) {
    return new Column(
      children: [
        testText,
        new RaisedButton(
            child: new Text("change"),
            onPressed: () => change(),
        ),
      ]
    );
  }
}

class TestText extends StatefulWidget {

  String text = "original";

  void set(String str) {
    this.text = str;
  }

  @override
  TestTextState createState() => new TestTextState();
}

class TestTextState extends State<TestText> {

  @override
  Widget build(BuildContext context) {
    return new Text(this.widget.text);
  }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-10-25 04:16:59

我已经通过将_TestTextState初始化为TestText小部件的最终属性来解决这个问题,它允许在按下“更改”按钮时简单地更新状态。这似乎是一个简单的解决方案,但我不确定这是否是一个良好的实践。

代码语言:javascript
复制
import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Test app',
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text("Test"),
        ),
        body: new Test(),
      ),
    );
  }
}

class Test extends StatelessWidget {

  final _TestText text = new _TestText();

  @override
  Widget build(BuildContext context) {
    return new Column(
        children: [
          text,
          new RaisedButton(
            child: new Text("change"),
            onPressed: () => text.update(),
          ),
        ]
    );
  }
}

class TestText extends StatefulWidget {

  final _TestTextState state = new _TestTextState();

  void update() {
    state.change();
  }

  @override
  _TestTextState createState() => state;
}

class _TestTextState extends State<TestText> {

  String text = "original";

  void change() {
    setState(() {
      this.text = this.text == "original" ? "changed" : "original";
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Text(this.text);
  }
}
票数 3
EN

Stack Overflow用户

发布于 2018-10-22 13:39:23

他们不可能这样做。任何您必须如何将StatelessWidget转换为StatefulWidget的方法。

票数 1
EN

Stack Overflow用户

发布于 2018-10-22 13:38:49

基于现有代码的解决方案

代码语言:javascript
复制
class Test extends StatelessWidget {
  final StreamController<String> streamController = StreamController<String>.broadcast();

  @override
  Widget build(BuildContext context) {
    final TestText testText = TestText(streamController.stream);
    return new Column(children: [
      testText,
      new RaisedButton(
        child: Text("change"),
        onPressed: () {
          String text = testText.text == "original" ? "changed" : "original";
          streamController.add(text);
        },
      ),
    ]);
  }
}

class TestText extends StatefulWidget {
  TestText(this.stream);
  final Stream<String> stream;
  String text = "original";

  @override
  TestTextState createState() => new TestTextState();
}

class TestTextState extends State<TestText> {
  @override
  void initState() {
    widget.stream.listen((str) {
      setState(() {
        widget.text = str;
      });
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Text(widget.text);
  }
}

但这不是最好的主意--在中使用非最终字段

你也可以使用这个- 模型

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52930529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档