【发布时间】:2018-09-25 20:28:44
【问题描述】:
我有一个显示页面列表的 pageView。
应用程序提供了一个 + 按钮以在集合末尾添加一个新页面。
成功创建新页面后,我需要 pageView 自动跳转到最后一页。
如果我尝试重新显示提供提供程序的视图,并将 initialPosition 设置为最后一个 pageView 索引,它不起作用
PageController(initialPage: 0, keepPage: false);
有什么实现思路吗?
- 使用监听器(但哪个监听器?)
完整代码:
@override
Widget build(BuildContext context) {
if (_userId == null) {
return Scaffold(body: Center(child: Text("Loading experiences")));
}
print('Rebuilding entire view.');
return Scaffold(
appBar: new AppBar(
title: StreamBuilder<ExperiencesInfo>(
stream: _experiencesInfoStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
_selectedExperience = snapshot.data.currentExperience;
return Text(snapshot.data.currentExperience.name);
} else {
return Center();
}
}),
),
body: new Container(
padding: new EdgeInsets.only(
top: 16.0,
),
decoration: new BoxDecoration(color: Colors.yellow),
child: Column(
children: <Widget>[
Expanded(
child:
StreamBuilder<List<Experience>>(
stream: _userExperiencesStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return _createExperiencesPagesWidget(snapshot.data);
} else {
return Center();
}
})
),
_buildPageIndicator(),
Padding(
padding: EdgeInsets.only(bottom: 20.0),
)
],
),
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.add),
onPressed: () {
_displayAddMenu();
}),
);
}
_createExperiencesPagesWidget(List<Experience> experiences) {
print('Creating ExperiencesPagesWidget ${experiences}');
return PageView.builder(
physics: new AlwaysScrollableScrollPhysics(),
controller: _pageController,
itemCount: experiences.length,
itemBuilder: (BuildContext context, int index) {
return ConstrainedBox(
constraints: const BoxConstraints.expand(),
child: Column(children: <Widget>[
_buildMoodIndicator(experiences[index]),
_buildMoodSelector(),
]));
},
onPageChanged: (index) {
if (_actionType == ActionType.none) {
print('page changed to index: ${index}, updating stream.');
_experiencesViewModel.experienceIndexSink.add(index);
} else {
_actionType = ActionType.none;
}
},
);
页面控制器被定义为类属性
PageController _pageController = PageController(initialPage: 0, keepPage: false);
【问题讨论】:
标签: flutter