【问题标题】:Flip all cards when click a button using flip_card flutter使用 Flip_card 颤动单击按钮时翻转所有卡片
【发布时间】:2021-04-30 06:05:57
【问题描述】:

解决了看看答案

我正在使用翻转卡包制作翻转卡。
我在同一页上有很多卡片,我想在按下按钮时将它们全部翻转。
我使用了文档中的示例:

GlobalKey<FlipCardState> cardKey = GlobalKey<FlipCardState>();

@override
Widget build(BuildContext context) {
  return FlipCard(
    key: cardKey,
    flipOnTouch: false,
    front: Container(
      child: RaisedButton(
        onPressed: () => cardKey.currentState.toggleCard(),
        child: Text('Toggle'),
      ),
    ),
    back: Container(
      child: Text('Back'),
    ),
  );
}

但我收到错误 Duplicate GlobalKey detected in widget tree.Multiple widgets used the same GlobalKey
那么我能做些什么来解决这个问题呢?

【问题讨论】:

  • 为什么需要全局密钥?我的意思是,你有没有用它做任何事情?
  • 不,我没有用它做任何事情,但我使用了这种方法,因为我在文档中找到了它:pub.dev/packages/flip_card 是否有另一种方法可以翻转所有卡片?

标签: android ios flutter flutter-layout flutter-dependencies


【解决方案1】:

我通过制作全局键映射解决了这个问题
var cardKeys = Map&lt;int, GlobalKey&lt;FlipCardState&gt;&gt;();
并在ListView.builderitemBuilder 我添加了

cardKeys.putIfAbsent(index, () => GlobalKey<FlipCardState>());
GlobalKey<FlipCardState> thisCard = cardKeys[index];

FlipCard 中我添加了key: thisCard

然后我在按钮的onPressed函数中做了一个简单的for循环

              RaisedButton(
                onPressed: () {
                  for (int i = 0; i < names.length; i++) {
                    cardKeys[i].currentState.toggleCard();
                  }
                },
                child: Text('Toggle'),
              ),

感谢这个答案here

【讨论】:

    猜你喜欢
    • 2021-05-20
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2021-01-21
    • 2020-04-12
    相关资源
    最近更新 更多