To change the selected item in a DropdownSearch widget after the widget has been initialized, you can update the value of the selected item in the associated state class and call setState().
Here's an example of how you can change the selected item in a DropdownSearch widget:
- Create a model class for your options (assuming the model class is called
ArabicEnglish):
class ArabicEnglish {
final String arabic;
final String english;
ArabicEnglish({this.arabic, this.english});
}
- Create an instance variable for the selected
ArabicEnglish item in the associated state class:
class _MyWidgetState extends State<MyWidget> {
ArabicEnglish _selectedItem;
// Rest of the code
}
- Initialize your
DropdownSearch widget with the initial selected item:
DropdownSearch<ArabicEnglish>(
items: _arabicEnglishOptions,
onChanged: (item) {
setState(() {
_selectedItem = item;
});
},
selectedItem: _selectedItem,
)
- To change the selected item later on, you can update the
_selectedItem variable and call setState():
setState(() {
_selectedItem = newSelectedItem;
});
Here's a complete example:
import 'package:flutter/material.dart';
import 'package:dropdown_search/dropdown_search.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class ArabicEnglish {
final String arabic;
final String english;
ArabicEnglish({this.arabic, this.english});
}
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
List<ArabicEnglish> _arabicEnglishOptions = [
ArabicEnglish(arabic: 'مرحبا', english: 'Hello'),
ArabicEnglish(arabic: 'مساء الخير', english: 'Good evening'),
ArabicEnglish(arabic: 'شكرا', english: 'Thank you'),
];
ArabicEnglish _selectedItem;
@override
void initState() {
super.initState();
// Set an initial selected item
_selectedItem = _arabicEnglishOptions[0];
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DropdownSearch<ArabicEnglish>(
items: _arabicEnglishOptions,
onChanged: (item) {
setState(() {
_selectedItem = item;
});
},
selectedItem: _selectedItem,
itemAsString: (ArabicEnglish ae) => ae.arabic, // Display the Arabic text in the dropdown
popupItemBuilder: (context, item, isSelected) {
return ListTile(
title: Text(item.arabic),
subtitle: Text(item.english),
selected: isSelected,
);
},
),
RaisedButton(
child: Text('Change Selected Item'),
onPressed: () {
// Change the selected item to the second item in the list
setState(() {
_selectedItem = _arabicEnglishOptions[1];
});
},
),
],
);
}
}
In this example, the _selectedItem variable is initially set to the first item in the _arabicEnglishOptions list. When the user selects a different item from the dropdown, the onChanged callback is called, updating the _selectedItem variable and triggering a rebuild of the widget. There is also a "Change Selected Item" button that allows you to change the selected item to the second item in the list.