gpt4 book ai didi

flutter - Flutter:ScrollController initialScrollOffset无法正常工作

转载 作者:行者123 更新时间:2023-12-03 03:57:21 26 4
gpt4 key购买 nike

我正在尝试使用自定义ScrollController初始化SingleChildScrollView以从某个位置开始。我以为我可以使用initialScrollOffset并在init方法中设置一个初始值。但是以某种方式,当SingleChildScrollView呈现时,它只会在首次构建时跳到initialOffset,然后当我导航到该Widget的另一个实例时,它不会跳到initialOffset位置。

我不知道为什么,如果我很幸运,也许你们中的一个有答案。

这是我的代码:

class Artiklar extends StatefulWidget {
final String path;
final double arguments;

Artiklar({
this.path,
this.arguments,
});

@override
_ArtiklarState createState() => _ArtiklarState(arguments: arguments);
}

class _ArtiklarState extends State<Artiklar> {
final double arguments;

_ArtiklarState({this.arguments});

ScrollController _scrollController;
double scrollPosition;

@override
void initState() {
super.initState();
double initialOffset = arguments != null ? arguments : 22.2;
_scrollController = ScrollController(initialScrollOffset: initialOffset);
}

@override
void dispose() {
_scrollController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
final bool isAdmin = Provider.of<bool>(context) ?? false;

var pathElements = widget.path.split('/');
String tag;
if (pathElements.length == 2) {
tag = null;
} else if (pathElements.length == 3) {
tag = pathElements[2];
} else {
tag = null;
}

return StreamBuilder<List<ArtikelData>>(
stream: DatabaseService(tag: tag).artiklarByDate,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
return GlobalScaffold(
body: SingleChildScrollView(
child: Container(
child: Center(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
GradientHeading(text: "Artiklar", large: true),
isAdmin
? NormalButton(
text: "Skapa ny artikel",
onPressed: () {
Navigator.pushNamed(
context, createNewArtikelRoute);
},
)
: Container(),
SizedBox(height: 10),
SingleChildScrollView(
controller: _scrollController,
scrollDirection: Axis.horizontal,
child: TagList(path: tag),
),
SizedBox(height: 10),
LatestArtiklar(
snapshot: snapshot,
totalPosts: snapshot.data.length,
numberOfPosts: 10,
),
],
),
),
),
),
),
);
} else if (!snapshot.hasData) {
return GlobalScaffold(
body: SingleChildScrollView(
child: Container(
child: Center(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
GradientHeading(text: "Artiklar", large: true),
SizedBox(height: 10),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: TagList(path: tag),
),
SizedBox(height: 10),
LatestArtiklar(hasNoPosts: true)
],
),
),
),
),
),
);
} else {
return GlobalScaffold(
body: Center(child: CircularProgressIndicator()),
);
}
},
);
}
}

最佳答案

那是因为该窗口小部件已经建立在树上,因此不会为该窗口小部件再次调用initState

例如,您可以覆盖didUpdateWidget方法,该方法将在每次重新构建小部件时触发并使其跳转到该位置。

@override
void didUpdateWidget(Widget old){
super.didUpdateWidget(old);
_scrollController.jumpTo(initialOffset);
}

关于flutter - Flutter:ScrollController initialScrollOffset无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60007833/

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