Skip to content

Instantly share code, notes, and snippets.

@LokieVikky
Created February 27, 2023 12:49
Show Gist options
  • Save LokieVikky/922df6174e3819b4f40720a1e140c6c5 to your computer and use it in GitHub Desktop.
Save LokieVikky/922df6174e3819b4f40720a1e140c6c5 to your computer and use it in GitHub Desktop.
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