WIP: Turn advent of code into flutter app #1
12
adventofcode2025/lib/days_enum.dart
Normal file
12
adventofcode2025/lib/days_enum.dart
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
|
||||||
|
enum AdventOfCodeDays {
|
||||||
|
day1(title: "Day 1"),
|
||||||
|
day2(title: "Day 2"),;
|
||||||
|
|
||||||
|
const AdventOfCodeDays({
|
||||||
|
required this.title
|
||||||
|
});
|
||||||
|
|
||||||
|
final String title;
|
||||||
|
}
|
||||||
@@ -1,122 +1,133 @@
|
|||||||
|
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
|
||||||
|
|
||||||
|
import 'package:english_words/english_words.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'days_enum.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(MyApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
const MyApp({super.key});
|
const MyApp({super.key});
|
||||||
|
|
||||||
// This widget is the root of your application.
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return ChangeNotifierProvider(
|
||||||
title: 'Flutter Demo',
|
create: (context) => MyAppState(),
|
||||||
theme: ThemeData(
|
child: MaterialApp(
|
||||||
// This is the theme of your application.
|
title: 'Advent of Code 2025',
|
||||||
//
|
theme: ThemeData(
|
||||||
// TRY THIS: Try running your application with "flutter run". You'll see
|
useMaterial3: true,
|
||||||
// the application has a purple toolbar. Then, without quitting the app,
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
|
||||||
// try changing the seedColor in the colorScheme below to Colors.green
|
),
|
||||||
// and then invoke "hot reload" (save your changes or press the "hot
|
home: MyHomePage(),
|
||||||
// reload" button in a Flutter-supported IDE, or press "r" if you used
|
|
||||||
// the command line to start the app).
|
|
||||||
//
|
|
||||||
// Notice that the counter didn't reset back to zero; the application
|
|
||||||
// state is not lost during the reload. To reset the state, use hot
|
|
||||||
// restart instead.
|
|
||||||
//
|
|
||||||
// This works for code too, not just values: Most code changes can be
|
|
||||||
// tested with just a hot reload.
|
|
||||||
colorScheme: .fromSeed(seedColor: Colors.deepPurple),
|
|
||||||
),
|
),
|
||||||
home: const MyHomePage(title: 'Flutter Demo Home Page'),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MyAppState extends ChangeNotifier {
|
||||||
|
var current = WordPair.random();
|
||||||
|
var history = <WordPair>[];
|
||||||
|
|
||||||
|
GlobalKey? historyListKey;
|
||||||
|
|
||||||
|
void getNext() {
|
||||||
|
history.insert(0, current);
|
||||||
|
var animatedList = historyListKey?.currentState as AnimatedListState?;
|
||||||
|
animatedList?.insertItem(0);
|
||||||
|
current = WordPair.random();
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
var favorites = <WordPair>[];
|
||||||
|
|
||||||
|
void toggleFavorite([WordPair? pair]) {
|
||||||
|
pair = pair ?? current;
|
||||||
|
if (favorites.contains(pair)) {
|
||||||
|
favorites.remove(pair);
|
||||||
|
} else {
|
||||||
|
favorites.add(pair);
|
||||||
|
}
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeFavorite(WordPair pair) {
|
||||||
|
favorites.remove(pair);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class MyHomePage extends StatefulWidget {
|
class MyHomePage extends StatefulWidget {
|
||||||
const MyHomePage({super.key, required this.title});
|
|
||||||
|
|
||||||
// This widget is the home page of your application. It is stateful, meaning
|
|
||||||
// that it has a State object (defined below) that contains fields that affect
|
|
||||||
// how it looks.
|
|
||||||
|
|
||||||
// This class is the configuration for the state. It holds the values (in this
|
|
||||||
// case the title) provided by the parent (in this case the App widget) and
|
|
||||||
// used by the build method of the State. Fields in a Widget subclass are
|
|
||||||
// always marked "final".
|
|
||||||
|
|
||||||
final String title;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<MyHomePage> createState() => _MyHomePageState();
|
State<MyHomePage> createState() => _MyHomePageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
int _counter = 0;
|
var selectedIndex = 0;
|
||||||
|
|
||||||
void _incrementCounter() {
|
|
||||||
setState(() {
|
|
||||||
// This call to setState tells the Flutter framework that something has
|
|
||||||
// changed in this State, which causes it to rerun the build method below
|
|
||||||
// so that the display can reflect the updated values. If we changed
|
|
||||||
// _counter without calling setState(), then the build method would not be
|
|
||||||
// called again, and so nothing would appear to happen.
|
|
||||||
_counter++;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
// This method is rerun every time setState is called, for instance as done
|
// Manage days and create items list of a pages.
|
||||||
// by the _incrementCounter method above.
|
final days = [AdventOfCodeDays.day1, AdventOfCodeDays.day2];
|
||||||
//
|
List<NavigationRailDestination> dayItems = [];
|
||||||
// The Flutter framework has been optimized to make rerunning build methods
|
for (var day in days) {
|
||||||
// fast, so that you can just rebuild anything that needs updating rather
|
dayItems.add(
|
||||||
// than having to individually change instances of widgets.
|
NavigationRailDestination(
|
||||||
return Scaffold(
|
icon: Icon(Icons.arrow_forward_ios),
|
||||||
appBar: AppBar(
|
label: Text(day.title),
|
||||||
// TRY THIS: Try changing the color here to a specific color (to
|
|
||||||
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
|
|
||||||
// change color while the other colors stay the same.
|
|
||||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
||||||
// Here we take the value from the MyHomePage object that was created by
|
|
||||||
// the App.build method, and use it to set our appbar title.
|
|
||||||
title: Text(widget.title),
|
|
||||||
),
|
|
||||||
body: Center(
|
|
||||||
// Center is a layout widget. It takes a single child and positions it
|
|
||||||
// in the middle of the parent.
|
|
||||||
child: Column(
|
|
||||||
// Column is also a layout widget. It takes a list of children and
|
|
||||||
// arranges them vertically. By default, it sizes itself to fit its
|
|
||||||
// children horizontally, and tries to be as tall as its parent.
|
|
||||||
//
|
|
||||||
// Column has various properties to control how it sizes itself and
|
|
||||||
// how it positions its children. Here we use mainAxisAlignment to
|
|
||||||
// center the children vertically; the main axis here is the vertical
|
|
||||||
// axis because Columns are vertical (the cross axis would be
|
|
||||||
// horizontal).
|
|
||||||
//
|
|
||||||
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
|
|
||||||
// action in the IDE, or press "p" in the console), to see the
|
|
||||||
// wireframe for each widget.
|
|
||||||
mainAxisAlignment: .center,
|
|
||||||
children: [
|
|
||||||
const Text('You have pushed the button this many times:'),
|
|
||||||
Text(
|
|
||||||
'$_counter',
|
|
||||||
style: Theme.of(context).textTheme.headlineMedium,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
var colorScheme = Theme.of(context).colorScheme;
|
||||||
|
|
||||||
|
Widget page = DayPage();
|
||||||
|
|
||||||
|
// 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,
|
||||||
),
|
),
|
||||||
floatingActionButton: FloatingActionButton(
|
);
|
||||||
onPressed: _incrementCounter,
|
|
||||||
tooltip: 'Increment',
|
return Scaffold(
|
||||||
child: const Icon(Icons.add),
|
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),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DayPage extends StatelessWidget {
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Column(
|
||||||
|
children: [
|
||||||
|
Row(children: [SizedBox(width: 10)]),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -49,6 +49,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.8"
|
version: "1.0.8"
|
||||||
|
english_words:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: english_words
|
||||||
|
sha256: "6a7ef6473a97bd8571b6b641d006a6e58a7c67e65fb6f3d6d1151cb46b0e983c"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.0.0"
|
||||||
fake_async:
|
fake_async:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -131,6 +139,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.17.0"
|
version: "1.17.0"
|
||||||
|
nested:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: nested
|
||||||
|
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.0"
|
||||||
path:
|
path:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -139,6 +155,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.9.1"
|
version: "1.9.1"
|
||||||
|
provider:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: provider
|
||||||
|
sha256: "4e82183fa20e5ca25703ead7e05de9e4cceed1fbd1eadc1ac3cb6f565a09f272"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "6.1.5+1"
|
||||||
sky_engine:
|
sky_engine:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description: flutter
|
description: flutter
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ dependencies:
|
|||||||
# The following adds the Cupertino Icons font to your application.
|
# The following adds the Cupertino Icons font to your application.
|
||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
cupertino_icons: ^1.0.8
|
cupertino_icons: ^1.0.8
|
||||||
|
english_words: ^4.0.0
|
||||||
|
provider: ^6.1.5+1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user