Files
adventofcode2025/lib/main.dart
2026-03-17 16:19:37 -05:00

118 lines
2.9 KiB
Dart

// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'days_enum.dart';
import 'day_page.dart';
import "util.dart";
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => MyAppState(),
child: MaterialApp(
title: 'Advent of Code 2025',
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
),
home: MyHomePage(),
),
);
}
}
class DayPageState {
String? selectedFile;
}
class MyAppState extends ChangeNotifier {
Map<AdventOfCodeDays, DayPageState> dayPageState = {};
MyAppState() {
for (var day in AdventOfCodeDays.values) {
dayPageState[day] = DayPageState();
}
}
String? getSelectedFile(AdventOfCodeDays day) {
return dayPageState[day]?.selectedFile;
}
void setSelectedFile(AdventOfCodeDays day, String? file) {
debugLog("setSelectedFile called on $day");
dayPageState[day]?.selectedFile = file;
notifyListeners();
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var selectedIndex = 0;
@override
Widget build(BuildContext context) {
// Manage days and create items list of a pages.
final days = AdventOfCodeDays.values;
List<NavigationRailDestination> dayItems = [];
for (var day in days) {
dayItems.add(
NavigationRailDestination(
icon: Icon(Icons.arrow_forward_ios),
label: Text(day.title),
),
);
}
var colorScheme = Theme.of(context).colorScheme;
Widget page = DayPage(days[selectedIndex]);
// The container for the current page, with its background color
// and subtle switching animation.
var mainArea = ColoredBox(
color: colorScheme.surfaceContainerHighest,
child: AnimatedSwitcher(
duration: Duration(milliseconds: 200),
child: page,
),
);
return Scaffold(
body: LayoutBuilder(
builder: (context, constraints) {
return Row(
children: [
SafeArea(
child: NavigationRail(
extended: constraints.maxWidth >= 600,
destinations: dayItems,
selectedIndex: selectedIndex,
onDestinationSelected: (value) {
setState(() {
selectedIndex = value;
});
},
),
),
Expanded(child: mainArea),
],
);
},
),
);
}
}