70 lines
2.1 KiB
Dart
70 lines
2.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../core/date_keys.dart';
|
|
import '../../models/app_state.dart';
|
|
import '../../models/habit.dart';
|
|
import '../../widgets/app_scaffold.dart';
|
|
import '../../widgets/dialogs.dart';
|
|
import '../../widgets/empty_state.dart';
|
|
import '../../widgets/habit_row.dart';
|
|
|
|
class HabitsScreen extends StatelessWidget {
|
|
const HabitsScreen({
|
|
required this.state,
|
|
required this.onAddHabit,
|
|
required this.onRenameHabit,
|
|
required this.onDeleteHabit,
|
|
required this.onToggleHabit,
|
|
super.key,
|
|
});
|
|
|
|
final AppState state;
|
|
final ValueChanged<String> onAddHabit;
|
|
final void Function(Habit habit, String title) onRenameHabit;
|
|
final ValueChanged<Habit> onDeleteHabit;
|
|
final ValueChanged<Habit> onToggleHabit;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final today = todayKey();
|
|
return AppScaffold(
|
|
title: 'Habits',
|
|
subtitle: 'Keep the list small and repeatable.',
|
|
action: IconButton.filledTonal(
|
|
tooltip: 'Add habit',
|
|
onPressed: () => showHabitDialog(context, onAddHabit),
|
|
icon: const Icon(Icons.add),
|
|
),
|
|
child: ListView(
|
|
padding: const EdgeInsets.fromLTRB(20, 8, 20, 24),
|
|
children: [
|
|
if (state.habits.isEmpty)
|
|
EmptyState(
|
|
icon: Icons.checklist_outlined,
|
|
title: 'No habits yet',
|
|
action: 'Add habit',
|
|
onAction: () => showHabitDialog(context, onAddHabit),
|
|
)
|
|
else
|
|
...state.habits.map(
|
|
(habit) => HabitRow(
|
|
habit: habit,
|
|
dateKey: today,
|
|
onToggle: () => onToggleHabit(habit),
|
|
onEdit:
|
|
() => showHabitDialog(
|
|
context,
|
|
(title) => onRenameHabit(habit, title),
|
|
initialValue: habit.title,
|
|
),
|
|
onDelete:
|
|
() =>
|
|
confirmDeleteHabit(context, () => onDeleteHabit(habit)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|