Search code examples
fluttercomponents

Variable not required in a components Flutter


I'm having a problem with my component in flutter.

The code of my component:

import 'package:flutter/material.dart';

class MyTextField extends StatelessWidget {
  final controller;
  final String hintText;
  final bool obscureText;
  final Icon icon;

  const MyTextField({
    super.key,
    required this.controller,
    required this.hintText,
    required this.obscureText,
    required this.icon,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 25.0),
      child: TextField(
        controller: controller,
        obscureText: obscureText,
        decoration: InputDecoration(
            enabledBorder: const OutlineInputBorder(
              borderSide: BorderSide(color: Colors.white),
            ),
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Colors.grey.shade400),
            ),
            fillColor: Colors.grey.shade200,
            filled: true,
            hintText: hintText,
            hintStyle: TextStyle(color: Colors.grey[500]),
          suffixIcon: icon,
        ),

      ),
    );
  }
}

As you can see I use an icon variable, but I would like to make that variable not necessarily to be used, but just in case I want to put an icon at the end of my TextField then I can do it. The problem is that when I use my component in the Login Page if I write this it gives me an error:

MyTextField(controller: emailController, hintText: 'Email', obscureText: false, icon: null),

How can I fix? Thanks in advance


Solution

  • you are indicating that the icon should be required , it should just change to that it can be null.

    import 'package:flutter/material.dart';
    
    class MyTextField extends StatelessWidget {
      final controller;
      final String hintText;
      final bool obscureText;
      final Icon? icon;
    
      const MyTextField({
        super.key,
        required this.controller,
        required this.hintText,
        required this.obscureText,
          this.icon,
      });
    
      @override
      Widget build(BuildContext context) {
        return Padding(
          padding: const EdgeInsets.symmetric(horizontal: 25.0),
          child: TextField(
            controller: controller,
            obscureText: obscureText,
            decoration: InputDecoration(
                enabledBorder: const OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.white),
                ),
                focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.grey.shade400),
                ),
                fillColor: Colors.grey.shade200,
                filled: true,
                hintText: hintText,
                hintStyle: TextStyle(color: Colors.grey[500]),
              suffixIcon: icon,
            ),
    
          ),
        );
      }
    }

    MyTextField(controller: emailController, hintText: 'Email', obscureText: false),