flutter - Flutter:通过 NavigationBar 更改子项中的滚动偏移量
问题描述
我目前有一个主页,它由一个Scaffold
, 和一个bottomAppBar
用于导航的:
body 有 5 页,第一页是 feed,它由一个ListView
Widgets 组成。我想做的和 Instagram 一样:当我向下滚动提要并单击导航栏上的提要按钮时,我希望它ListView
自动滚动回顶部。
这是我的代码的一部分:
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: [
HomeFeed(),
Page2(),
...
].elementAt(_selectedIndex),
bottomNavigationBar: BottomAppBar(
child: Row(
children: <Widget>[
IconButton(
icon: FaIcon(FontAwesomeIcons.houseUser),
onPressed: (){
if (_selectedIndex == 0) {
//add logic to make the HomeFeed ListView scroll up
} else {
setState((){
_selectedIndex = 0;
});
}
},
IconButton(
icon: FaIcon(FontAwesomeIcons.compass),
onPressed: (){
setState((){
_selectedIndex = 1;
});
},
...
],
),
), //BottomAppBar
), //Scaffold
}
我知道如果我在里面有 HomeFeed 的代码,Scaffold.body
那么我可以只使用 aScrollcontroller
和animateTo
方法。问题是 Homefeed 是另一个有状态的小部件,即使在单击提要图标时调用了 setState,HomeFeed 小部件也不会重建。我尝试Scrollcontroller
在主页中定义 a 并将其传递给 HomeFeed,但它不起作用。
任何人都可以帮助我吗?
解决方案
您可以为小部件GlobalKey
的状态设置一个HomeFeed
。使用它GlobalKey
,您可以调用HomeFeed
小部件状态的功能。
主要代码:
GlobalKey<HomeFeedState> feedKey = new GlobalKey<HomeFeedState>(); // this is new
@override
Widget build(BuildContext context) {
return Scaffold(
body: [
HomeFeed(key: feedKey), // this is new
Page3(),
].elementAt(_selectedIndex),
bottomNavigationBar: BottomAppBar(
child: Row(
children: <Widget>[
IconButton(
icon: FaIcon(FontAwesomeIcons.houseUser),
onPressed: (){
if (_selectedIndex == 0) {
feedKey.currentState.jumpUp(); // this is new
} else {
setState(() {
_selectedIndex = 0;
});
}
},
),
IconButton(
icon: FaIcon(FontAwesomeIcons.compass),
onPressed: (){
setState(() {
_selectedIndex = 1;
});
},
),
],
),
), //BottomAppBar
);
}
主页:
class HomeFeed extends StatefulWidget {
final GlobalKey<HomeFeedState> key; // this is new
HomeFeed({this.key}) : super(key: key); // this is new
@override
HomeFeedState createState() => HomeFeedState();
}
class HomeFeedState extends State<HomeFeed> {
var _scrollController = new ScrollController();
jumpUp() { // this will be called when tapped on the home icon
_scrollController.animateTo(0,
duration: Duration(seconds: 2), curve: Curves.ease);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
shrinkWrap: true,
controller: _scrollController,
itemCount: 100,
itemBuilder: (context, index) {
return Container(
height: 300,
child: Card(
child: Center(
child: Text('$index'),
),
),
);
},
),
);
}
}
推荐阅读
- azure - 如何从工件 UI 下载通用工件
- ruby-on-rails - Rails 6 SQL 方法的语法
- shell - grep - 匹配前 n 行的字符串
- c# - 删除成绩单数据 C# 中的时间模式
- javascript - Javascript chrome 扩展在新标签页中不起作用
- python - 如何阻止熊猫按字母顺序对数据框中的列进行自动排序?
- ag-grid - 行详细信息面板
- reporting-services - SSRS 表达式,用于获取当前周周日至周六
- flutter - Flutter 有两个 SliverPersistentHeader 一个接一个堆叠
- html - 更新 VSCode html 制表符间距