6

只是测试颤振。下面的代码示例是一个非常简单的颤振应用程序。问题是我不知道如何调用 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);
  }
}
4

3 回答 3

3

我通过将 _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);
  }
}
于 2018-10-25T04:16:59.643 回答
1

他们是没有办法这样做的。您必须如何将StatelessWidget转换为StatefulWidget

于 2018-10-22T13:39:23.137 回答
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);
  }
}

但这不是最好的主意 - 在 Stateful Widget 中使用 non-final 字段

PS你也可以使用这个 - scoped_model

于 2018-10-22T13:38:49.587 回答