0

我想做的是,在RawMaterialButton单击按钮时更改 a 的颜色。阅读一下StatefulWidget,它似乎应该工作,但由于某种原因它没有。

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

按钮测试类:

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

我在这里做错了什么?

4

2 回答 2

0

像这样包装 setState() 。

 if(this.mounted) {
      setState(() {
          this.enabled = true;
        });
 }
于 2019-10-21T06:10:19.353 回答
0

有几件事:

  • ButtonState应该被调用ButtonTest,因为这是 StatefulWidget
  • ButtonTest应该被调用ButtonTestState,因为这是State.

然后DetailScreen,在build()方法中,您可以返回StatefulWidget( ButtonTest),如下所示:

@override
Widget build(BuildContext context) {
  return ButtonTest();
}
于 2019-10-21T02:20:06.853 回答