我正在尝试使用有状态小部件来定制类,我必须使用有状态的,因为它有 setState 函数,但是我想为类添加属性,所以,每当我调用类时,我都会传递我想要的颜色或存储我想要的数据我使用无状态小部件对 Rassed Button 做了同样的事情,它是有效的,但是对于有状态我有一个错误,即变量未定义
我尝试使用 Appln_id 3 3 3 10 10 10 10 2 4 4
Person_id 23 22 24 49 50 55 51 101 122 104
调用它,但我有一个错误,该小部件未定义
这是代码:
widget.borderColor
这里我正在尝试使用它:
class DoseDropDown extends StatefulWidget {
Color borderColor;
Color hintColor;
DoseDropDown({
this.hintColor,
this.borderColor,
});
@override
_DoseDropDownState createState() => _DoseDropDownState();
}
String medicationDose;
List<DropdownMenuItem> getDropDownItem() {
List<DropdownMenuItem> dropDownItems = [];
for (String dose in medcationDose) {
var newItem = DropdownMenuItem(
child: Text(
dose,
style: TextStyle(
我有一个错误,它没有定义
color: hintColor,
答案 0 :(得分:1)
您可以在下面复制粘贴运行完整代码
在这种情况下,函数 getDropDownItem()
是全局的,而不是在 class _DoseDropDownState
您可以将 hintColor
作为参数传递
您可以在 DropdownButtonFormField
中使用 widget.hintColor
并传递给 getDropDownItem
代码片段
List<DropdownMenuItem> getDropDownItem(Color hintColor) {
...
child: DropdownButtonFormField(
dropdownColor: Colors.white,
value: medicationDose,
items: getDropDownItem(widget.hintColor),
工作演示
完整代码
import 'package:flutter/material.dart';
String medicationDose;
List<DropdownMenuItem> getDropDownItem(Color hintColor) {
List<DropdownMenuItem> dropDownItems = [];
for (String dose in medcationDose) {
var newItem = DropdownMenuItem(
child: Text(
dose,
style: TextStyle(
color: hintColor,
fontSize: 23,
fontWeight: FontWeight.bold,
),
),
value: dose,
);
dropDownItems.add(newItem);
}
return dropDownItems;
}
List<String> medcationDose = [
'مرة واحدة في اليوم',
'مرتان في اليوم',
'ثلاث مرات في اليوم',
'اربعة مرات في اليوم',
'وقت الحاجة'
];
class DoseDropDown extends StatefulWidget {
Color borderColor;
Color hintColor;
DoseDropDown({
this.hintColor,
this.borderColor,
});
@override
_DoseDropDownState createState() => _DoseDropDownState();
}
class _DoseDropDownState extends State<DoseDropDown> {
@override
Widget build(BuildContext context) {
return SizedBox(
height: 70,
width: 350,
child: DropdownButtonFormField(
dropdownColor: Colors.white,
value: medicationDose,
items: getDropDownItem(widget.hintColor),
iconSize: 50,
iconEnabledColor: Colors.yellow,
onChanged: (value) {
setState(() {
medicationDose = value;
});
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.home,
color: Colors.yellow,
size: 30,
),
hintText: 'الجرعة',
hintStyle: TextStyle(
fontSize: 30, fontWeight: FontWeight.bold, color: Colors.white),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.green,
),
borderRadius: BorderRadius.circular(30.0),
),
),
),
);
}
}
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> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DoseDropDown(
hintColor: Colors.brown,
)
],
),
),
);
}
}