Skip to content

Commit

Permalink
fix: reduce flickering due to FutureWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
singularity-s0 committed Jul 31, 2021
1 parent 4803e99 commit 65b469d
Showing 1 changed file with 67 additions and 40 deletions.
107 changes: 67 additions & 40 deletions lib/page/bus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class BusPage extends StatefulWidget {
class _BusPageState extends State<BusPage> {
Future<List<BusScheduleItem>> _busListWeekday;
Future<List<BusScheduleItem>> _busListHoliday;
List<BusScheduleItem> _busListWeekdayLoaded;
List<BusScheduleItem> _busListHolidayLoaded;

int _holidaySliding;

Expand All @@ -63,37 +65,72 @@ class _BusPageState extends State<BusPage> {
Future<List<BusScheduleItem>> _setContent() async {
List<BusScheduleItem> content;
if (_holidaySliding == 1) {
content = await _busListHoliday;
content = _busListHolidayLoaded = await _busListHoliday;
} else {
content = await _busListWeekday;
content = _busListWeekdayLoaded = await _busListWeekday;
}
// Normalize all entries
content.forEach((element) {
if (element.direction == BusDirection.BACKWARD) {
final start = element.start;
element.start = element.end;
element.end = start;
final startTime = element.startTime;
element.startTime = element.endTime;
element.endTime = startTime;
element.direction = BusDirection.FORWARD;
}
});
return _filterBus(content);
}

Widget _buildFutureWidget() => FutureWidget(
future: _setContent(),
successBuilder: (context, snapshot) {
return ListView(
physics: AlwaysScrollableScrollPhysics(),
primary: true,
children: _getListWidgets(snapshot.data),
);
},
errorBuilder: (context, snapshot) {
return Center(
child: GestureDetector(
child: Text(S.of(context).failed),
onTap: () => setState(() {}),
),
);
},
loadingBuilder: (context) {
return Center(
child: PlatformCircularProgressIndicator(),
);
});

Widget _autoSelectWidget() {
if (_holidaySliding == 1) {
if (_busListHolidayLoaded == null)
return _buildFutureWidget();
else {
return ListView(
physics: AlwaysScrollableScrollPhysics(),
primary: true,
children: _getListWidgets(_filterBus(_busListHolidayLoaded)),
);
}
} else {
if (_busListWeekdayLoaded == null)
return _buildFutureWidget();
else {
return ListView(
physics: AlwaysScrollableScrollPhysics(),
primary: true,
children: _getListWidgets(_filterBus(_busListWeekdayLoaded)),
);
}
}
}

@override
void initState() {
super.initState();
if (widget.arguments['dataIsHoliday']) {
_busListWeekday = FudanBusRepository.getInstance()
.loadBusList(StateProvider.personInfo.value, holiday: false);
_busListHoliday = Future.value(widget.arguments['busList']);
_busListHolidayLoaded = widget.arguments['busList'];
_holidaySliding = 1;
} else {
_busListHoliday = FudanBusRepository.getInstance()
.loadBusList(StateProvider.personInfo.value, holiday: true);
_busListWeekday = Future.value(widget.arguments['busList']);
_busListWeekdayLoaded = widget.arguments['busList'];
_holidaySliding = 0;
}

Expand Down Expand Up @@ -122,6 +159,18 @@ class _BusPageState extends State<BusPage> {
}

List<BusScheduleItem> _filterBus(List<BusScheduleItem> origBusList) {
// Normalize all entries
origBusList.forEach((element) {
if (element.direction == BusDirection.BACKWARD) {
final start = element.start;
element.start = element.end;
element.end = start;
final startTime = element.startTime;
element.startTime = element.endTime;
element.endTime = startTime;
element.direction = BusDirection.FORWARD;
}
});
return origBusList
.where((element) =>
(element.start == _startSelectItem &&
Expand Down Expand Up @@ -226,30 +275,8 @@ class _BusPageState extends State<BusPage> {
),
Expanded(
child: WithScrollbar(
controller: PrimaryScrollController.of(context),
child: FutureWidget(
future: _setContent(),
successBuilder: (context, snapshot) {
return ListView(
physics: AlwaysScrollableScrollPhysics(),
primary: true,
children: _getListWidgets(snapshot.data),
);
},
errorBuilder: (context, snapshot) {
return Center(
child: GestureDetector(
child: Text(S.of(context).failed),
onTap: () => setState(() {}),
),
);
},
loadingBuilder: (context) {
return Center(
child: PlatformCircularProgressIndicator(),
);
}),
),
controller: PrimaryScrollController.of(context),
child: _autoSelectWidget()),
),
],
),
Expand Down

0 comments on commit 65b469d

Please sign in to comment.