gpt4 book ai didi

flutter - 将监听器附加到 scrollController

转载 作者:IT王子 更新时间:2023-10-29 07:17:04 26 4
gpt4 key购买 nike

我正在 flutter 中使用 DraggableScrollableSheet,我想知道工作表是折叠还是展开。 DraggableScrollableSheet 的构建器提供了一个 scrollController,我为其附加了一个监听器。

我看到这个 helpful guide但是他们创建了一个 ScrollController,但在我的例子中,scrollController 是由构造函数提供的。我不明白我怎么能得到他们相同的行为

这是我的代码:

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox.expand(
child: myBottomSheet()
),
);
}
}

class myBottomSheet extends StatefulWidget{
@override
_myBottomSheetState createState() => _myBottomSheetState();
}

class _myBottomSheetState extends State<myBottomSheet> {
double appbarSize=0.08;
@override
Widget build(BuildContext context) {
return DraggableScrollableSheet(
maxChildSize: 0.8,
minChildSize: appbarSize,
initialChildSize: appbarSize,
builder: (BuildContext context, ScrollController scrollController) {
scrollController.addListener(_scrollListener); // <----------------
return Container(
child: ListView.builder(
controller: scrollController,
itemCount: 25,
itemBuilder: (BuildContext context, int index) {
return ListTile(title: Text('Item $index'));
},
),
);
},
);
}

_scrollListener(){
//use controller such in the guide
}

最佳答案

您只需要重构代码。在构建器中定义您的 _scrollListener 方法。

见下文:

class _myBottomSheetState extends State<myBottomSheet> {
double appbarSize=0.08;
@override
Widget build(BuildContext context) {
return DraggableScrollableSheet(
maxChildSize: 0.8,
minChildSize: appbarSize,
initialChildSize: appbarSize,
builder: (BuildContext context, ScrollController scrollController) {
_scrollListener(){
//use controller such in the guide
}

scrollController.addListener(_scrollListener); // <----------------
return Container(
child: ListView.builder(
controller: scrollController,
itemCount: 25,
itemBuilder: (BuildContext context, int index) {
return ListTile(title: Text('Item $index'));
},
),
);
},
);
}

关于flutter - 将监听器附加到 scrollController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57054320/

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