TKK_E32221292/Aplikasi Android/lib/features/home/home_page.dart

763 lines
25 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter_application_1/app/app_session_view_model.dart';
import 'package:flutter_application_1/core/enums/ride_mode_enum.dart';
import 'package:flutter_application_1/core/theme/app_colors.dart';
import 'package:flutter_application_1/shared/widgets/brand_logo.dart';
import 'package:provider/provider.dart';
class HomePage extends StatelessWidget {
final Future<bool> Function()? onExitRequested;
const HomePage({super.key, this.onExitRequested});
static const Color _pageBackground = Color(0xFFF7F7FA);
static const Color _trackColor = Color(0xFFE3E8F0);
static const Color _cardShadow = Color(0x140F172A);
double _modeMaxSpeed(RideMode mode) {
switch (mode) {
case RideMode.eco:
return 10;
case RideMode.normal:
return 15;
case RideMode.boost:
return 20;
}
}
Color _modeTint(RideMode mode) {
switch (mode) {
case RideMode.eco:
return const Color(0xFFEAF8EF);
case RideMode.normal:
return const Color(0xFFEEF4FF);
case RideMode.boost:
return const Color(0xFFFDEEEE);
}
}
Future<void> _handleExit(BuildContext context) async {
await onExitRequested?.call();
}
@override
Widget build(BuildContext context) {
final session = context.watch<AppSessionViewModel>();
final connected = session.isDeviceConnected;
final speed = session.speedKmh;
final battery = session.batteryPercent;
final batteryVoltage = session.batteryVoltage;
final ping = session.pingMs;
final mode = session.rideMode;
final maxSpeed = _modeMaxSpeed(mode);
final progress = connected && speed != null
? (speed / maxSpeed).clamp(0.0, 1.0)
: 0.0;
final displaySpeed = connected && speed != null ? speed.round().toString() : '--';
final displayBattery = connected && battery != null ? '$battery%' : '--';
final displayBatteryVoltage = connected && batteryVoltage != null
? '${batteryVoltage.toStringAsFixed(1)} V'
: 'Voltage belum tersedia';
final displayPing = connected && ping != null ? '${ping}ms' : '--';
return Scaffold(
backgroundColor: _pageBackground,
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const BrandLogo(
width: 170,
showSubtitle: false,
centered: false,
),
const SizedBox(height: 6),
Text(
connected ? 'Connected' : 'Disconnected',
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: connected ? AppColors.green : AppColors.red,
),
),
],
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 13, vertical: 8),
decoration: BoxDecoration(
color: AppColors.white,
borderRadius: BorderRadius.circular(999),
boxShadow: const [
BoxShadow(
color: _cardShadow,
blurRadius: 18,
offset: Offset(0, 4),
),
],
),
child: Text(
displayPing,
style: const TextStyle(
fontSize: 11,
fontWeight: FontWeight.w700,
color: AppColors.textDark,
),
),
),
const SizedBox(width: 8),
InkWell(
borderRadius: BorderRadius.circular(999),
onTap: () => _handleExit(context),
child: Container(
width: 42,
height: 42,
decoration: BoxDecoration(
color: AppColors.white,
shape: BoxShape.circle,
border: Border.all(
color: connected ? const Color(0xFFF4C7C7) : AppColors.inputBorder,
width: 1.2,
),
boxShadow: const [
BoxShadow(
color: _cardShadow,
blurRadius: 18,
offset: Offset(0, 4),
),
],
),
child: Icon(
Icons.logout_rounded,
color: connected ? AppColors.red : AppColors.textMuted,
size: 20,
),
),
),
],
),
const SizedBox(height: 18),
_InfoCard(
padding: const EdgeInsets.fromLTRB(18, 18, 18, 16),
child: LayoutBuilder(
builder: (context, constraints) {
final dialSize = math.min(constraints.maxWidth - 24, 242.0);
return Center(
child: SizedBox(
width: dialSize,
height: dialSize,
child: Stack(
alignment: Alignment.center,
children: [
TweenAnimationBuilder<double>(
tween: Tween<double>(begin: 0, end: progress),
duration: const Duration(milliseconds: 100),
curve: Curves.linear,
builder: (context, animatedProgress, _) {
return CustomPaint(
size: Size.square(dialSize),
painter: _SpeedometerPainter(
progress: animatedProgress,
trackColor: _trackColor,
progressColor: AppColors.primaryBlue,
),
);
},
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
displaySpeed,
style: const TextStyle(
fontSize: 56,
height: 1,
fontWeight: FontWeight.w800,
color: AppColors.textDark,
),
),
const SizedBox(height: 8),
const Text(
'KM/H',
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: AppColors.textMuted,
letterSpacing: 0.6,
),
),
],
),
],
),
),
);
},
),
),
const SizedBox(height: 12),
_InfoCard(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'BATTERY',
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w800,
letterSpacing: 1.1,
color: AppColors.textMuted,
),
),
const SizedBox(height: 8),
Text(
displayBattery,
style: const TextStyle(
fontSize: 28,
height: 1,
fontWeight: FontWeight.w800,
color: AppColors.textDark,
),
),
const SizedBox(height: 6),
Text(
displayBatteryVoltage,
style: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: AppColors.textMuted,
),
),
],
),
),
const SizedBox(height: 12),
Row(
children: [
Expanded(
child: _ModeButton(
label: 'ECO',
subLabel: '10 KM/H',
selected: mode == RideMode.eco,
enabled: connected,
activeColor: AppColors.green,
activeTint: _modeTint(RideMode.eco),
onTap: () => session.setRideMode(RideMode.eco),
),
),
const SizedBox(width: 8),
Expanded(
child: _ModeButton(
label: 'NORMAL',
subLabel: '15 KM/H',
selected: mode == RideMode.normal,
enabled: connected,
activeColor: AppColors.primaryBlue,
activeTint: _modeTint(RideMode.normal),
onTap: () => session.setRideMode(RideMode.normal),
),
),
const SizedBox(width: 8),
Expanded(
child: _ModeButton(
label: 'BOOST',
subLabel: '20 KM/H',
selected: mode == RideMode.boost,
enabled: connected,
activeColor: AppColors.red,
activeTint: _modeTint(RideMode.boost),
onTap: () => session.setRideMode(RideMode.boost),
),
),
],
),
const SizedBox(height: 12),
_PwmCalibrationCard(
session: session,
enabled: connected,
),
],
),
),
),
);
}
}
class _InfoCard extends StatelessWidget {
final Widget child;
final EdgeInsetsGeometry padding;
const _InfoCard({required this.child, required this.padding});
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
padding: padding,
decoration: BoxDecoration(
color: AppColors.white,
borderRadius: BorderRadius.circular(30),
boxShadow: const [
BoxShadow(
color: HomePage._cardShadow,
blurRadius: 22,
offset: Offset(0, 12),
),
],
),
child: child,
);
}
}
class _ModeButton extends StatelessWidget {
final String label;
final String subLabel;
final bool selected;
final bool enabled;
final Color activeColor;
final Color activeTint;
final VoidCallback onTap;
const _ModeButton({
required this.label,
required this.subLabel,
required this.selected,
required this.enabled,
required this.activeColor,
required this.activeTint,
required this.onTap,
});
@override
Widget build(BuildContext context) {
final background = !enabled
? const Color(0xFFF8FAFC)
: selected
? activeTint
: AppColors.white;
final borderColor = !enabled
? AppColors.inputBorder
: selected
? activeColor
: const Color(0xFFE6EAF0);
final labelColor = !enabled
? AppColors.textMuted
: selected
? activeColor
: AppColors.textDark;
final subLabelColor = !enabled
? AppColors.textMuted
: selected
? activeColor
: const Color(0xFF6B7280);
return InkWell(
borderRadius: BorderRadius.circular(24),
onTap: enabled ? onTap : null,
child: AnimatedContainer(
duration: const Duration(milliseconds: 100),
curve: Curves.linear,
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 8),
decoration: BoxDecoration(
color: background,
borderRadius: BorderRadius.circular(22),
border: Border.all(color: borderColor, width: selected ? 1.6 : 1.0),
boxShadow: const [
BoxShadow(
color: Color(0x0D0F172A),
blurRadius: 12,
offset: Offset(0, 6),
),
],
),
child: Column(
children: [
Text(
label,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w800,
color: labelColor,
),
),
const SizedBox(height: 6),
Text(
subLabel,
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w600,
color: subLabelColor,
),
),
],
),
),
);
}
}
class _PwmCalibrationCard extends StatelessWidget {
final AppSessionViewModel session;
final bool enabled;
const _PwmCalibrationCard({
required this.session,
required this.enabled,
});
String _modeLabel(RideMode mode) {
switch (mode) {
case RideMode.eco:
return 'ECO';
case RideMode.normal:
return 'NORMAL';
case RideMode.boost:
return 'BOOST';
}
}
@override
Widget build(BuildContext context) {
return _InfoCard(
padding: const EdgeInsets.fromLTRB(18, 18, 18, 18),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Expanded(
child: Text(
'PWM CALIBRATION',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w900,
letterSpacing: 1.0,
color: AppColors.textDark,
),
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
decoration: BoxDecoration(
color: const Color(0xFFFFF7ED),
borderRadius: BorderRadius.circular(999),
border: Border.all(color: const Color(0xFFFED7AA)),
),
child: const Text(
'TEMP',
style: TextStyle(
fontSize: 10,
fontWeight: FontWeight.w900,
color: Color(0xFFC2410C),
),
),
),
],
),
const SizedBox(height: 6),
Text(
'Mode aktif: ${_modeLabel(session.rideMode)}. Kalibrasi duty 0–1023. Auto-rem dimatikan; rem manual dan ESTOP tetap aktif.',
style: const TextStyle(
fontSize: 12,
height: 1.35,
fontWeight: FontWeight.w600,
color: AppColors.textMuted,
),
),
const SizedBox(height: 14),
_PwmAdjustRow(
label: 'MIN DUTY',
valueLabel: session.selectedCalibrationMinDutyLabel,
helperText: 'tenaga awal motor',
enabled: enabled,
onMinusLarge: () => session.adjustSelectedMinDuty(-50),
onMinusSmall: () => session.adjustSelectedMinDuty(-10),
onPlusSmall: () => session.adjustSelectedMinDuty(10),
onPlusLarge: () => session.adjustSelectedMinDuty(50),
),
const SizedBox(height: 10),
_PwmAdjustRow(
label: 'MAX DUTY',
valueLabel: session.selectedCalibrationMaxDutyLabel,
helperText: 'batas tenaga maksimal',
enabled: enabled,
onMinusLarge: () => session.adjustSelectedMaxDuty(-50),
onMinusSmall: () => session.adjustSelectedMaxDuty(-10),
onPlusSmall: () => session.adjustSelectedMaxDuty(10),
onPlusLarge: () => session.adjustSelectedMaxDuty(50),
),
const SizedBox(height: 14),
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: enabled
? () => session.applySelectedPwmCalibration()
: null,
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.primaryBlue,
foregroundColor: AppColors.white,
elevation: 0,
padding: const EdgeInsets.symmetric(vertical: 13),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18),
),
),
child: const Text(
'APPLY PWM',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w900,
),
),
),
),
const SizedBox(width: 10),
Expanded(
child: ElevatedButton(
onPressed: enabled ? () => session.triggerEmergencyStop() : null,
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.red,
foregroundColor: AppColors.white,
elevation: 0,
padding: const EdgeInsets.symmetric(vertical: 13),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18),
),
),
child: const Text(
'ESTOP',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w900,
),
),
),
),
],
),
],
),
);
}
}
class _PwmAdjustRow extends StatelessWidget {
final String label;
final String valueLabel;
final String helperText;
final bool enabled;
final VoidCallback onMinusLarge;
final VoidCallback onMinusSmall;
final VoidCallback onPlusSmall;
final VoidCallback onPlusLarge;
const _PwmAdjustRow({
required this.label,
required this.valueLabel,
required this.helperText,
required this.enabled,
required this.onMinusLarge,
required this.onMinusSmall,
required this.onPlusSmall,
required this.onPlusLarge,
});
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: const Color(0xFFF8FAFC),
borderRadius: BorderRadius.circular(20),
border: Border.all(color: const Color(0xFFE6EAF0)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.w900,
color: AppColors.textDark,
),
),
const SizedBox(height: 4),
Text(
helperText,
style: const TextStyle(
fontSize: 11,
fontWeight: FontWeight.w600,
color: AppColors.textMuted,
),
),
],
),
),
Text(
valueLabel,
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w900,
color: AppColors.textDark,
),
),
],
),
const SizedBox(height: 10),
Row(
children: [
Expanded(
child: _PwmStepButton(
label: '-50',
enabled: enabled,
onTap: onMinusLarge,
),
),
const SizedBox(width: 8),
Expanded(
child: _PwmStepButton(
label: '-10',
enabled: enabled,
onTap: onMinusSmall,
),
),
const SizedBox(width: 8),
Expanded(
child: _PwmStepButton(
label: '+10',
enabled: enabled,
onTap: onPlusSmall,
),
),
const SizedBox(width: 8),
Expanded(
child: _PwmStepButton(
label: '+50',
enabled: enabled,
onTap: onPlusLarge,
),
),
],
),
],
),
);
}
}
class _PwmStepButton extends StatelessWidget {
final String label;
final bool enabled;
final VoidCallback onTap;
const _PwmStepButton({
required this.label,
required this.enabled,
required this.onTap,
});
@override
Widget build(BuildContext context) {
return InkWell(
borderRadius: BorderRadius.circular(16),
onTap: enabled ? onTap : null,
child: Container(
height: 38,
alignment: Alignment.center,
decoration: BoxDecoration(
color: enabled ? AppColors.white : const Color(0xFFF1F5F9),
borderRadius: BorderRadius.circular(14),
border: Border.all(
color: enabled ? AppColors.inputBorder : const Color(0xFFE5E7EB),
),
),
child: Text(
label,
style: TextStyle(
fontSize: 13,
height: 1,
fontWeight: FontWeight.w900,
color: enabled ? AppColors.textDark : AppColors.textMuted,
),
),
),
);
}
}
class _SpeedometerPainter extends CustomPainter {
final double progress;
final Color trackColor;
final Color progressColor;
const _SpeedometerPainter({
required this.progress,
required this.trackColor,
required this.progressColor,
});
@override
void paint(Canvas canvas, Size size) {
final center = size.center(Offset.zero);
final radius = (size.width / 2) - 18;
const trackWidth = 13.0;
const progressWidth = 15.5;
final rect = Rect.fromCircle(center: center, radius: radius);
final trackPaint = Paint()
..color = trackColor
..style = PaintingStyle.stroke
..strokeWidth = trackWidth
..strokeCap = StrokeCap.round;
final progressPaint = Paint()
..color = progressColor
..style = PaintingStyle.stroke
..strokeWidth = progressWidth
..strokeCap = StrokeCap.round;
canvas.drawArc(rect, -math.pi / 2, math.pi * 2, false, trackPaint);
if (progress <= 0) {
return;
}
final clamped = progress.clamp(0.0, 1.0);
final sweepAngle = math.pi * 2 * clamped;
canvas.drawArc(rect, -math.pi / 2, sweepAngle, false, progressPaint);
}
@override
bool shouldRepaint(covariant _SpeedometerPainter oldDelegate) {
return oldDelegate.progress != progress ||
oldDelegate.trackColor != trackColor ||
oldDelegate.progressColor != progressColor;
}
}