How can I resolve Error: The argument type ‘Text’ can’t be assigned to the parameter type ‘String’

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

Your dropdown:

Widget _buildDropdownField(String label) {
  return Padding(
    padding: ...

Is expecting a String, but your passing in:

_buildDropdownField(
                Text(
              'Country*',
              style: TextStyle(color: Colors.white),)

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:

Widget _buildDropdownField(String label, {TextStyle? style}) {
  return Padding(

and use it:

return DropdownMenuItem<String>(
              value: value,
              child: Text(value, style: style ?? TextStyle()),
            );

Complete runnable snippet:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: Center(
            child: _buildDropdownField('red',
                style: TextStyle(color: Colors.red))),
      ),
    );
  }
}

Widget _buildDropdownField(String label, {TextStyle? style}) {
  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, style: style ?? TextStyle()),
            );
          }).toList(),
          onChanged: (_) {},
          hint: Text('Dropdown menu'),
        ),
      ),
    ),
  );
}

Result:


Note: you are doing passing in:

child: DropdownButton<String>(
          value: null,
          isExpanded: true,

null to the value, so, you won’t actually be able to select anything…

Here is your fix for your widget method

body: SingleChildScrollView(
  padding: EdgeInsets.all(16.0),
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      _buildDropdownField(
        Text(
          'Country*',
          style: TextStyle(color: Colors.white), // Set text color here
        ),
      ),
      _buildDropdownField("City*"),
      _buildDropdownField("District*"),
    ],
  ),
),

Widget _buildDropdownField(Widget label) { // Use Widget instead of String
  return Padding(
    padding: const EdgeInsets.symmetric(vertical: 8.0),
    child: InputDecorator(
      decoration: InputDecoration(
        labelText: label is Text ? label.data : label.toString(), // Handle both Text and String types
        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'),
        ),
      ),
    ),
  );
}

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.

 Widget _buildDropdownField(String label, {Color fillColor= Colors.black}) {
       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: fillColor,
        ),
        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'),
          ),
        ),
      ),
    );

   }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật