窗口小部件应在不重建的情况下

时间:2019-10-13 10:35:25

标签: android flutter dart widget

所以我有一个PopUpMenuButton,其中有一些选择,

PopupMenuButton(
    icon: Icon(
      Icons.tune,
      color: FortBuddyTheme.grey,
      size: 24,
    ),
    color: Colors.white,
    onCanceled: () {},
    onSelected: (int value) {
      if (selectedChart == 0) {
        setState(() {
          playlist = 'p2';
          name = 'Solo';
          color = 0xff4af699;
        });
      } else if (selectedChart == 1) {
        setState(() {
          playlist = 'p10';
          name = 'Duo';
          color = 0xFFA29BFE;
        });
      } else if (selectedChart == 2) {
        setState(() {
          playlist = 'p9';
          name = 'Squad';
          color = 0xFF0984E3;
        });
      }
    },
    itemBuilder: (_) => [
      PopupMenuItem(
        child: Text(
          'Show Solos Chart',
          style: TextStyle(
            fontFamily:
                FortBuddyTheme.fontName,
            fontSize: 16,
            letterSpacing: 0.5,
            height: 0.9,
            color:
                FortBuddyTheme.darkerText,
          ),
        ),
        value: 0,
      ),
      PopupMenuItem(
        child: Text(
          'Show Duos Chart',
          style: TextStyle(
            fontFamily:
                FortBuddyTheme.fontName,
            fontSize: 16,
            letterSpacing: 0.5,
            height: 0.9,
            color:
                FortBuddyTheme.darkerText,
          ),
        ),
        value: 1,
      ),
      PopupMenuItem(
        child: Text(
          'Show Squad Chart',
          style: TextStyle(
            fontFamily:
                FortBuddyTheme.fontName,
            fontSize: 16,
            letterSpacing: 0.5,
            height: 0.9,
            color:
                FortBuddyTheme.darkerText,
          ),
        ),
        value: 2,
      ),
    ],
  )),

它有一些if语句,用于检查并设置一些变量,这些变量由窗口小部件使用,但是即使变量在setState下更改,窗口小部件也不会重建。 (小工具)

MainContainer(
        animation: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
            parent: animationController,
            curve:
                Interval((1 / count) * 3, 1.0, curve: Curves.fastOutSlowIn))),
        animationController: animationController,
        title: name,
        key: UniqueKey(),
        child: LineChartSample1(
          key: UniqueKey(),
          playlist: playlist,
          title: '$name' + 's',
          color: color,
        ),
      ),

还请注意,正在通过addAllListData()函数添加窗口小部件,该函数将所有窗口小部件添加到列表中,稍后该列表将通过builder方法进行构建。

1 个答案:

答案 0 :(得分:0)

更改if语句,使用value代替selectedChart。像这样:

if (value == 0)

代替

if (selectedChart == 0)