dart Flutter -展开图标工作不正常

dsf9zpds  于 2023-03-27  发布在  Flutter
关注(0)|答案(2)|浏览(139)

我正在创建可扩展数据。我尝试使用“扩展”图标小部件。但扩展图标无法正常工作。按钮状态未更改。
验证码:

bool _isExpanded = true;
Container(
                color: const Color(0xffeaeaea),
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        child: Text('Description'),
                      ),
                      ExpandIcon(
                          expandedColor: Colors.black,
                          color: Colors.black,
                          isExpanded: _isExpanded,
                          onPressed: (bool _isExpanded) {
                            setState(() {
                              _isExpanded = !_isExpanded;
                            });
                          }),
                    ]),
              ),
            if (_isExpanded) const Text('Data'),
bmp9r5qi

bmp9r5qi1#

你的代码中的问题是你在onPressed方法的回调中使用了私有bool变量,这导致了问题。
下面是工作代码:

class Entry extends StatefulWidget {
  const Entry({Key? key}) : super(key: key);

  @override
  State<Entry> createState() => _EntryState();
}

class _EntryState extends State<Entry> {
  bool _isExpanded = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            color: const Color(0xffeaeaea),
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(
                    child: Text('Description'),
                  ),
                  ExpandIcon(
                    isExpanded: _isExpanded,
                    color: Colors.white,
                    expandedColor: Colors.black,
                    disabledColor: Colors.grey,
                    onPressed: (bool isExpanded) {
                      setState(() {
                        _isExpanded = !isExpanded;
                      });
                    },
                  ),
                ]),
          ),
          if (_isExpanded) const Text('Data'),
        ],
      ),
    ));
  }
}
iyfamqjs

iyfamqjs2#

在MyScreen类内部但在_MyScreenState类外部声明_isExpanded布尔变量,可以使其成为MyScreen类的成员变量。它允许您从build()方法和ExpandIcon小部件的onPressed回调访问该变量。

请尝试此代码:

class MyScreen extends StatefulWidget {
  @override
  State<MyScreen> createState() => _MyScreenState();
}
bool _isExpanded = true;

class _MyScreenState extends State<MyScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My Screen'),
        backgroundColor: Colors.blueGrey[900], // set the background color here
      ),
      body: Center(
        child: Column(
          children: [
            Container(
      color: const Color(0xffeaeaea),
      child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  child: Text('Description'),
                ),
                ExpandIcon(
                    expandedColor: Colors.black,
                    color: Colors.black,
                    isExpanded: _isExpanded,
                    onPressed: (bool _expended) {
                      setState(() {
                        _isExpanded = !_isExpanded;
                      });
                    }),
              ]),
    ),
        if (_isExpanded) const Text('Data'),
    ]
        ),
      ),
    );
  }
}
  • 希望它能解决你的问题!*

相关问题