pageView中的appbar文本的幻影动画

发布于 2025-01-24 06:50:56 字数 2498 浏览 0 评论 0原文

我想使标题淡出,当上一个屏幕到达中屏幕时,标题应该消失。下一个标题应逐渐消失,直到中途,下一个屏幕的新标题应逐渐消失。我该如何添加此动画,我使用的是动画播放和AnimatedSwitcher,但它不起作用。代码文件是:

数据类

import 'package:flutter/material.dart';

class DataModel {
  List<String> titles = ['Red1', 'Blue2', 'Green3'];
  List<String> content = ['Red1', 'Blue2', 'Green3'];
}

主屏幕

import 'package:flutter/material.dart';
import 'package:ui_design/model/data.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
  AnimationController controller;
  Animation<double> animation;

  initState() {
    super.initState();
    controller =
        AnimationController(vsync: this, duration: Duration(milliseconds: 100));
    animation = CurvedAnimation(parent: parent, curve: curve)
  }

  //
  DataModel dataModel = DataModel();
  int currentPageIndex = 0;
  bool isPageChanged = true;
  //
  @override
  Widget build(BuildContext context) {
    var appBarTitle = dataModel.titles[currentPageIndex];

    return Scaffold(
      appBar: AppBar(
        leading: const Icon(Icons.menu),
        title: Text(
          appBarTitle,
        ),
      ),
      body: PageView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: dataModel.content.length,
        onPageChanged: (value) => setState(() {
          appBarTitle = dataModel.titles[currentPageIndex];
        }),
        itemBuilder: (BuildContext context, int index) {
          currentPageIndex = index;
          print('Current page index $currentPageIndex');
          return Container(
            alignment: Alignment.center,
            child: Text(
              dataModel.content[index],
              style: const TextStyle(
                fontSize: 22,
                fontWeight: FontWeight.bold,
              ),
            ),
          );
        },
      ),
    );
  }
}


// AnimatedSwitcher(
//       duration: const Duration(milliseconds: 500),
//       transitionBuilder: (Widget child, Animation<double> animation) {
//         return FadeTransition(child: child, opacity: animation);
//       },
//       child: Image.asset(
//         imageList[currentIndex.toInt()],
//         key: ValueKey<int>(currentIndex),
//       ),

I want to make the title fade and the title should disappear when the previous screen reaches the mid Screen. The next title should fade till midway and the and a new title for the next screen should fade in. How can I add this animation I am using AnimatedOpacity and AnimatedSwitcher but it doesn't work. The code files are:

The Data Class

import 'package:flutter/material.dart';

class DataModel {
  List<String> titles = ['Red1', 'Blue2', 'Green3'];
  List<String> content = ['Red1', 'Blue2', 'Green3'];
}

The Main Screen

import 'package:flutter/material.dart';
import 'package:ui_design/model/data.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
  AnimationController controller;
  Animation<double> animation;

  initState() {
    super.initState();
    controller =
        AnimationController(vsync: this, duration: Duration(milliseconds: 100));
    animation = CurvedAnimation(parent: parent, curve: curve)
  }

  //
  DataModel dataModel = DataModel();
  int currentPageIndex = 0;
  bool isPageChanged = true;
  //
  @override
  Widget build(BuildContext context) {
    var appBarTitle = dataModel.titles[currentPageIndex];

    return Scaffold(
      appBar: AppBar(
        leading: const Icon(Icons.menu),
        title: Text(
          appBarTitle,
        ),
      ),
      body: PageView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: dataModel.content.length,
        onPageChanged: (value) => setState(() {
          appBarTitle = dataModel.titles[currentPageIndex];
        }),
        itemBuilder: (BuildContext context, int index) {
          currentPageIndex = index;
          print('Current page index $currentPageIndex');
          return Container(
            alignment: Alignment.center,
            child: Text(
              dataModel.content[index],
              style: const TextStyle(
                fontSize: 22,
                fontWeight: FontWeight.bold,
              ),
            ),
          );
        },
      ),
    );
  }
}


// AnimatedSwitcher(
//       duration: const Duration(milliseconds: 500),
//       transitionBuilder: (Widget child, Animation<double> animation) {
//         return FadeTransition(child: child, opacity: animation);
//       },
//       child: Image.asset(
//         imageList[currentIndex.toInt()],
//         key: ValueKey<int>(currentIndex),
//       ),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自我难过 2025-01-31 06:50:56

您可以尝试窗口小部件,我正在使用pagecontrolleropcaity on title


class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  DataModel dataModel = DataModel();
  int currentPageIndex = 0;
  double opacity = 1;
  late final PageController pageController = PageController(
    initialPage: 0,
  )..addListener(() {
      debugPrint(pageController.page.toString());
      opacity =
          double.tryParse(pageController.page.toString().substring(1)) ?? 1;

      if (opacity < .5) {
        opacity = 1 - opacity * 2;
      }

      setState(() {});
    });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: const Icon(Icons.menu),
        title: Opacity(
          opacity: opacity,
          child: Text(
            dataModel.titles[currentPageIndex],
          ),
        ),
      ),
      body: PageView.builder(
        controller: pageController,
        scrollDirection: Axis.horizontal,
        itemCount: dataModel.content.length,
        onPageChanged: (value) {
          debugPrint("on page changed: $value");
          currentPageIndex = value;
          setState(() {});
        },
        itemBuilder: (BuildContext context, int index) {
          return Container(
            alignment: Alignment.center,
            child: Text(
              dataModel.content[index],
              style: const TextStyle(
                fontSize: 22,
                fontWeight: FontWeight.bold,
              ),
            ),
          );
        },
      ),
    );
  }
}

You can try widget, I am using PageController and Opcaity on title.


class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  DataModel dataModel = DataModel();
  int currentPageIndex = 0;
  double opacity = 1;
  late final PageController pageController = PageController(
    initialPage: 0,
  )..addListener(() {
      debugPrint(pageController.page.toString());
      opacity =
          double.tryParse(pageController.page.toString().substring(1)) ?? 1;

      if (opacity < .5) {
        opacity = 1 - opacity * 2;
      }

      setState(() {});
    });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: const Icon(Icons.menu),
        title: Opacity(
          opacity: opacity,
          child: Text(
            dataModel.titles[currentPageIndex],
          ),
        ),
      ),
      body: PageView.builder(
        controller: pageController,
        scrollDirection: Axis.horizontal,
        itemCount: dataModel.content.length,
        onPageChanged: (value) {
          debugPrint("on page changed: $value");
          currentPageIndex = value;
          setState(() {});
        },
        itemBuilder: (BuildContext context, int index) {
          return Container(
            alignment: Alignment.center,
            child: Text(
              dataModel.content[index],
              style: const TextStyle(
                fontSize: 22,
                fontWeight: FontWeight.bold,
              ),
            ),
          );
        },
      ),
    );
  }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文