Skip to content

Commit 23cce53

Browse files
authored
Merge pull request #41 from andreped:fix-viz-tab
Preserves state of visualization state between tab changes
2 parents 9ced0a9 + 986959d commit 23cce53

File tree

2 files changed

+89
-79
lines changed

2 files changed

+89
-79
lines changed

lib/tabs/home.dart

+82-78
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class _ExerciseStoreHomePageState extends State<ExerciseStoreHomePage> with Sing
2828
DateTime _selectedDay = DateTime.now();
2929
late TabController _tabController;
3030
final PageController _pageController = PageController();
31+
final PageStorageBucket bucket = PageStorageBucket();
3132

3233
@override
3334
void initState() {
@@ -108,88 +109,91 @@ class _ExerciseStoreHomePageState extends State<ExerciseStoreHomePage> with Sing
108109
],
109110
),
110111
),
111-
body: PageView(
112-
controller: _pageController,
113-
physics: const NeverScrollableScrollPhysics(), // Disable swipe gesture
114-
onPageChanged: (index) {
115-
_tabController.animateTo(index);
116-
},
117-
children: [
118-
// Log Exercise Tab
119-
Padding(
120-
padding: const EdgeInsets.all(16.0),
121-
child: ExerciseSetter(
122-
onExerciseAdded: () {
123-
setState(() {});
124-
},
112+
body: PageStorage(
113+
bucket: bucket,
114+
child: PageView(
115+
controller: _pageController,
116+
physics: const NeverScrollableScrollPhysics(), // Disable swipe gesture
117+
onPageChanged: (index) {
118+
_tabController.animateTo(index);
119+
},
120+
children: [
121+
// Log Exercise Tab
122+
Padding(
123+
padding: const EdgeInsets.all(16.0),
124+
child: ExerciseSetter(
125+
onExerciseAdded: () {
126+
setState(() {});
127+
},
128+
),
125129
),
126-
),
127-
// Summary Tab
128-
SummaryTab(
129-
selectedDay: _selectedDay,
130-
onDateSelected: _onDateSelected,
131-
),
132-
// Visualize Data Tab
133-
Padding(
134-
padding: const EdgeInsets.all(16.0),
135-
child: VisualizationTab(),
136-
),
137-
// View Table Tab
138-
SingleChildScrollView(
139-
scrollDirection: Axis.vertical,
140-
child: SingleChildScrollView(
141-
scrollDirection: Axis.horizontal,
142-
child: FutureBuilder<List<Map<String, dynamic>>>(
143-
future: _getExercises(),
144-
builder: (context, snapshot) {
145-
if (!snapshot.hasData) {
146-
return const Center(child: CircularProgressIndicator());
147-
}
148-
final exercises = snapshot.data!;
149-
return DataTable(
150-
columns: const [
151-
DataColumn(label: Text('ID')),
152-
DataColumn(label: Text('Exercise')),
153-
DataColumn(label: Text('Weight')),
154-
DataColumn(label: Text('Reps')),
155-
DataColumn(label: Text('Sets')),
156-
DataColumn(label: Text('Timestamp')),
157-
DataColumn(label: Text('Actions')),
158-
],
159-
rows: exercises.map((exercise) {
160-
return DataRow(cells: [
161-
DataCell(Text(exercise['id'].toString())),
162-
DataCell(Text(exercise['exercise'])),
163-
DataCell(Text(exercise['weight'])),
164-
DataCell(Text(exercise['reps'].toString())),
165-
DataCell(Text(exercise['sets'].toString())),
166-
DataCell(Text(exercise['timestamp'])),
167-
DataCell(
168-
Row(
169-
children: [
170-
IconButton(
171-
icon: const Icon(Icons.edit),
172-
onPressed: () {
173-
_showEditDialog(exercise);
174-
},
175-
),
176-
IconButton(
177-
icon: const Icon(Icons.delete),
178-
onPressed: () async {
179-
await _deleteExercise(exercise['id']);
180-
},
181-
),
182-
],
130+
// Summary Tab
131+
SummaryTab(
132+
selectedDay: _selectedDay,
133+
onDateSelected: _onDateSelected,
134+
),
135+
// Visualize Data Tab
136+
Padding(
137+
padding: const EdgeInsets.all(16.0),
138+
child: VisualizationTab(key: PageStorageKey('visualizationTab')),
139+
),
140+
// View Table Tab
141+
SingleChildScrollView(
142+
scrollDirection: Axis.vertical,
143+
child: SingleChildScrollView(
144+
scrollDirection: Axis.horizontal,
145+
child: FutureBuilder<List<Map<String, dynamic>>>(
146+
future: _getExercises(),
147+
builder: (context, snapshot) {
148+
if (!snapshot.hasData) {
149+
return const Center(child: CircularProgressIndicator());
150+
}
151+
final exercises = snapshot.data!;
152+
return DataTable(
153+
columns: const [
154+
DataColumn(label: Text('ID')),
155+
DataColumn(label: Text('Exercise')),
156+
DataColumn(label: Text('Weight')),
157+
DataColumn(label: Text('Reps')),
158+
DataColumn(label: Text('Sets')),
159+
DataColumn(label: Text('Timestamp')),
160+
DataColumn(label: Text('Actions')),
161+
],
162+
rows: exercises.map((exercise) {
163+
return DataRow(cells: [
164+
DataCell(Text(exercise['id'].toString())),
165+
DataCell(Text(exercise['exercise'])),
166+
DataCell(Text(exercise['weight'])),
167+
DataCell(Text(exercise['reps'].toString())),
168+
DataCell(Text(exercise['sets'].toString())),
169+
DataCell(Text(exercise['timestamp'])),
170+
DataCell(
171+
Row(
172+
children: [
173+
IconButton(
174+
icon: const Icon(Icons.edit),
175+
onPressed: () {
176+
_showEditDialog(exercise);
177+
},
178+
),
179+
IconButton(
180+
icon: const Icon(Icons.delete),
181+
onPressed: () async {
182+
await _deleteExercise(exercise['id']);
183+
},
184+
),
185+
],
186+
),
183187
),
184-
),
185-
]);
186-
}).toList(),
187-
);
188-
},
188+
]);
189+
}).toList(),
190+
);
191+
},
192+
),
189193
),
190194
),
191-
),
192-
],
195+
],
196+
),
193197
),
194198
);
195199
}

lib/tabs/visualization.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import 'package:fl_chart/fl_chart.dart';
33
import '../core/database.dart';
44

55
class VisualizationTab extends StatefulWidget {
6+
const VisualizationTab({Key? key}) : super(key: key);
7+
68
@override
79
_VisualizationTabState createState() => _VisualizationTabState();
810
}
911

10-
class _VisualizationTabState extends State<VisualizationTab> {
12+
class _VisualizationTabState extends State<VisualizationTab> with AutomaticKeepAliveClientMixin {
1113
String? _selectedExercise;
1214
List<String> _exerciseNames = [];
1315
List<ScatterSpot> _dataPoints = [];
@@ -53,6 +55,7 @@ class _VisualizationTabState extends State<VisualizationTab> {
5355

5456
@override
5557
Widget build(BuildContext context) {
58+
super.build(context); // Required by AutomaticKeepAliveClientMixin
5659
return Padding(
5760
padding: const EdgeInsets.all(16.0),
5861
child: Column(
@@ -150,4 +153,7 @@ class _VisualizationTabState extends State<VisualizationTab> {
150153
),
151154
);
152155
}
156+
157+
@override
158+
bool get wantKeepAlive => true; // Required by AutomaticKeepAliveClientMixin
153159
}

0 commit comments

Comments
 (0)