如何在StatelessWidget内更改StatefulWidget的状态?

时间:2018-10-22 13:25:15

标签: dart widget flutter state statefulwidget

只需测试抖动即可。下面的代码示例是一个非常简单的flutter应用程序。问题是我不知道如何在每次按下更改按钮时调用TestTextState类内的setState()函数来更改文本。

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);
  }
}

3 个答案:

答案 0 :(得分:0)

基于现有代码的解决方案

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);
  }
}

但这不是最好的主意-在有状态小部件内使用非最终字段

P.S。 您也可以使用-scoped_model

答案 1 :(得分:0)

这是没有办法的。将您的 StatelessWidget 转换为 StatefulWidget 的方式。

答案 2 :(得分:0)

我通过将_TestTextState初始化为TestText小部件的最终属性来解决此问题,该属性允许在按下更改按钮时简单地更新状态。看来这是一个简单的解决方案,但我不确定这是否是一个好习惯。

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);
  }
}