Custom Widgets in Flutter | How to make Custom widgets in Flutter ?

custom widget in flutter

Flutter Tutorial:

Introduction

Flutter

Why Flutter

About Flutter

Cross Platform

MVVM vs MVC vs MVP

Flutter Framework

Flutter Benefits

Flutter Comparison I

Flutter Comparison II

Flutter Comparison III

Install Flutter

Android studio vs VsCode

Android Setup

VsCode Setup

Vs Code Plugins

Android Studio Plugins

Flutter Widgets:

Flutter Basic Templates

Flutter Commands

Common Widgets

Top 10 popular widgets

Flutter Stateless vs Stateful

Type of Widgets

Flutter Text

Flutter Text Style

Textfield vs TextFormField

Flutter Scaffold

Flutter Container & SizedBox

Flutter Row & Column

Flutter Buttons

Flutter Stack

Flutter Forms

Flutter AlertDialog

Flutter Icons

Flutter Images

Flutter Drawer

Flutter ListView

Flutter GridView

Flutter Toast

Flutter Checkbox

Flutter Radio Button

Flutter Progress Bar

Flutter Tooltip

Flutter Slider

Flutter Table

Flutter SnackBar

Shimmer in Flutter

Bottom Navigation Bar

Flutter Gesture

Flutter Error Handling

Flutter DropDown

Flutter Toggle

Flutter Auto Close Keyboard

Flutter Screen Size

Flutter Advance

Custom Widget in Flutter

Flutter Navigator

Flutter Read Json

Flutter Generate Excel

Flutter Multiple Widgets

Flutter Bottom sheet

Flutter Copy to Clipboard

Flutter Tab bar

Flutter Code Editor

Flutter youtube Player

Flutter REST API

Flutter http

Flutter dio

dio vs http

Advanced Concepts

Tips Flutter App Development

Flutter App version Update

Flutter Copy Text in App

Flutter Handle Null Value

Flutter Splash Screen

Flutter Disposable

Notification Listener

Flutter Switch Cases

Flutter Slivers

Flutter Custom Appbar

Databinding in Flutter

Flutter Cards

Wrap vs Builder vs OverBarFlow

Flutter App Upgrade

GoogleMap vs FlutterMap

Circular progress contain Icon

DropDown Timer in Flutter

Flutter State management Comparison

Flutter vs Other Framework

Flutter Mixin

Flutter Database

Flutter Database

Suitable DB for Flutter

DBs for Flutter

Backend for flutter

SharedPreferences

Flutter Token Expired Handling

Flutter Provider

Flutter Provider Tutorial

Flutter GetX

Flutter GetX tutorial

Flutter with Native

Flutter FFI

Flutter Testing

Pass values in Flutter

WorkManager

Flutter Tips:

Best Practices

Reduce Flutter Screens

Tips to make app smart

Optimize App

Handle Multiple Pages

Interview Questions

Top 10 Interview Questions

Dart Interview Questions

Flutter 100 Interview Questions

Flutter 20 Interview Questions

Provider Interview Questions

GetX interview Questions

BLoC interview Questions

Custom widget in flutter

Using custom widgets is one of the best practices, it not only reduces code but also improves efficiency. In Flutter applications, we generally implement custom widgets where we need common functionality such as entering text fields (email, password, mobile number, etc.). The most common example of a custom widget requiring a text field

To create a custom text field in Flutter that allows users to modify the text field according to their needs, below code clearly explain

CustomTextField widget Example

import 'package:flutter/material.dart';
import 'package:rttutorials/custom_text_field.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ResearchThinker',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyAppCardExample(),
    );
  }
}

class MyAppCardExample extends StatelessWidget {
  const MyAppCardExample({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('ResearchThinker Flutter Examples'),
        ),
        body: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.all(16.0),
            child: Column(
              children: [
//Here we integrate customTextField instead of normal Textfield and we can use this CustomTextField multiple times as per our requirements
                CustomTextField(
                  controller: TextEditingController(),
                  hintText: 'Enter your name',
                  icon: Icons.person,
                ),
                const SizedBox(
                  height: 5,
                ),
                CustomTextField(
                  controller: TextEditingController(),
                  hintText: 'Enter Mobile Number',
                  icon: Icons.mobile_friendly,
                ),
                const SizedBox(
                  height: 5,
                ),
                CustomTextField(
                  controller: TextEditingController(),
                  hintText: 'Enter Password',
                  icon: Icons.password,
                  obscureText: true,
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}



// you can create different dart file for below code, in this example we //create file name custom_text_field.dart

import 'package:flutter/material.dart';
//In this way you can create custom text field,  this custom widget is used for text related services
class CustomTextField extends StatefulWidget {
  final TextEditingController controller;
  final String hintText;
  final IconData icon;
  final bool obscureText;

  CustomTextField({
    required this.controller, // required mean compulsory to pass parameters
    required this.hintText,
    required this.icon,
    this.obscureText = false,
  });

  @override
  _CustomTextFieldState createState() => _CustomTextFieldState();
}

class _CustomTextFieldState extends State<CustomTextField> {
  @override
  Widget build(BuildContext context) {
//Note: If you want to add more parameters, then you have decalre also like  widget.obscureText
    return TextFormField(
      controller: widget.controller,
      obscureText: widget.obscureText,
      decoration: InputDecoration(
        hintText: widget.hintText,
        prefixIcon: Icon(widget.icon),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10.0),
        ),
      ),
    );
  }
}

In this example, the CustomTextField widget takes several parameters:

  • controller: The TextEditingController to manage the text field’s value, which behave similar like normal TextFormField.
  • hintText: The placeholder text to display inside the text field, this one also behave similar like TextFormField.
  • icon: The icon to show as a prefix inside the text field.(you can also add Sufix Icon in this)
  • obscureText: A flag indicating whether the text should be visible or not (e.g., for password fields).

Remaning parameters are same like TextFormField

You can use this CustomTextField widget in your Flutter application , the syntax of declaring customTextField is :-

CustomTextField(
  controller: TextEditingController(),
  hintText: 'Enter your name',
  icon: Icons.person,
)

If you face any issue, please comment below, we will reply soon

Leave a Reply

Your email address will not be published. Required fields are marked *

web_horizontal
About Us ♢ Disclaimer ♢ Privacy Policy ♢ Terms & Conditions ♢ Contact Us

Copyright © 2023 ResearchThinker.com. All rights reserved.