Skip to content

Instantly share code, notes, and snippets.

@gonexwind
Created July 11, 2023 02:31
Show Gist options
  • Save gonexwind/ca52128423b8f77747c37054b89abf52 to your computer and use it in GitHub Desktop.
Save gonexwind/ca52128423b8f77747c37054b89abf52 to your computer and use it in GitHub Desktop.
Dropdown Example using Flutter Bloc
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../data/models/order/courier.dart';
import '../../blocs/order/order_bloc.dart';
class DropdownExample extends StatefulWidget {
const DropdownExample({super.key});
@override
State<DropdownExample> createState() => _DropdownExampleState();
}
class _DropdownExampleState extends State<DropdownExample> {
Courier? _selectedCourier;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
SizedBox(height: 200),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Builder(builder: (context) {
final couriers = context.watch<OrderBloc>().state.couriers;
return DropdownButton(
hint: Text("Select Courier"),
value: _selectedCourier,
items: couriers.map((item) {
return DropdownMenuItem(
child: Text(item.namaKurir ?? ''),
value: item,
);
}).toList(),
onChanged: (value) {
setState(() {
_selectedCourier = value;
});
},
);
}),
ElevatedButton(
child: Text('SUBMIT'),
onPressed: () {
print(
'SELECTED: ${_selectedCourier?.id} - ${_selectedCourier?.namaKurir}');
},
),
],
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment