附加到多个滚动视图的页面视图控制器
Page view controller attached to multiple scroll view
我的页面视图有问题,它在第一次初始化时工作正常(我的意思是当从“屏幕 A”打开包含页面视图的屏幕时,然后从另一个名为“屏幕”的屏幕打开“pageView 屏幕” B") 第二次遇到这个问题
我认为这个问题的发生是因为控制器再次重新初始化,但我处理了控制器并在处理屏幕时使其等于 null
ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
package:flutter/…/widgets/scroll_controller.dart:1
Failed assertion: line 109 pos 12: '_positions.length == 1'
使用provider 来处理tabBar 和Page View 的变化,provider 下面的代码class
class PageViewProvider extends BaseViewModel {
// Tabs Controller
int selectedTapIndex = 0;
PageController? pageViewController;
initPageIndex(int index) {
if (pageViewController != null) {
pageViewController = null;
}
print('init index $index');
selectedTapIndex = index;
pageViewController = PageController(initialPage: index, keepPage: false);
}
void changeSelectedTapIndex(int index) {
selectedTapIndex = index;
setState(ViewState.Idle);
}
void changePageView(int index) {
pageViewController!.jumpToPage(index);
}
}
这是带有 onTap 功能的选项卡项小部件,用于更改 TabBar 的索引
Consumer<PageViewProvider>(
builder: (context, pageViewProvider, _) => Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: Column(
children: [
Icon(
iconData,
color:
pageViewProvider.selectedTapIndex == selectedIndex
? purpleColor
: grey500,
),
normal14Text(
title,
color:
pageViewProvider.selectedTapIndex == selectedIndex
? purpleColor
: grey500,
),
],
),
).onTap(() {
pageViewProvider.changeSelectedTapIndex(selectedIndex!);
pageViewProvider.changePageView(selectedIndex!);
}));
至少,这是 pageView body
Consumer<PageViewProvider>(
builder: (context, pageViewProvider, _) => PageView(
controller: pageViewProvider.pageViewController,
onPageChanged: (index) {
pageViewProvider.changeSelectedTapIndex(index);
},
pageSnapping: true,
children: [
LeadProfileContent(),
LeadProfileTimeLine(),
LeadProfileCases(),
LeadProfileDeals(),
],
),
),
提前致谢。
解决了。这里的问题与页面视图正文上方的消费者有关。刚刚删除它,然后像这样访问pageViewController:
context.read<PageViewProvider>.pageViewController
我的页面视图有问题,它在第一次初始化时工作正常(我的意思是当从“屏幕 A”打开包含页面视图的屏幕时,然后从另一个名为“屏幕”的屏幕打开“pageView 屏幕” B") 第二次遇到这个问题
我认为这个问题的发生是因为控制器再次重新初始化,但我处理了控制器并在处理屏幕时使其等于 null
ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
package:flutter/…/widgets/scroll_controller.dart:1
Failed assertion: line 109 pos 12: '_positions.length == 1'
使用provider 来处理tabBar 和Page View 的变化,provider 下面的代码class
class PageViewProvider extends BaseViewModel {
// Tabs Controller
int selectedTapIndex = 0;
PageController? pageViewController;
initPageIndex(int index) {
if (pageViewController != null) {
pageViewController = null;
}
print('init index $index');
selectedTapIndex = index;
pageViewController = PageController(initialPage: index, keepPage: false);
}
void changeSelectedTapIndex(int index) {
selectedTapIndex = index;
setState(ViewState.Idle);
}
void changePageView(int index) {
pageViewController!.jumpToPage(index);
}
}
这是带有 onTap 功能的选项卡项小部件,用于更改 TabBar 的索引
Consumer<PageViewProvider>(
builder: (context, pageViewProvider, _) => Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: Column(
children: [
Icon(
iconData,
color:
pageViewProvider.selectedTapIndex == selectedIndex
? purpleColor
: grey500,
),
normal14Text(
title,
color:
pageViewProvider.selectedTapIndex == selectedIndex
? purpleColor
: grey500,
),
],
),
).onTap(() {
pageViewProvider.changeSelectedTapIndex(selectedIndex!);
pageViewProvider.changePageView(selectedIndex!);
}));
至少,这是 pageView body
Consumer<PageViewProvider>(
builder: (context, pageViewProvider, _) => PageView(
controller: pageViewProvider.pageViewController,
onPageChanged: (index) {
pageViewProvider.changeSelectedTapIndex(index);
},
pageSnapping: true,
children: [
LeadProfileContent(),
LeadProfileTimeLine(),
LeadProfileCases(),
LeadProfileDeals(),
],
),
),
提前致谢。
解决了。这里的问题与页面视图正文上方的消费者有关。刚刚删除它,然后像这样访问pageViewController:
context.read<PageViewProvider>.pageViewController