gpt4 book ai didi

dart - 如何按照垂直拖动操作的方向从底部淡入新屏幕?

转载 作者:IT王子 更新时间:2023-10-29 07:12:43 24 4
gpt4 key购买 nike

我需要从屏幕底部导航到一个带有淡入动画的新屏幕,遵循自定义 Material 小部件上的垂直拖动方向。

我创建了两个屏幕,屏幕 -1 和屏幕 -2。在屏幕 - 1 上,我有一个 Container 小部件。我已将小部件包装在 GestureDetector 中,我正在尝试导航到屏幕 - 2 垂直拖动,我正在使用 GestureDetector.onVerticalDrag 属性。

import 'package:flutter/material.dart';

void main(){
runApp(MyApp());
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: {
'/':(context) => ScreenOne(),
'/two': (context) => ScreenTwo(),
},
title: 'Screens',
);
}
}

class ScreenOne extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Screen-1'),
),
body: GestureDetector(
onVerticalDragStart: (DragStartDetails details){
Navigator.pushNamed(context, '/two');
},
child: Container(
margin: EdgeInsets.all(8.0),
color: Colors.red,
),
),
);
}
}

class ScreenTwo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Screen-2'),
),
body: Center(
child: Text('Screen-2'),
),
);
}
}

你能帮我实现预期的过渡吗?我附上一张 GIF 以供引用。

enter image description here

最佳答案

如果您不介意使用 PageView,使用它和 Opacity 小部件会容易得多。这是 demo video .

import 'package:flutter/material.dart';

class TestView extends StatefulWidget {
@override
_TestViewState createState() => _TestViewState();
}

class _TestViewState extends State<TestView> {
PageController pageController;
double panPosition = 1; // dummy value prevents division with 0
double deviceHeight;

void updatePageState() {
setState(() {
panPosition =
pageController.position.pixels.abs(); // updates pan position
});
}

@override
void initState() {
// TODO: implement initState
super.initState();
pageController = PageController(
keepPage: true,
);
pageController
.addListener(updatePageState); // add listener to page controller.
}

@override
Widget build(BuildContext context) {
deviceHeight =
MediaQuery.of(context).size.height; //get device screen height
return Scaffold(
backgroundColor: Colors.white,
body: PageView(
controller: pageController,
scrollDirection: Axis.vertical, //vertical scroll
children: <Widget>[
Opacity(
// opacity handles the transition effect
opacity: 1 - (panPosition / deviceHeight),
//first screen opacity goes from 1 to 0
child: ScreenOne(),
),
Opacity(
opacity: (panPosition / deviceHeight),
//first screen opacity goes from 0 to 1
child: ScreenTwo(
title: "this title is from parent widget.",
),
)
],
),
);
}
}

class ScreenOne extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Screen-1'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Container(
height: 96,
color: Colors.red,
)
],
),
);
}
}

class ScreenTwo extends StatelessWidget {
final String title;

const ScreenTwo({Key key, this.title}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Text(title),
),
);
}
}

关于dart - 如何按照垂直拖动操作的方向从底部淡入新屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54081855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com