在同一行中显示另一个小部件后,如何避免小部件移位?

时间:2019-07-01 07:18:33

标签: flutter

我刚刚开始学习颤振,为了练习,我试图重现iOS联系人应用程序。在此应用程序中,当您点击应用程序标题中的Edit按钮时,左侧的Clear按钮将可见。这是我重拍的两个屏幕截图: enter image description here 红色箭头显示新按钮可用的位置。 现在,当有一个新按钮可用时,“所有/缺少”窗口小部件将向右移动,因为它需要为新窗口小部件释放一些空间。蓝色箭头显示发生位移的位置。

这是包含这些控件的Container子级的内容:

child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Visibility(
            visible: _listState == ListState.EDITING,
            child: InkWell(
              child: Text("Clear",
                  style: kAppleActionButtonTextStyle),
              onTap: () {
                print("You pressed clear and eveything will be deleted");
              },
            ),
          ),
          CupertinoSegmentedControl(
            groupValue: _allOrMissedControlGroupValue,
            onValueChanged: (key) {
              print(key);
              if (key == kAllCalls) {
                setState(() {
                  _showMissingOnly = false;
                  _allOrMissedControlGroupValue = kAllCalls;
                });
              } else {
                setState(() {
                  _showMissingOnly = true;
                  _allOrMissedControlGroupValue = kMissedCalls;
                });
              }
            },
            children: {
              kAllCalls: Padding(
                child: Text(kAllCalls),
                padding: EdgeInsets.symmetric(horizontal: 10),
              ),
              kMissedCalls: Padding(
                child: Text(kMissedCalls),
                padding: EdgeInsets.symmetric(horizontal: 10),
              ),
            },
          ),
          InkWell(
            child:
                Text(_editButtonText, style: kAppleActionButtonTextStyle),
            onTap: () {
              setState(() {
                if (_listState == ListState.VIEWING) {
                  _listState = ListState.EDITING;
                } else {
                  _listState = ListState.VIEWING;
                }
                _editButtonText = editButtonText[_listState];
                getListOfCalls(_showMissingOnly, _listState);
              });
            },
          )
        ],
      ),

如何避免这种情况发生?

1 个答案:

答案 0 :(得分:1)

您有一些选择:

1-向Visibility小部件中添加其他属性。

Visibility(
              maintainSize: true,
              maintainAnimation: true,
              maintainState: true,
              visible: _listState == ListState.EDITING,
              child: InkWell(
                child: Text("Clear", style: kAppleActionButtonTextStyle),
                onTap: () {
                  print("You pressed clear and eveything will be deleted");
                },
              ),
            ),

2-使用Opacity小部件。

Opacity(
              opacity: _listState == ListState.EDITING ? 1.0 : 0.0,
              child: InkWell(
                child: Text("Clear", style: kAppleActionButtonTextStyle),
                onTap: onTap: _listState == ListState.EDITING
                    ? () {
                        print(
                            "You pressed clear and eveything will be deleted");
                      }
                    : null
              ),
            ),