【问题标题】:Create gradient between background colors of PageView pages in Flutter?在 Flutter 中的 PageView 页面的背景颜色之间创建渐变?
【发布时间】:2019-01-26 15:20:17
【问题描述】:

我有一个 4 页的 PageView,设置如下:

PageView(
          children: [
              _buildPage(color: Colors.orange[600]),
              _buildPage(color: Colors.deepPurple[400]),
              _buildPage(color: Colors.yellow[300]),
              _buildPage(color: Colors.blue[400]),
            ],
          ),

在每个页面中,我都有一个容器,其中包含作为参数传入的颜色:

Widget _buildPage({color: Color}) {
    return Container(
      color: color,
      ...

每当我在页面上滑动时,颜色之间都会出现硬过渡

我想拥有它,这样每当我在页面上滑动时,来自一个页面的颜色会作为渐变淡入新页面的颜色。有可能这样做吗?我对 Flutter 很陌生,我还没有找到任何关于这方面的内容。

编辑:这就是我希望它们的样子。

Swiping across quickly.
And slowly.

我正在添加 gfycat 链接,因为下载的 gif 播放速度太慢。

【问题讨论】:

  • 您可以添加所需效果的 gif 吗?可能有很多潜在的结果
  • @RémiRousselet 谢谢你的建议,我加了他们
  • 但是内容如何转换呢?
  • @RémiRousselet 哦,内容转换正常,只是被刷掉了。只有背景以渐变方式淡出。

标签: flutter flutter-layout


【解决方案1】:

您可以使用TweenSequence 在多个补间之间进行转换。结合ColorTween 定义颜色过渡。

然后您可以通过收听您的PageController 来使用AnimatedBuilder 将其全部打包。

class Home extends StatefulWidget {
  @override
  HomeState createState() {
    return new HomeState();
  }
}

class HomeState extends State<Home> {
  PageController pageController;
  Animatable<Color> background;

  @override
  void initState() {
    _initialize();
    super.initState();
  }

  void _initialize() {
    background = TweenSequence<Color>([
      TweenSequenceItem(
        weight: 1.0,
        tween: ColorTween(
          begin: Colors.orange[600],
          end: Colors.deepPurple[400],
        ),
      ),
      TweenSequenceItem(
        weight: 1.0,
        tween: ColorTween(
          begin: Colors.deepPurple[400],
          end: Colors.yellow[300],
        ),
      ),
      TweenSequenceItem(
        weight: 1.0,
        tween: ColorTween(
          begin: Colors.yellow[300],
          end: Colors.blue[400],
        ),
      ),
    ]);
    pageController = PageController();
  }

  @override
  void reassemble() {
    pageController.dispose();
    _initialize();
    super.reassemble();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnimatedBuilder(
        animation: pageController,
        builder: (context, child) {
          final color = pageController.hasClients ? pageController.page / 3 : .0;

          return DecoratedBox(
            decoration: BoxDecoration(
              color: background.evaluate(AlwaysStoppedAnimation(color)),
            ),
            child: child,
          );
        },
        child: PageView(
          controller: pageController,
          children: [
            Center(child: Text("Orange")),
            Center(child: Text("Purple")),
            Center(child: Text("Lime")),
            Center(child: Text("Blue")),
          ],
        ),
      ),
    );
  }
}

【讨论】:

  • 你知道需要导入什么吗?我不断收到未定义 TweenSequence 和 TweenSequenceItem 的错误。我尝试导入动画包,但没有这样做。
  • 平常的东西。但这是一个新功能。你可能需要升级 Flutter 甚至切换到 master 分支
  • 你能解释一下,为什么pageController.page / 3这个?
  • 因为有 4 个页面的 pagescrool 是宽度的三倍(第一页的 pageController.page =0,第二页的值 pageController.page =1 或一个宽度等一个)。因此,如果您有 7 页,则需要除以 6 (n-1)
猜你喜欢
  • 1970-01-01
  • 2020-04-30
  • 2022-12-21
  • 1970-01-01
  • 2019-11-17
  • 2021-03-16
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
相关资源
最近更新 更多