gpt4 book ai didi

flutter - 你怎么知道 DraggableScrollableSheet 是折叠还是展开

转载 作者:IT王子 更新时间:2023-10-29 06:56:08 24 4
gpt4 key购买 nike

在我的应用程序中有一个 DraggableScrollableSheet 和一个 FAB。如果展开 DraggableScrollableSheet,我希望 FAB 不可见。我需要检查扩展事件

我尝试将监听器附加到 scrollController 并检查 scrollController.offset 的值。但我意识到监听器是在 DraggableScrollableSheet 完全展开时触发的,而不是之前。是否有另一种方法来检查它是展开还是折叠?

  double appbarSize = 0.08;
double offsetVisibility=100.0;
bool FAB_visibility=true;
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: Visibility(
visible: FAB_visibility,
child: FloatingActionButton(
child: Icon(Icons.add),
),
),
body: SizedBox.expand(
child: DraggableScrollableSheet(
maxChildSize: 0.8,
minChildSize: appbarSize,
initialChildSize: appbarSize,
builder: (BuildContext context, ScrollController scrollController) {
_scrollListener() {
if(FAB_visibility==false && scrollController.offset<=offsetVisibility){
setState(() {
FAB_visibility=true;
});
}
else if(FAB_visibility==true && scrollController.offset>offsetVisibility){
setState(() {
FAB_visibility=false;
});
}
}
scrollController.addListener(_scrollListener);
return Container(
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(10.0),
topRight: const Radius.circular(10.0))),
child: ListView.builder(
controller: scrollController,
itemCount: 25,
itemBuilder: (BuildContext context, int index) {
return ListTile(title: Text('Item $index'));
},
),
);
},
)),
);
}

最佳答案

Thanks to flutter team我明白怎么做了!我们应该将 DraggableScrollableSheet 包装在 NotificationListener 中

这是工作代码:

class _MyHomePageState extends State<MyHomePage> {
double appbarSize = 0.08;
double offsetVisibility = 100.0;
bool FAB_visibility = true;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox.expand(
child: NotificationListener<DraggableScrollableNotification>(
onNotification: (DraggableScrollableNotification DSNotification){
if(FAB_visibility && DSNotification.extent>=0.2){
setState(() {
FAB_visibility=false;
});
}
else if(!FAB_visibility && DSNotification.extent<0.2){
setState(() {
FAB_visibility=true;
});
}
},
child: DraggableScrollableSheet(
maxChildSize: 0.8,
minChildSize: appbarSize,
initialChildSize: appbarSize,
builder: (BuildContext context, ScrollController scrollController) {
return Container(
child: ListView.builder(
controller: scrollController,
itemCount: 25,
itemBuilder: (BuildContext context, int index) {
return ListTile(title: Text('Item $index'));
},
),
);
},
),
)),
floatingActionButton: Visibility(
visible: FAB_visibility,
child: FloatingActionButton(
child: Icon(Icons.add),
),
),
);
}
}

关于flutter - 你怎么知道 DraggableScrollableSheet 是折叠还是展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57059314/

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