Skip to content

Instantly share code, notes, and snippets.

@knezzz
Created March 27, 2019 12:03
Show Gist options
  • Save knezzz/7cc3bb75992413cb43f1d2a3328b7295 to your computer and use it in GitHub Desktop.
Save knezzz/7cc3bb75992413cb43f1d2a3328b7295 to your computer and use it in GitHub Desktop.
StackOverflow
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Car _selectedCar;
List<Car> _cars;
@override
void initState() {
super.initState();
_cars = Car.getCars();
_selectedCar = _cars.first;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(child: getDropDown()));
}
Widget getDropDown() {
var items = _cars.map((car) {
return new DropdownMenuItem<Car>(
value: car,
child: new Text(
car.make,
style: new TextStyle(color: Colors.black),
),
);
}).toList();
return DropdownButton<Car>(
value: this._selectedCar,
onChanged: (Car car) {
setState(() {
this._selectedCar = car;
});
},
items: items);
}
}
class Car {
final int id;
final String make;
Car(this.id, this.make);
static List<Car> getCars() {
return <Car>[
Car(1, "Ford"),
Car(2, "Toyota"),
Car(3, "BMW"),
];
}
}
@wesleyhodaj
Copy link

hallall je i madh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment