Skip to content

Instantly share code, notes, and snippets.

@mj-hd
Last active June 15, 2022 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mj-hd/6fc64669b074dff03ad1fa9bef9e1ae8 to your computer and use it in GitHub Desktop.
Save mj-hd/6fc64669b074dff03ad1fa9bef9e1ae8 to your computer and use it in GitHub Desktop.
enum_widget.dart
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatefulWidget {
const MyWidget({Key? key}) : super(key: key);
@override
State<MyWidget> createState() => MyState();
}
enum EnumWidget {
one(
title: 'one',
description: 'one is the first item',
color: Colors.amber,
),
two(
title: 'two',
description: 'two is the second item',
color: Colors.cyan,
),
three(
title: 'three',
description: 'three is the third item',
color: Colors.lime,
);
const EnumWidget({
required this.title,
required this.description,
required this.color,
});
final String title;
final String description;
final Color color;
Widget build(BuildContext context) {
return ColoredBox(
key: ValueKey(this),
color: color,
child: Column(children: [
Text(title, style: const TextStyle(fontSize: 16.0)),
Text(description),
]),
);
}
}
class MyState extends State<MyWidget> {
EnumWidget value = EnumWidget.one;
@override
Widget build(BuildContext context) {
return Column(
children: [
DropdownButton<EnumWidget>(
value: value,
icon: const Icon(Icons.arrow_downward),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (EnumWidget? newValue) {
setState(() {
value = newValue!;
});
},
items: EnumWidget.values.map((EnumWidget value) {
return DropdownMenuItem<EnumWidget>(
value: value,
child: Text(value.name),
);
}).toList(),
),
const Text('Content:'),
value.build(context),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment