Last active
June 15, 2022 07:38
-
-
Save mj-hd/6fc64669b074dff03ad1fa9bef9e1ae8 to your computer and use it in GitHub Desktop.
enum_widget.dart
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(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