app/lib/screens/behavioral_screen.dart
John Mizerek 151106aa8e Initial commit: Add Months MVP
Local-first Flutter app that identifies the single behavioral change
most likely to extend lifespan using hazard-based modeling.

Features:
- Risk engine with hazard ratios from meta-analyses
- 50 countries mapped to 4 mortality groups
- 6 modifiable factors: smoking, alcohol, sleep, activity, driving, work hours
- SQLite local storage (no cloud, no accounts)
- Muted clinical UI theme
- 23 unit tests for risk engine

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-20 21:25:00 -08:00

357 lines
10 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../models/models.dart';
import '../storage/local_storage.dart';
import '../theme.dart';
import 'results_screen.dart';
class BehavioralScreen extends StatefulWidget {
final UserProfile profile;
const BehavioralScreen({super.key, required this.profile});
@override
State<BehavioralScreen> createState() => _BehavioralScreenState();
}
class _BehavioralScreenState extends State<BehavioralScreen> {
SmokingStatus _smoking = SmokingStatus.never;
int _cigarettesPerDay = 0;
AlcoholLevel _alcohol = AlcoholLevel.none;
double _sleepHours = 7.5;
bool _sleepConsistent = true;
ActivityLevel _activity = ActivityLevel.moderate;
DrivingExposure _driving = DrivingExposure.low;
WorkHoursLevel _workHours = WorkHoursLevel.normal;
@override
void initState() {
super.initState();
_loadExistingBehaviors();
}
Future<void> _loadExistingBehaviors() async {
final behaviors = await LocalStorage.getBehaviors();
if (behaviors != null) {
setState(() {
_smoking = behaviors.smoking;
_cigarettesPerDay = behaviors.cigarettesPerDay;
_alcohol = behaviors.alcohol;
_sleepHours = behaviors.sleepHours;
_sleepConsistent = behaviors.sleepConsistent;
_activity = behaviors.activity;
_driving = behaviors.driving;
_workHours = behaviors.workHours;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Behaviors'),
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => Navigator.pop(context),
),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Modifiable Factors',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
Text(
'These behaviors can be changed. We\'ll identify which has the largest impact.',
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 32),
// Smoking
_buildSectionLabel('Smoking'),
const SizedBox(height: 12),
_buildSmokingSelector(),
if (_smoking == SmokingStatus.current) ...[
const SizedBox(height: 16),
_buildCigarettesSlider(),
],
const SizedBox(height: 28),
// Alcohol
_buildSectionLabel('Alcohol'),
const SizedBox(height: 12),
_buildAlcoholSelector(),
const SizedBox(height: 28),
// Sleep
_buildSectionLabel('Sleep'),
const SizedBox(height: 12),
_buildSleepSlider(),
const SizedBox(height: 12),
_buildSleepConsistentToggle(),
const SizedBox(height: 28),
// Physical Activity
_buildSectionLabel('Physical Activity'),
const SizedBox(height: 12),
_buildActivitySelector(),
const SizedBox(height: 28),
// Driving Exposure
_buildSectionLabel('Driving Exposure'),
const SizedBox(height: 12),
_buildDrivingSelector(),
const SizedBox(height: 28),
// Work Hours
_buildSectionLabel('Work Hours'),
const SizedBox(height: 12),
_buildWorkHoursSelector(),
const SizedBox(height: 40),
// Calculate button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _calculate,
child: const Text('Calculate'),
),
),
const SizedBox(height: 16),
],
),
),
);
}
Widget _buildSectionLabel(String label) {
return Text(
label,
style: Theme.of(context).textTheme.labelLarge,
);
}
Widget _buildSmokingSelector() {
return _buildSegmentedControl<SmokingStatus>(
value: _smoking,
options: [
(SmokingStatus.never, 'Never'),
(SmokingStatus.former, 'Former'),
(SmokingStatus.current, 'Current'),
],
onChanged: (value) => setState(() => _smoking = value),
);
}
Widget _buildCigarettesSlider() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Cigarettes per day',
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: Slider(
value: _cigarettesPerDay.toDouble(),
min: 1,
max: 40,
divisions: 39,
onChanged: (value) {
// Haptic feedback at 20 (one pack)
if (value.round() == 20 && _cigarettesPerDay != 20) {
HapticFeedback.mediumImpact();
}
setState(() => _cigarettesPerDay = value.round());
},
),
),
SizedBox(
width: 50,
child: Text(
'$_cigarettesPerDay',
style: Theme.of(context).textTheme.headlineMedium,
textAlign: TextAlign.center,
),
),
],
),
Text(
_cigarettesPerDay <= 20
? '${(_cigarettesPerDay / 20).toStringAsFixed(1)} pack/day'
: '${(_cigarettesPerDay / 20).toStringAsFixed(1)} packs/day',
style: Theme.of(context).textTheme.bodySmall,
),
],
);
}
Widget _buildAlcoholSelector() {
return _buildSegmentedControl<AlcoholLevel>(
value: _alcohol,
options: [
(AlcoholLevel.none, 'None'),
(AlcoholLevel.light, '1-7/wk'),
(AlcoholLevel.moderate, '8-14'),
(AlcoholLevel.heavy, '15-21'),
(AlcoholLevel.veryHeavy, '21+'),
],
onChanged: (value) => setState(() => _alcohol = value),
);
}
Widget _buildSleepSlider() {
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Average hours per night',
style: Theme.of(context).textTheme.bodyMedium,
),
Text(
_sleepHours.toStringAsFixed(1),
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
Slider(
value: _sleepHours,
min: 4,
max: 12,
divisions: 16,
onChanged: (value) => setState(() => _sleepHours = value),
),
],
);
}
Widget _buildSleepConsistentToggle() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Consistent schedule',
style: Theme.of(context).textTheme.bodyLarge,
),
Switch(
value: _sleepConsistent,
onChanged: (value) => setState(() => _sleepConsistent = value),
),
],
);
}
Widget _buildActivitySelector() {
return _buildSegmentedControl<ActivityLevel>(
value: _activity,
options: [
(ActivityLevel.sedentary, 'Sedentary'),
(ActivityLevel.light, 'Light'),
(ActivityLevel.moderate, 'Moderate'),
(ActivityLevel.high, 'High'),
],
onChanged: (value) => setState(() => _activity = value),
);
}
Widget _buildDrivingSelector() {
return _buildSegmentedControl<DrivingExposure>(
value: _driving,
options: [
(DrivingExposure.low, '<50 mi/wk'),
(DrivingExposure.moderate, '50-150'),
(DrivingExposure.high, '150-300'),
(DrivingExposure.veryHigh, '300+'),
],
onChanged: (value) => setState(() => _driving = value),
);
}
Widget _buildWorkHoursSelector() {
return _buildSegmentedControl<WorkHoursLevel>(
value: _workHours,
options: [
(WorkHoursLevel.normal, '<40'),
(WorkHoursLevel.elevated, '40-55'),
(WorkHoursLevel.high, '55-70'),
(WorkHoursLevel.extreme, '70+'),
],
onChanged: (value) => setState(() => _workHours = value),
);
}
Widget _buildSegmentedControl<T>({
required T value,
required List<(T, String)> options,
required ValueChanged<T> onChanged,
}) {
return Container(
decoration: BoxDecoration(
color: AppColors.surfaceVariant,
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: options.map((option) {
final isSelected = value == option.$1;
return Expanded(
child: GestureDetector(
onTap: () => onChanged(option.$1),
child: Container(
padding: const EdgeInsets.symmetric(vertical: 12),
decoration: BoxDecoration(
color: isSelected ? AppColors.primary : Colors.transparent,
borderRadius: BorderRadius.circular(12),
),
child: Text(
option.$2,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: isSelected ? Colors.white : AppColors.textSecondary,
),
),
),
),
);
}).toList(),
),
);
}
void _calculate() async {
final behaviors = BehavioralInputs(
smoking: _smoking,
cigarettesPerDay: _cigarettesPerDay,
alcohol: _alcohol,
sleepHours: _sleepHours,
sleepConsistent: _sleepConsistent,
activity: _activity,
driving: _driving,
workHours: _workHours,
);
await LocalStorage.saveBehaviors(behaviors);
if (mounted) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => ResultsScreen(
profile: widget.profile,
behaviors: behaviors,
),
),
);
}
}
}