Skip to content

Instantly share code, notes, and snippets.

@xErik
Created March 3, 2022 21:13
Show Gist options
  • Save xErik/71d006f9334512a415f63a255844d13e to your computer and use it in GitHub Desktop.
Save xErik/71d006f9334512a415f63a255844d13e to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class Controller extends GetxController {
var items = <String>['a'].obs;
var itemCurrent = 'a'.obs;
addItem(String item) => items.add(item);
}
void main() => runApp(GetMaterialApp(home: Home()));
class Home extends StatelessWidget {
final Controller c = Get.put(Controller());
@override
Widget build(context) {
return Scaffold(
appBar: AppBar(),
body: MyDropdown(),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
c.addItem((c.items.length + 1).toString());
}));
}
}
class MyDropdown extends StatelessWidget {
@override
Widget build(context) {
final Controller c = Get.find();
return Obx(() => DropdownButton<String>(
icon: const Icon(Icons.arrow_drop_down),
value: c.itemCurrent.value,
items: c.items.map((String item) {
return DropdownMenuItem<String>(
value: item,
child: Text(item),
);
}).toList(),
onChanged: (item) {
c.itemCurrent.value = item!;
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment