How to set a red asterisk (*) for sign of is mandatory field in Flutter
Asked Answered
V

6

8

I am trying to get a red asterisk() in a RichText widget; I can use the style property. But it will make the entire text red. I want only the asterisk() to be red. Any clue, how to make it happen?

Here is my RichText Widget, right now i am able to view a asterisk(*) in the same color as the rest of the text.

RichText(
    text: TextSpan(
        text: '$labelText *',
        style: TextStyle(
            color: labelColor, fontWeight: fontWeight, fontSize: fontSize)),
    textScaleFactor: labelTextScale,
    maxLines: labelMaxLines,
    overflow: overflow,
    textAlign: textAlign,
  );
Vasilek answered 14/12, 2018 at 19:4 Comment(0)
V
9

I figured out how to do it. TextSpan has an attribute for children, which takes a List as value.

So i assigned a a TextSpan for the asterisk(*).

 RichText(
    text: TextSpan(
        text: '$labelText',
        style: TextStyle(
            color: labelColor, fontWeight: fontWeight, fontSize: fontSize),
        children: [
           TextSpan(
                  text: ' *',
                  style: TextStyle(
                      color: Colors.red,
                      fontWeight: fontWeight,
                      fontSize: fontSize))
        ]),
    textScaleFactor: labelTextScale,
    maxLines: labelMaxLines,
    overflow: overflow,
    textAlign: textAlign,
  ),
Vasilek answered 14/12, 2018 at 19:42 Comment(0)
E
2

Upgrade flutter to 2.5.3 ,you will get a "label" widget to customize your label. You can change however you want .

 decoration: InputDecoration(
          label: Row(
            children: [
              CustomText(widget.label,
                  font: Font.NUNITO_REGULAR, color: ColorResource.COLOR_cacaca),
              if (widget.isMandatory)
                const CustomText(" *",
                    font: Font.NUNITO_REGULAR,
                    color: ColorResource.COLOR_DB4437)
            ],
          ), 
Eratosthenes answered 29/10, 2021 at 10:20 Comment(0)
A
0

You can use this :

class FormField extends StatelessWidget {
  final String label;
  final bool withAsterisk;
  const FormField({Key key, @required this.label, @required this.withAsterisk})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(children: [
      Container(
        padding: EdgeInsets.fromLTRB(16, 16, 16, 0),
        child: TextFormField(
          decoration: InputDecoration(
              border: OutlineInputBorder(),
              focusedBorder: OutlineInputBorder(),
              errorBorder: OutlineInputBorder(
                borderSide: const BorderSide(color: Colors.red, width: 1),
              )),
        ),
      ),
      Padding(
        padding: const EdgeInsets.only(left: 32, top: 10),
        child: RichText(
          text: TextSpan(
            children: <TextSpan>[
              TextSpan(
                  text: ' $label',
                  style: TextStyle(
                      backgroundColor:
                          Theme.of(context).scaffoldBackgroundColor,
                      color: DsColor.textColorDrkGrey,
                      fontWeight: FontWeight.w400,
                      fontSize: 12)),
              TextSpan(
                  text: withAsterisk ? '* ' : ' ',
                  style: TextStyle(
                      fontWeight: FontWeight.w500,
                      fontSize: 12,
                      backgroundColor:
                          Theme.of(context).scaffoldBackgroundColor,
                      color: Colors.red)),
            ],
          ),
        ),
      ),
    ]);
  }
}

Usage :
FormField(
  label: "Name",
  withAsterisk: true,
),
FormField(
  label: "Phone",
  withAsterisk: false,
)
Astrobiology answered 21/2, 2021 at 9:4 Comment(0)
D
0

You Can Use This even you can change the colors of all the characters in TextSpan via children property.

Here is my sample Code you figure out it :

Here is output you can view it

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Navigation Basics',
    home: HomePage(),
  ));
}

class HomePage extends StatelessWidget {
  String labelText = "Ayush Kumar ";
  
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
     body:Center(
       child:RichText(
    text: TextSpan(
        text: '$labelText',
        style: TextStyle(
            color: Colors.black,
            fontSize:30.0,
          fontWeight: FontWeight.bold,
        ),
        children: [
           TextSpan(
                  text: ' *',
                  style: TextStyle(
                      color: Colors.red,
                         fontSize:30.0,
                    fontWeight: FontWeight.bold,
                    )),
           TextSpan(
                  text: ' *',
                  style: TextStyle(
                      color: Colors.blue,
                    fontSize:30.0,
                    fontWeight: FontWeight.bold,
                    )),
        ])),
     
     
     ),
      
  );
 }
}
 
Delphadelphi answered 26/8, 2021 at 18:48 Comment(0)
Q
0

thanks, to answer from here, I've found a solution...except that you have to change labeltext to label

child: TextFormField(
                      style: TextStyle(
                          color: Colors.black,
                          fontWeight: FontWeight.w500,
                          fontFamily: 'DM Sans'),
                      keyboardType: TextInputType.name,
                      textAlign: TextAlign.center,
                      decoration: InputDecoration(
                        label: RichText(
                          text: TextSpan(
                              text: 'Nom',
                              style: TextStyle(
                                color: Colors.green,
                              ),
                              children: [
                                TextSpan(
                                    text: '*',
                                    style: TextStyle(color: Colors.yellow))
                              ]),
                        ),
                        hintText: 'Nom',
                        labelStyle: TextStyle(color: Color(0xFF6F6F6F)),
                        hintStyle: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.w500,
                          color: Colors.black,
                          fontFamily: 'DM Sans',
                        ),
                        focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Color(0xFF6F6F6F))),
                        border: OutlineInputBorder(
                            borderSide: BorderSide(
                          color: Color(0xFF6F6F6F),
                        )),
                        prefixIcon: Padding(
                          padding: EdgeInsets.all(10),
                          child: SvgPicture.asset("assets/icons/icon_name.svg"),
                        ),
                      ),
                    ),
Quadriplegia answered 8/6, 2023 at 14:24 Comment(0)
D
0

You can easily include an asterisk (*) in the label of a TextFormField without relying on the RichText widget. Here's how you can achieve that:

TextFormField(
  decoration: InputDecoration(
    label: Text("Title *"), // Adding an asterisk to indicate a required field
    border: OutlineInputBorder(borderSide: BorderSide()),
  ),
),

This simple approach allows you to indicate required fields clearly while keeping your code clean and straightforward! Feel free to modify any part of it to better fit your style or needs!

Output: sample 1

Sample 2

Devise answered 2/9 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.