如何在flutter中使用lerp()方法以及用例是什么?

Bal*_*esh 9 flutter

我实际上在许多 Flutter 动画中都遇到过 lerp 函数。我研究了线性插值。但我想知道它在颤振中是如何使用的以及它的用例是什么。谁能解释一下?

iDe*_*ode 10

它在两个值之间进行线性插值,例如:

var color = Color.lerp(Colors.white, Colors.black, 0.5);
var value = lerpDouble(10, 20, 0.5); // 15
Run Code Online (Sandbox Code Playgroud)

color这里将有白色和黑色之间的中间值。


The*_*oem 9

您还可以使用它在选项卡视图上拖动期间为颜色设置动画。

BTN

var unselectedColor = Theme.of(context).unselectedWidgetColor;
var selectedColor = Theme.of(context).primaryColor;

Color color;

bool isSelected = itemIndex == tabController.index;
int currentIndex = tabController.index;

double offset = tabController.offset;

bool dragToRight = offset > 0;

if (dragToRight) {
  if (itemIndex >= currentIndex && itemIndex <= currentIndex + 1) {
    color = isSelected
        ? Color.lerp(selectedColor, unselectedColor, offset)
        : Color.lerp(unselectedColor, selectedColor, offset);
  }
} else {
  if (itemIndex >= currentIndex - 1 && itemIndex <= currentIndex) {
    color = isSelected
        ? Color.lerp(selectedColor, unselectedColor, -offset)
        : Color.lerp(unselectedColor, selectedColor, -offset);
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 不是我们想要的答案,而是我们需要的答案。谢谢。 (4认同)
  • @AbdolHussainMozaffari https://gist.github.com/theachoem/d92b0ec4d0b23df7678e13aa0145b623 (2认同)