Search code examples
fluttertextfield

What is the difference between BaseInputField and TextField?


I'm reading a flutter source code and there is a widget named BaseInputField inside it. Is BaseInputField a standard Flutter widget? If so when we use BaseInputField instead of TextField and why? What is the difference between the two and what are the benefits?


Solution

  • BaseInputField is not a standard Flutter widget, so it’s likely that it’s a custom widget created for the specific project you’re looking at.

    Developers often create custom widgets to encapsulate and reuse code throughout their projects.

    On the other hand, TextField is a basic Flutter widget that is used to collect basic text input from a user. You should first get proficient with this fundamental widget.

    The BaseInputField could be a custom widget that extends or wraps the TextField or TextFormField to provide additional functionality or to customize the appearance.

    Sample Code :

    import 'package:flutter/material.dart';
    
    class BaseInputField extends StatelessWidget {
      final String hint;
      final TextEditingController controller;
      final TextInputType keyboardType;
    
      BaseInputField({
        required this.hint,
        required this.controller,
        this.keyboardType = TextInputType.text,
      });
    
      @override
      Widget build(BuildContext context) {
        return TextField(
          controller: controller,
          keyboardType: keyboardType,
          decoration: InputDecoration(
            hintText: hint,
            contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.all(Radius.circular(32.0)),
            ),
            enabledBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Colors.blueAccent, width: 1.0),
              borderRadius: BorderRadius.all(Radius.circular(32.0)),
            ),
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Colors.blueAccent, width: 2.0),
              borderRadius: BorderRadius.all(Radius.circular(32.0)),
            ),
          ),
        );
      }
    }
    

    You can use this BaseInputField in your app like this:

    BaseInputField(
      hint: 'Enter your text here',
      controller: myTextController,
    )