flutter 中的一个控制器两个小部件
是否有任何选项可以为两个小部件保留一个控制器?我有 3 个按钮来更改视图,其中两个使用相同的分页控制器,一切正常,直到切换到第三个视图。然后,返回第一或第二视图后,控制器停止工作。
class ViewsScreen extends StatefulWidget {
const ViewsScreen({Key? key}) : super(key: key);
@override
State<ViewsScreen> createState() => _ViewsScreenState();
}
class _ViewsScreenState extends State<ViewsScreen> {
int _view = 1;
final _pagingController = PagingController<int, Item>(
firstPageKey: 1,
);
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
width: double.infinity,
height: 40,
child: Row(
children: [
_pageButton(Icons.one_k, 'view1', 1),
_pageButton(Icons.two_k, 'view2', 2),
_pageButton(Icons.three_k, 'view3', 3),
],
),
),
Expanded(
child: _view == 1
? View12(
view: 1,
controller: _pagingController,
)
: _view == 2
? View12(
view: 2,
controller: _pagingController,
)
: View3()),
],
),
);
}
Widget _pageButton(IconData icon, String name, int view) {
return Expanded(
child: InkWell(
onTap: () {
setState(() {
_view = view;
});
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(icon),
Text(name),
],
),
),
);
}
}
View12仅根据视图参数改变列表的显示,但使用相同的控制器。
class View12 extends StatefulWidget {
final int view;
final PagingController<int, Item> controller;
const View12(
{Key? key, required this.view, required this.controller})
: super(key: key);
@override
_View12ViewState createState() => _View12ViewState();
}
class _View12ViewState extends State<View12> {
@override
void initState() {
widget.controller.addPageRequestListener((pageKey) {
//fetching data from service
context.read<ViewModel>().fetchPage(pageKey, widget.controller);
});
super.initState();
}
@override
Widget build(BuildContext context) {
return RefreshIndicator(
onRefresh: () => Future.sync(
() => widget.controller.refresh(),
),
child: PagedListView.separated(
pagingController: widget.controller,
separatorBuilder: (context, index) =>
Container(height: 1, color: Colors.black),
builderDelegate: PagedChildBuilderDelegate<Item>(
itemBuilder: (context, item, index) {
if (widget.view == 1)
return ItemGallery(
item: item,
);
if (widget.view == 2)
return ItemList(
item: item,
);
return Container();
},
),
),
);
}
}
获取方法
Future<void> fetchPage(
int pageKey, PagingController<int, Offer> controller) async {
try {
final newItems = await loadData(pageKey);
if (isLastPage) {
controller.appendLastPage(newItems);
} else {
final nextPageKey = pageKey + 1;
controller.appendPage(newItems, nextPageKey);
}
} catch (error) {
controller.error = error;
}
}
从view3返回到view12后错误描述:
is there any option to keep one controller for two widgets? I have 3 buttons to change view, two of them use the same paging controller, everything works fine until it switches to the third view. Then, after back to the first or second view, the controller stop working.
class ViewsScreen extends StatefulWidget {
const ViewsScreen({Key? key}) : super(key: key);
@override
State<ViewsScreen> createState() => _ViewsScreenState();
}
class _ViewsScreenState extends State<ViewsScreen> {
int _view = 1;
final _pagingController = PagingController<int, Item>(
firstPageKey: 1,
);
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
width: double.infinity,
height: 40,
child: Row(
children: [
_pageButton(Icons.one_k, 'view1', 1),
_pageButton(Icons.two_k, 'view2', 2),
_pageButton(Icons.three_k, 'view3', 3),
],
),
),
Expanded(
child: _view == 1
? View12(
view: 1,
controller: _pagingController,
)
: _view == 2
? View12(
view: 2,
controller: _pagingController,
)
: View3()),
],
),
);
}
Widget _pageButton(IconData icon, String name, int view) {
return Expanded(
child: InkWell(
onTap: () {
setState(() {
_view = view;
});
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(icon),
Text(name),
],
),
),
);
}
}
View12 only changes the display of the list depending on the view parameter, but uses the same controller.
class View12 extends StatefulWidget {
final int view;
final PagingController<int, Item> controller;
const View12(
{Key? key, required this.view, required this.controller})
: super(key: key);
@override
_View12ViewState createState() => _View12ViewState();
}
class _View12ViewState extends State<View12> {
@override
void initState() {
widget.controller.addPageRequestListener((pageKey) {
//fetching data from service
context.read<ViewModel>().fetchPage(pageKey, widget.controller);
});
super.initState();
}
@override
Widget build(BuildContext context) {
return RefreshIndicator(
onRefresh: () => Future.sync(
() => widget.controller.refresh(),
),
child: PagedListView.separated(
pagingController: widget.controller,
separatorBuilder: (context, index) =>
Container(height: 1, color: Colors.black),
builderDelegate: PagedChildBuilderDelegate<Item>(
itemBuilder: (context, item, index) {
if (widget.view == 1)
return ItemGallery(
item: item,
);
if (widget.view == 2)
return ItemList(
item: item,
);
return Container();
},
),
),
);
}
}
Fetching method
Future<void> fetchPage(
int pageKey, PagingController<int, Offer> controller) async {
try {
final newItems = await loadData(pageKey);
if (isLastPage) {
controller.appendLastPage(newItems);
} else {
final nextPageKey = pageKey + 1;
controller.appendPage(newItems, nextPageKey);
}
} catch (error) {
controller.error = error;
}
}
Error description after returning from view3 to view12:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样使用它,
Use it like this,
}
方法
像这样改变你的获取
}
}
change your fetching method like this