如何从每次扑波中开始淡出淡出的过渡动画?
在页面视图中,每当更改页面时,都应更改标头,并且有一个按钮可以执行此操作。有一个名为标题的文本列表,每次按下按钮时,都应使用另一个使用淡出过渡动画更改文本。现在,我真正想实现的目标是,按下按钮时,下一个文本应用0不透明度替换当前文本,并且在一秒钟内,它的不透明度应达到1,并且应在没有任何缺陷的情况下执行,并且每次页面时都会顺利进行。更改。
我正在努力的是,当动画完成而没有对UI的任何副作用时,我无法将动画值设置为0。 P>
简而言之,我希望每次按下按钮时从零开始开始。
您可以观看动画通过链接 https://youtube.com/shorts/3aiawjhz2am?feature=share 。。
0); 以某种方式工作不如预期...
这是我的业务逻辑:
class AnnouncementController extends ChangeNotifier {
late AnimationController controller;
late Tween<double> tween;
late Animation<double> opacityAnimation;
PageController pageController = PageController();
List<String> headers = [
'Welcome back, Jasurbek',
'What do you offer?',
'What kind of accommodation do you have?',
'Tell the guests about the advantages of your dwelling',
'Add photos of the property',
'Let\'s think of a bright title',
'Let\'s set the price',
];
navigate(int index){
currentPageIndex = index;
notifyListeners();
controller.forward(from: 0);
pageController.animateToPage(
index,
duration: const Duration(milliseconds: 500),
curve: Curves.ease,
);
}
}
这是我的主要类:
class _AnnouncementState extends State<Announcement> with SingleTickerProviderStateMixin {
final AnnouncementController viewModel = AnnouncementController();
@override
void initState() {
viewModel.controller = AnimationController(vsync: this, duration: const Duration(seconds: 1));
viewModel.tween = Tween(begin: 0.0, end: 1.0);
viewModel.opacityAnimation = viewModel.tween.animate(viewModel.controller);
viewModel.controller.forward();
super.initState();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (BuildContext context) => viewModel,
child: Consumer<AnnouncementController>(builder: (ctx, model, index) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
height: 250,
width: MediaQuery.of(context).size.width,
padding: const EdgeInsets.all(20),
child: FadeTransition(
opacity: viewModel.opacityAnimation,
child: Text(
viewModel.headers[viewModel.currentPageIndex],
style: TextStyle(
color: Colors.black,
fontSize: 26.sp,
fontWeight: FontWeight.bold,
),
),
)
),
Expanded(
child: PageView(
controller: viewModel.pageController,
physics: const NeverScrollableScrollPhysics(),
children: [
FirstPage(viewModel: viewModel),
SecondPage(viewModel: viewModel),
ThirdPage(viewModel: viewModel),
FourthPage(viewModel: viewModel),
FifthPage(viewModel: viewModel),
],
onPageChanged: (int index) {
viewModel.navigate(index);
},
),
),
],
),
);
}),
);
}
}
Showcase https://youtube.com/shorts/3aiawjhz2am?feature=share Share
In page view, there is a header that should be changed whenever the page is changed and there is a button to perform this operation. There is a list of texts named headers, and every time the button is pressed the text should be changed with another one with a fade transition animation. Now what I really want to achieve is that when the button is pressed the next text should replace the current text with 0 opacity and within a second its opacity should go up to 1 and it should be performed without any flaws and smoothly every time the page changes.
What I'm struggling with is that I can't set the animation value to 0 when the animation is completed without any side effects to the UI, it shouldn’t be noticeable.
In short, I want the animation value to start from zero every time the button is pressed.
You can watch how the animation is working via the link https://youtube.com/shorts/3aIAWjhZ2AM?feature=share.
The controller.forward(from: 0);
is somehow working not as expected...
Here is my business logic:
class AnnouncementController extends ChangeNotifier {
late AnimationController controller;
late Tween<double> tween;
late Animation<double> opacityAnimation;
PageController pageController = PageController();
List<String> headers = [
'Welcome back, Jasurbek',
'What do you offer?',
'What kind of accommodation do you have?',
'Tell the guests about the advantages of your dwelling',
'Add photos of the property',
'Let\'s think of a bright title',
'Let\'s set the price',
];
navigate(int index){
currentPageIndex = index;
notifyListeners();
controller.forward(from: 0);
pageController.animateToPage(
index,
duration: const Duration(milliseconds: 500),
curve: Curves.ease,
);
}
}
Here is my main class:
class _AnnouncementState extends State<Announcement> with SingleTickerProviderStateMixin {
final AnnouncementController viewModel = AnnouncementController();
@override
void initState() {
viewModel.controller = AnimationController(vsync: this, duration: const Duration(seconds: 1));
viewModel.tween = Tween(begin: 0.0, end: 1.0);
viewModel.opacityAnimation = viewModel.tween.animate(viewModel.controller);
viewModel.controller.forward();
super.initState();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (BuildContext context) => viewModel,
child: Consumer<AnnouncementController>(builder: (ctx, model, index) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
height: 250,
width: MediaQuery.of(context).size.width,
padding: const EdgeInsets.all(20),
child: FadeTransition(
opacity: viewModel.opacityAnimation,
child: Text(
viewModel.headers[viewModel.currentPageIndex],
style: TextStyle(
color: Colors.black,
fontSize: 26.sp,
fontWeight: FontWeight.bold,
),
),
)
),
Expanded(
child: PageView(
controller: viewModel.pageController,
physics: const NeverScrollableScrollPhysics(),
children: [
FirstPage(viewModel: viewModel),
SecondPage(viewModel: viewModel),
ThirdPage(viewModel: viewModel),
FourthPage(viewModel: viewModel),
FifthPage(viewModel: viewModel),
],
onPageChanged: (int index) {
viewModel.navigate(index);
},
),
),
],
),
);
}),
);
}
}
Showcase
https://youtube.com/shorts/3aIAWjhZ2AM?feature=share
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请使用
动画容器
,您可以在其中添加动画,然后将视图放入子女文档link- https://docs.flutter.dev/cookbook/animation/animation/animated-container
Please use
animated container
and you can add your animation in that and put your view inside as childDocument link- https://docs.flutter.dev/cookbook/animation/animated-container