CustomScrollview自定义滚动颤音
我一直在学习,并试图做一些项目并堆放。因此,我创建了CustomScrollview,并且必须完全打开或完全关闭SliverAppbar。我试图做,但在动画滚动方面有问题。
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
),
body: const MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _scrollController = ScrollController();
bool isClosed = false;
@override
void initState() {
super.initState();
_scrollController.addListener(() {
if (_scrollController.offset >= 5 && _scrollController.offset <= 200) {
if (isClosed) {
_scrollController.jumpTo(0);
close();
} else {
_scrollController.jumpTo(202);
close();
}
}
});
}
close() {
setState(() {
isClosed = !isClosed;
});
}
@override
Widget build(BuildContext context) {
return CustomScrollView(
controller: _scrollController,
slivers: [
SliverAppBar(
expandedHeight: 200,
flexibleSpace: SizedBox(
child: Container(
height: 200,
decoration: const BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS5-OgasVpm-kc2HaOUloxKVlLzLuM6Q53mfA&usqp=CAU"),
fit: BoxFit.cover)),
),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
color: Color(index),
height: 100,
width: MediaQuery.of(context).size.width,
child: Text("$index"),
);
},
childCount: 10,
))
],
);
}
}
当使用Jumpto时,它起作用,但是使用Animateto时会出现问题。谢谢你!
I have been studing and trying to do some projects and stack in this. So, I created CustomScrollView and the SliverAppBar has to be either fully opened or fully closed. I tried to do but have problems with animation scrolling.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
),
body: const MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _scrollController = ScrollController();
bool isClosed = false;
@override
void initState() {
super.initState();
_scrollController.addListener(() {
if (_scrollController.offset >= 5 && _scrollController.offset <= 200) {
if (isClosed) {
_scrollController.jumpTo(0);
close();
} else {
_scrollController.jumpTo(202);
close();
}
}
});
}
close() {
setState(() {
isClosed = !isClosed;
});
}
@override
Widget build(BuildContext context) {
return CustomScrollView(
controller: _scrollController,
slivers: [
SliverAppBar(
expandedHeight: 200,
flexibleSpace: SizedBox(
child: Container(
height: 200,
decoration: const BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS5-OgasVpm-kc2HaOUloxKVlLzLuM6Q53mfA&usqp=CAU"),
fit: BoxFit.cover)),
),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
color: Color(index),
height: 100,
width: MediaQuery.of(context).size.width,
child: Text("$index"),
);
},
childCount: 10,
))
],
);
}
}
It works when jumpTo is used but has problems when animateTo is used. Thank You!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在
Future Delay
内包装.animateto()
。以下代码对我有用
,或者您也可以在
FrameCallback
中包装。You need to wrap
.animateTo()
inside aFuture delay
.The below code worked for me
Or you can also wrap inside
PostFrameCallback