如何强制pageView刷新更改页面?
我已经实现了pageView,并且在一个方案中,我希望在更改此页面时刷新页面。
如何实现这一目标?
我的页面是我的页面浏览量课。
这是我的代码,
child: PageView(
physics: const NeverScrollableScrollPhysics(
parent: BouncingScrollPhysics()),
onPageChanged: (page) {
setState(() {
currentIndex = page;
});
},
scrollDirection: Axis.horizontal,
controller: pageController,
children: widget.noAccount == null
? [
const NoConnectionWidgetHandler(
child: PartyPage()),
const NoConnectionWidgetHandler(
child: ClubsPage()),
const QrCamera(),
const NoConnectionWidgetHandler(
child: FriendsPage()),
const NoConnectionWidgetHandler(
child: ProfilePage()),
]
void changePage(int page, BuildContext context) async {
FocusScope.of(context).unfocus();
if (page == 2) {
Provider.of<QrProvider>(context, listen: false)
.changeCameraVisibility(true);
} else if (Provider.of<QrProvider>(context, listen: false).isQrCameraOn) {
Provider.of<QrProvider>(context, listen: false)
.changeCameraVisibility(false);
}
print('previous $currentIndex');
if (currentIndex == 0) {
print('wysyłam statystyki o kliknietych eventach');
var stats = await MySecureStorage().readData('stats_event');
print('arr: $stats');
if (stats != null) {
// send data
}
await MySecureStorage().deleteData('stats_event');
}
else if (currentIndex == 1) {
print('wysyłam statystyki o kliknietych partyspotach');
var stats = await MySecureStorage().readData('stats_partyspots');
print('arr: $stats');
if (stats != null) {
// send data
}
await MySecureStorage().deleteData('stats_partyspots');
}
if (currentIndex + 1 == page || currentIndex - 1 == page) {
currentIndex = page;
pageController.animateToPage(page,
duration: const Duration(milliseconds: 300), curve: Curves.ease);
} else {
currentIndex = page;
pageController.jumpToPage(page);
}
print('current $currentIndex');
}
这是我的Partypage类,其状态小部件,这是我要刷新的部分:
child: Consumer<ControllerProvider>(
builder: (context, shower, child) {
return FutureBuilder<List>(
future: _loadingList,
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot.connectionState);
if (snapshot.hasData && snapshot.data.length >= 1) {
return LoadingIfNotSatisfied(
rebuildIfChanged: Provider.of<MainProvider>(
context,
listen: false,
).isConnectedToInternet,
isSatisfied: snapshot.data.isNotEmpty,
child: LazyLoadScrollView(
isLoading: isListReloading,
onEndOfPage: () {
//TODO uncomment after pagination on events is fixed
pageNumber += 1;
setState(() {
_loadingList = listLoader(pageNumber, true);
});
},
scrollOffset: (size.height * 3).toInt(),
child: isListLoading
? Center(
child: SpinKitSquareCircle(
duration: const Duration(seconds: 1),
itemBuilder: (context, index) {
return DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: const LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.topRight,
colors: [
Color(0xffff7a00),
Color(0xffff00d6)
],
)),
);
},
))
: ListViewBuilder(
dataList:
//TODO remove sublist
(snapshot.data as List),
isParty: true,
scrollController: scrollController,
onClick: (details) async {
//TODO wysyłać do backendu informację o tym, że ten klub został odwiedzony - kwestia statystyk - konkretnie odwiedzin danego klubu
print ('details: $details');
var stats = await storage.readData('stats_event');
if (stats == null) {
array.clear();
}
if (!array.contains(details['id'])) {
array.add(details['id']);
}
storage.writeData('stats_event', array.toString());
print(array);
Provider.of<ControllerProvider>(context, listen: false)
.changeFollowOnList(changeFollow);
Provider.of<ControllerProvider>(context, listen: false)
.loadPartyMap(details);
//TODO fix 30 minutes/ reatach controller..
if (shower.controller != null) {
Provider.of<ControllerProvider>(context, listen: false)
.moveMap(LatLng(
double.parse(details['lat']) +
0.004,
double.parse(details['lng'])));
Provider.of<ControllerProvider>(context, listen: false)
.loadMarker(
Marker(
markerId: const MarkerId('1'),
position: LatLng(
double.parse(details['lat']),
double.parse(details['lng']),
),
),
);
}
Provider.of<ControllerProvider>(context, listen: false).show();
},
refresher: () async {
requestedList = [];
pageNumber = 1;
final loader = listLoader(1, true);
setState(() {
_loadingList = loader;
});
await loader!.whenComplete(() {});
},
size: size),
),
);
} else if (snapshot.hasData &&
snapshot.connectionState != ConnectionState.waiting) {
return Center(
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: size.width * 0.05),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AutoSizeText(
local.searchNotFoundEvents,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25),
),
Theme(
data: ThemeData(),
child: TextButton(
onPressed: () {
showFilter();
},
child: AutoSizeText(
local.backToFilter,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25)
),
),
)
],
),
)
);
} else {
return Center(
child: SpinKitSquareCircle(
duration: const Duration(seconds: 1),
itemBuilder: (context, index) {
return DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: const LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.topRight,
colors: [
Color(0xffff7a00),
Color(0xffff00d6)
],
)),
);
},
));
}
},
);
}),
我知道此代码有些混乱,但是我是初级开发人员,这不是我的代码,我正在尝试逐步清洁它,在这个项目中与此合作很多
I have implemented PageView and I want in one scenario to refresh page when I change to this page.
How to achieve this?
My pages are another classes than my PageView class.
Here is my code
child: PageView(
physics: const NeverScrollableScrollPhysics(
parent: BouncingScrollPhysics()),
onPageChanged: (page) {
setState(() {
currentIndex = page;
});
},
scrollDirection: Axis.horizontal,
controller: pageController,
children: widget.noAccount == null
? [
const NoConnectionWidgetHandler(
child: PartyPage()),
const NoConnectionWidgetHandler(
child: ClubsPage()),
const QrCamera(),
const NoConnectionWidgetHandler(
child: FriendsPage()),
const NoConnectionWidgetHandler(
child: ProfilePage()),
]
void changePage(int page, BuildContext context) async {
FocusScope.of(context).unfocus();
if (page == 2) {
Provider.of<QrProvider>(context, listen: false)
.changeCameraVisibility(true);
} else if (Provider.of<QrProvider>(context, listen: false).isQrCameraOn) {
Provider.of<QrProvider>(context, listen: false)
.changeCameraVisibility(false);
}
print('previous $currentIndex');
if (currentIndex == 0) {
print('wysyłam statystyki o kliknietych eventach');
var stats = await MySecureStorage().readData('stats_event');
print('arr: $stats');
if (stats != null) {
// send data
}
await MySecureStorage().deleteData('stats_event');
}
else if (currentIndex == 1) {
print('wysyłam statystyki o kliknietych partyspotach');
var stats = await MySecureStorage().readData('stats_partyspots');
print('arr: $stats');
if (stats != null) {
// send data
}
await MySecureStorage().deleteData('stats_partyspots');
}
if (currentIndex + 1 == page || currentIndex - 1 == page) {
currentIndex = page;
pageController.animateToPage(page,
duration: const Duration(milliseconds: 300), curve: Curves.ease);
} else {
currentIndex = page;
pageController.jumpToPage(page);
}
print('current $currentIndex');
}
Here is my part of PartyPage class, its stateful widget, and here is part I want to refresh:
child: Consumer<ControllerProvider>(
builder: (context, shower, child) {
return FutureBuilder<List>(
future: _loadingList,
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot.connectionState);
if (snapshot.hasData && snapshot.data.length >= 1) {
return LoadingIfNotSatisfied(
rebuildIfChanged: Provider.of<MainProvider>(
context,
listen: false,
).isConnectedToInternet,
isSatisfied: snapshot.data.isNotEmpty,
child: LazyLoadScrollView(
isLoading: isListReloading,
onEndOfPage: () {
//TODO uncomment after pagination on events is fixed
pageNumber += 1;
setState(() {
_loadingList = listLoader(pageNumber, true);
});
},
scrollOffset: (size.height * 3).toInt(),
child: isListLoading
? Center(
child: SpinKitSquareCircle(
duration: const Duration(seconds: 1),
itemBuilder: (context, index) {
return DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: const LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.topRight,
colors: [
Color(0xffff7a00),
Color(0xffff00d6)
],
)),
);
},
))
: ListViewBuilder(
dataList:
//TODO remove sublist
(snapshot.data as List),
isParty: true,
scrollController: scrollController,
onClick: (details) async {
//TODO wysyłać do backendu informację o tym, że ten klub został odwiedzony - kwestia statystyk - konkretnie odwiedzin danego klubu
print ('details: $details');
var stats = await storage.readData('stats_event');
if (stats == null) {
array.clear();
}
if (!array.contains(details['id'])) {
array.add(details['id']);
}
storage.writeData('stats_event', array.toString());
print(array);
Provider.of<ControllerProvider>(context, listen: false)
.changeFollowOnList(changeFollow);
Provider.of<ControllerProvider>(context, listen: false)
.loadPartyMap(details);
//TODO fix 30 minutes/ reatach controller..
if (shower.controller != null) {
Provider.of<ControllerProvider>(context, listen: false)
.moveMap(LatLng(
double.parse(details['lat']) +
0.004,
double.parse(details['lng'])));
Provider.of<ControllerProvider>(context, listen: false)
.loadMarker(
Marker(
markerId: const MarkerId('1'),
position: LatLng(
double.parse(details['lat']),
double.parse(details['lng']),
),
),
);
}
Provider.of<ControllerProvider>(context, listen: false).show();
},
refresher: () async {
requestedList = [];
pageNumber = 1;
final loader = listLoader(1, true);
setState(() {
_loadingList = loader;
});
await loader!.whenComplete(() {});
},
size: size),
),
);
} else if (snapshot.hasData &&
snapshot.connectionState != ConnectionState.waiting) {
return Center(
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: size.width * 0.05),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AutoSizeText(
local.searchNotFoundEvents,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25),
),
Theme(
data: ThemeData(),
child: TextButton(
onPressed: () {
showFilter();
},
child: AutoSizeText(
local.backToFilter,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25)
),
),
)
],
),
)
);
} else {
return Center(
child: SpinKitSquareCircle(
duration: const Duration(seconds: 1),
itemBuilder: (context, index) {
return DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: const LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.topRight,
colors: [
Color(0xffff7a00),
Color(0xffff00d6)
],
)),
);
},
));
}
},
);
}),
I know this code a little bit messy, but I am junior developer and it is not my code, I am trying to clean it step by step and it's a lot of work with this in this project
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论