Created
February 27, 2023 12:49
-
-
Save LokieVikky/922df6174e3819b4f40720a1e140c6c5 to your computer and use it in GitHub Desktop.
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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
home: FirstPage(), | |
); | |
} | |
} | |
class FirstPage extends StatefulWidget { | |
const FirstPage({Key? key}) : super(key: key); | |
@override | |
State<FirstPage> createState() => _FirstPageState(); | |
} | |
class _FirstPageState extends State<FirstPage> { | |
List<String> items = [ | |
'Item 1', | |
'Item 2', | |
'Item 3', | |
'Item 4', | |
'Item 5', | |
]; | |
String dropdownValue = 'Item 1'; | |
@override | |
Widget build(BuildContext context) { | |
List<DropdownMenuItem<String>> menuItems = []; | |
for (int i = 0; i < items.length; i++) { | |
DropdownMenuItem<String> item = DropdownMenuItem( | |
value: items[i], | |
child: Text(items[i]), | |
); | |
menuItems.add(item); | |
} | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Heading'), | |
), | |
body: Row( | |
children: [ | |
Container( | |
color: Colors.red, | |
child: DropdownButton<String>( | |
value: dropdownValue, | |
items: items.map((e) => DropdownMenuItem(value: e, child: Text(e))).toList(), | |
onChanged: (String? data) { | |
dropdownValue = data!; | |
setState(() {}); | |
}, | |
), | |
), | |
ElevatedButton( | |
onPressed: () {}, | |
child: const Text('Change State'), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment