Last active
April 3, 2023 18:16
-
-
Save Abion47/200aded3bd14a91a93ec0a5e9c92ae52 to your computer and use it in GitHub Desktop.
Dart Generic Enums Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
abstract class ClassPreviewConfig { | |
final String name; | |
final String icon; | |
final String description; | |
const ClassPreviewConfig(this.name, this.icon, this.description); | |
} | |
class WarriorPreviewConfig extends ClassPreviewConfig { | |
const WarriorPreviewConfig(super.name, super.icon, super.description); | |
} | |
class RangerPreviewConfig extends ClassPreviewConfig { | |
const RangerPreviewConfig(super.name, super.icon, super.description); | |
} | |
class ThiefPreviewConfig extends ClassPreviewConfig { | |
const ThiefPreviewConfig(super.name, super.icon, super.description); | |
} | |
class ClericPreviewConfig extends ClassPreviewConfig { | |
const ClericPreviewConfig(super.name, super.icon, super.description); | |
} | |
enum ClassPreview<T extends ClassPreviewConfig> { | |
warrior(WarriorPreviewConfig('warrior', 'warrior.png', 'smacky mcmuscles')), | |
ranger(RangerPreviewConfig('ranger', 'ranger.png', 'tracky mcpewpew')), | |
thief(ThiefPreviewConfig('thief', 'thief.png', 'stabby mcklepto')), | |
cleric(ClericPreviewConfig('cleric', 'cleric.png', 'uptight mchealy')); | |
final T config; | |
const ClassPreview(this.config); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const HomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class HomePage extends StatelessWidget { | |
final String title; | |
const HomePage({ | |
Key? key, | |
required this.title, | |
}) : super(key: key); | |
Widget buildClassPreviewButton(BuildContext context, ClassPreview classPreview) { | |
return Padding( | |
padding: const EdgeInsets.only(top: 8), | |
child: ElevatedButton( | |
child: Text(classPreview.config.name), | |
onPressed: () => Navigator.of(context).push( | |
MaterialPageRoute( | |
builder: (cxt) => ClassPreviewPage(classPreview), | |
), | |
), | |
), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Home'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
const Text('Select a class:'), | |
const SizedBox(height: 12), | |
buildClassPreviewButton(context, ClassPreview.warrior), | |
buildClassPreviewButton(context, ClassPreview.ranger), | |
buildClassPreviewButton(context, ClassPreview.thief), | |
buildClassPreviewButton(context, ClassPreview.cleric), | |
], | |
), | |
), | |
); | |
} | |
} | |
class ClassPreviewPage extends StatelessWidget { | |
final ClassPreview classPreview; | |
const ClassPreviewPage(this.classPreview); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(classPreview.config.name), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text(classPreview.config.name), | |
Text(classPreview.config.icon), | |
Text(classPreview.config.description), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: const Icon(Icons.arrow_left), | |
onPressed: () => Navigator.of(context).pop(), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment