I have this code snippetbelow
body: SingleChildScrollView(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildDropdownField(
Text(
'Country*',
style: TextStyle(color: Colors.white),)
),
_buildDropdownField("City*"),
_buildDropdownField("District*"),
I tried to change the color of first _buildDropdownField
by using Text()
but then I get this error:
64:17: Error: The argument type 'Text' can't be assigned to the parameter type 'String'.
- 'Text' is from 'package:flutter/src/widgets/text.dart' ('../../../flutter/packages/flutter/lib/src/widgets/text.dart').
Text(
How can I change the text color of plain text inside a widget like that ? Also I tried RichText()
I got the same error.
here is _buildDropdownField
Widget _buildDropdownField(String label) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: InputDecorator(
decoration: InputDecoration(
labelText: label,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
),
filled: true,
fillColor: Colors.white,
),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: null,
isExpanded: true,
items: <String>['Option 1', 'Option 2', 'Option 3'].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (_) {},
hint: Text('Dropdown menu'),
),
),
),
);
}
2
Answers
Your dropdown:
Is expecting a
String
, but your passing in:a
Text
widget.If you want to modify the text color, you should do it on as a property for the
Text
widget.For example:
accept an optional
style
:and use it:
Complete runnable snippet:
Result:
Note: you are doing passing in:
null
to thevalue
, so, you won’t actually be able to select anything…Here is your fix for your widget method
But efficent way to handle this scenario is having a resuable method which accept text:string, color: Color as arguments and have default color within method.