rsflutterapp

Bahnhofsfoto-App for Android and iOS using Flutter
git clone git://archive.git.mtrnord.blog/MTRNord/rsflutterapp.git
Log | Files | Refs | README

commit d691d8f73f27ee8d8d20fe46c65f911a09512869
parent 31c13956455a68e1df6f8fadba3b4c40bf891ec9
Author: Marcel <mtrnord1@gmail.com>
Date:   Sun, 23 Feb 2020 20:35:32 +0100

Prevent reloading on each tab switch

Took 10 minutes

Diffstat:
Mlib/views/HomePage.dart | 2+-
Mlib/views/tabs/RankingsTab.dart | 23++++++++++++++++++++---
Mlib/views/tabs/StationsTab.dart | 24++++++++++++++++++++----
3 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/lib/views/HomePage.dart b/lib/views/HomePage.dart @@ -23,7 +23,7 @@ class HomePage extends StatelessWidget { return DefaultTabController( length: 4, - initialIndex: 3, + initialIndex: 2, child: Scaffold( bottomNavigationBar: Container( color: Theme.of(context).primaryColor, diff --git a/lib/views/tabs/RankingsTab.dart b/lib/views/tabs/RankingsTab.dart @@ -1,20 +1,34 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; + import '../../repos/repositories.dart'; -class RankingsTab extends StatelessWidget { +class RankingsTab extends StatefulWidget { final RailwayStationsRepository railwayStationsRepository; RankingsTab({@required this.railwayStationsRepository}) : assert(railwayStationsRepository != null); - Future<List<List<String>>> getScores() { - return this.railwayStationsRepository.getScores(); + @override + RankingsTabState createState() => RankingsTabState(); +} + +class RankingsTabState extends State<RankingsTab> + with AutomaticKeepAliveClientMixin<RankingsTab> { + List<List<String>> scores; + + Future<List<List<String>>> getScores() async { + if (scores == null) { + scores = await widget.railwayStationsRepository.getScores(); + } + return scores; } @override Widget build(BuildContext context) { + super.build(context); + return FutureBuilder( future: getScores(), builder: @@ -98,4 +112,7 @@ class RankingsTab extends StatelessWidget { }, ); } + + @override + bool get wantKeepAlive => true; } diff --git a/lib/views/tabs/StationsTab.dart b/lib/views/tabs/StationsTab.dart @@ -5,18 +5,31 @@ import '../../models/models.dart'; import '../../repos/repositories.dart'; import '../DetailsPage.dart'; -class StationsTab extends StatelessWidget { +class StationsTab extends StatefulWidget { final RailwayStationsRepository railwayStationsRepository; StationsTab({@required this.railwayStationsRepository}) : assert(railwayStationsRepository != null); - Future<List<Station>> getStations() { - return this.railwayStationsRepository.getStations(); + @override + StationsTabState createState() => StationsTabState(); +} + +class StationsTabState extends State<StationsTab> + with AutomaticKeepAliveClientMixin<StationsTab> { + List<Station> stations; + + Future<List<Station>> getStations() async { + if (stations == null) { + stations = await widget.railwayStationsRepository.getStations(); + } + return stations; } @override Widget build(BuildContext context) { + super.build(context); + return FutureBuilder( future: getStations(), builder: (BuildContext context, AsyncSnapshot<List<Station>> snapshot) { @@ -49,7 +62,7 @@ class StationsTab extends StatelessWidget { builder: (BuildContext context) => DetailsPage( station: stations[index], railwayStationsRepository: - railwayStationsRepository, + widget.railwayStationsRepository, ), ), ); @@ -68,4 +81,7 @@ class StationsTab extends StatelessWidget { }, ); } + + @override + bool get wantKeepAlive => true; }