Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Speed Indicator #99

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 71 additions & 21 deletions src/lib/treadmill_control/widgets/controls/speed_indicator.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fitness_machine/treadmill_control/models/speed_state.dart';
import 'package:fitness_machine/treadmill_control/widgets/cubits/speed_range_and_setting_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class SpeedIndicator extends StatelessWidget {
const SpeedIndicator({super.key});
Expand All @@ -16,29 +16,79 @@ class SpeedIndicator extends StatelessWidget {
return Card(
child: Row(
children: [
Padding(
padding: const EdgeInsets.all(8),
child: ElevatedButton(onPressed: () async => cubit.speedDown(), child: const Icon(Icons.arrow_downward))),
_SpeedIndicatorButton(
onPressed: () async => cubit.speedDown(),
icon: const Icon(Icons.remove)),
Expanded(
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Row(children: [
Align(alignment: Alignment.centerLeft, child: Text(state.minSpeed.toStringAsFixed(1))),
Expanded(child: Align(alignment: Alignment.center, child: Text(state.speedInKmh.toStringAsFixed(1)))),
Align(alignment: Alignment.centerRight, child: Text(state.maxSpeed.toStringAsFixed(1)))
]),
Row(children: [
Expanded(
child: LinearProgressIndicator(
value: state.maxSpeed > 0 ? (state.speedInKmh - state.minSpeed) / (state.maxSpeed - state.minSpeed) : 0))
]),
])),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(onPressed: () async => cubit.speedUp(), child: const Icon(Icons.arrow_upward)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(children: [
Expanded(
child: Align(
alignment: Alignment.center,
child: Text(
state.speedInKmh.toStringAsFixed(1),
style:
Theme.of(context).textTheme.displayLarge,
))),
]),
Row(children: [
Expanded(
child: LinearProgressIndicator(
value: state.maxSpeed > 0
? (state.speedInKmh - state.minSpeed) /
(state.maxSpeed - state.minSpeed)
: 0))
]),
Row(
children: [
Align(
alignment: Alignment.centerLeft,
child: Text(state.minSpeed.toStringAsFixed(1))),
const Spacer(),
Align(
alignment: Alignment.centerRight,
child: Text(state.maxSpeed.toStringAsFixed(1))),
],
),
Align(
alignment: Alignment.center,
child: Text("Speed (km/h)",
style: Theme.of(context).textTheme.labelMedium)),
])),
_SpeedIndicatorButton(
onPressed: () async => cubit.speedUp(),
icon: const Icon(Icons.add)),
],
));
},
));
}
}

class _SpeedIndicatorButton extends StatelessWidget {
const _SpeedIndicatorButton({
required this.onPressed,
required this.icon,
});

final Function() onPressed;
final Icon icon;

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8),
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: WidgetStatePropertyAll(Theme.of(context)
.buttonTheme
.colorScheme!
.onSecondary
.withOpacity(0.9)),
),
onPressed: onPressed,
child: icon));
}
}