StatefulWidget中的setState()无法正常工作

时间:2019-10-20 19:35:43

标签: flutter dart statefulwidget

我要尝试做的是单击按钮时更改RawMaterialButton的颜色。阅读有关StatefulWidget的信息,它似乎应该可以工作,但是由于某种原因,它不起作用。

flutter: Another exception was thrown: setState() called in constructor: ButtonTest#1a93b(lifecycle state: created, no widget, not mounted)

ButtonTest类:

class ButtonState extends StatefulWidget {
  @override
  State createState() => ButtonTest();
}

class ButtonTest extends State<ButtonState> implements Cipher {
  @override
  String icon = '';

  @override
  String title = '';

  bool enabled = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
      ),
      body: RawMaterialButton(
          shape: CircleBorder(side: BorderSide(color: Colors.black)),
          fillColor: enabled ? Colors.blue : Colors.red,
          onPressed: () {
            setState(() {
              this.enabled = true;
            });
          },
          padding: EdgeInsets.all(0)),
    );
  }
}

密码类:

abstract class Cipher {
  String icon;
  String title;
  Widget build(BuildContext context);
}

getCiphers()

getCiphers() {
  final List<Cipher> ciphers = new List();

  ciphers.add(ButtonTest());
  return ciphers;
}

主类:

void main() => runApp(CipherTools());

class CipherTools extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'CipherTools',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: CipherScreen(
        ciphers: getCiphers(),
      ),
    );
  }
}

class CipherScreen extends StatelessWidget {
  final List<Cipher> ciphers;

  CipherScreen({Key key, @required this.ciphers}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Ciphers'),
      ),
      body: ListView.builder(
        itemCount: ciphers.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(ciphers[index].title),
            // When a user taps on the ListTile, navigate to the DetailScreen.
            // Notice that we're not only creating a DetailScreen, we're
            // also passing the current todo through to it!
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => DetailScreen(cipher: ciphers[index]),
                ),
              );
            },
          );
        },
      ),
    );
  }
}

class DetailScreen extends StatelessWidget {
  // Declare a field that holds the Todo
  final Cipher cipher;

  // In the constructor, require a Todo
  DetailScreen({Key key, @required this.cipher}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return cipher.build(context);
  }
}

我在这里做什么错了?

2 个答案:

答案 0 :(得分:0)

几件事:

  • ButtonState应该称为ButtonTest,因为这是 StatefulWidget
  • ButtonTest应该称为ButtonTestState,因为它是State

然后在DetailScreen的{​​{1}}方法中,您可以返回build()StatefulWidget),如下所示:

ButtonTest

答案 1 :(得分:0)

像这样包装setState()。

 if(this.mounted) {
      setState(() {
          this.enabled = true;
        });
 }