![](/img/trans.png)
[英]How to Change the splash color when tapping DropdownButton In flutter?
[英]How to change a variable when changing DropDownButton
我有 3 個變量,其中包含可以傳遞給message
的文本。
String easyDrop = 'All ok';
String mediumDrop = '1 problem';
String hardDrop = 'All not ok';
message: easyDrop ,
我想根據我的 DropDownButton 的 state 更改它們。 我怎樣才能做到這一點?
import 'package:flutter/material.dart';
class DropDownButtonDifficultySettingsWidget extends StatefulWidget {
DropDownButtonDifficultySettingsWidget({Key? key}) : super(key: key);
@override
State<DropDownButtonDifficultySettingsWidget> createState() => _DropDownButtonDifficultySettingsState();
}
class _DropDownButtonDifficultySettingsState extends State<DropDownButtonDifficultySettingsWidget> {
String dropdownValue = 'Medium';
@override
Widget build(BuildContext context) {
return Theme(
data: ThemeData(
splashColor: Colors.blue.withOpacity(0.4),),
child: DropdownButton<String>(
value: dropdownValue,
elevation: 8,
alignment: Alignment.centerRight,
iconDisabledColor: Colors.blue,
iconEnabledColor: Colors.blue,
underline: Container(
height: 0,
),
style: const TextStyle(color: Colors.blue, fontWeight: FontWeight.w500, ),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['Easy', 'Medium', 'Hard']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
);
}
}
您可以包含一個回調方法以從DropDownButtonDifficultySettingsWidget
獲取所選項目
class DropDownButtonDifficultySettingsWidget extends StatefulWidget {
final Function(String? selectedValue) callback;
const DropDownButtonDifficultySettingsWidget(
{Key? key, required this.callback})
: super(key: key);
@override
State<DropDownButtonDifficultySettingsWidget> createState() =>
_DropDownButtonDifficultySettingsState();
}
並改變了
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
widget.callback(dropdownValue); //this
},
現在,當您使用DropDownButtonDifficultySettingsWidget
時,您將在回調中獲得選定的值
DropDownButtonDifficultySettingsWidget(
callback: (selectedValue) {
print(selectedValue);
/// do the thing you like to have
},
),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.