Skip to content

Commit

Permalink
Refactor PreDefinedExercisesDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
brylie committed Jul 27, 2024
1 parent 3cc0835 commit 4f8005b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 48 deletions.
53 changes: 5 additions & 48 deletions random_workout/lib/pages/workout_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:random_workout/widgets/exercise/add_exercise_options_dialog.dart';
import 'package:random_workout/widgets/exercise/predefined_exercises_dialog.dart';
import '../models/exercise.dart';
import '../providers/app_state.dart';
import '../data/all_exercises.dart';
Expand Down Expand Up @@ -85,54 +86,10 @@ class WorkoutPage extends StatelessWidget {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Choose an Exercise'),
content: SizedBox(
width: double.maxFinite,
child: availableExercises.isEmpty
? const Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'No more pre-defined exercises available. Try creating your own custom exercise!',
style: TextStyle(fontStyle: FontStyle.italic),
),
)
: ScrollbarTheme(
data: ScrollbarThemeData(
thumbVisibility: WidgetStateProperty.all(true),
),
child: ListView.builder(
itemCount: availableExercises.length,
itemBuilder: (context, index) {
final exercise = availableExercises[index];
return ListTile(
title: Text(exercise.name),
subtitle: Text(exercise.description),
onTap: () {
appState.addExercise(exercise);
Navigator.of(context).pop();
},
);
},
),
),
),
actions: <Widget>[
TextButton(
child: const Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
if (availableExercises.isEmpty)
TextButton(
child: const Text('Create Custom Exercise'),
onPressed: () {
Navigator.of(context).pop();
_showCreateExerciseDialog(context);
},
),
],
return PreDefinedExercisesDialog(
availableExercises: availableExercises,
category: category,
onCreateCustomExercise: () => _showCreateExerciseDialog(context),
);
},
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../models/exercise.dart';
import '../../providers/app_state.dart';

class PreDefinedExercisesDialog extends StatelessWidget {
final List<Exercise> availableExercises;
final ExerciseCategory category;
final VoidCallback onCreateCustomExercise;

const PreDefinedExercisesDialog({
Key? key,
required this.availableExercises,
required this.category,
required this.onCreateCustomExercise,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final appState = Provider.of<AppState>(context, listen: false);

return AlertDialog(
title: const Text('Choose an Exercise'),
content: SizedBox(
width: double.maxFinite,
child: availableExercises.isEmpty
? const Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'No more pre-defined exercises available. Try creating your own custom exercise!',
style: TextStyle(fontStyle: FontStyle.italic),
),
)
: ScrollbarTheme(
data: ScrollbarThemeData(
thumbVisibility: MaterialStateProperty.all(true),
),
child: ListView.builder(
itemCount: availableExercises.length,
itemBuilder: (context, index) {
final exercise = availableExercises[index];
return ListTile(
title: Text(exercise.name),
subtitle: Text(exercise.description),
onTap: () {
appState.addExercise(exercise);
Navigator.of(context).pop();
},
);
},
),
),
),
actions: <Widget>[
TextButton(
child: const Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
if (availableExercises.isEmpty)
TextButton(
child: const Text('Create Custom Exercise'),
onPressed: () {
Navigator.of(context).pop();
onCreateCustomExercise();
},
),
],
);
}
}

0 comments on commit 4f8005b

Please sign in to comment.