I am looking for a way to modify TextField Widget in Flutter in a way that it’s label is displayed in it’s border. Is there any property that ‘TextField’ have to achieve this? or any other way that I could implement it.
I want to achieve something like this.
3
Use the property labelText in TextFormField:
labelText
TextFormField
for example:
TextFormField( maxLines: null, keyboardType: TextInputType.text, textCapitalization: TextCapitalization.sentences, decoration: InputDecoration( isDense: true, labelText: 'Label', labelStyle: const TextStyle( color: Colors.purple, fontSize: 16.0, fontWeight: FontWeight.w800), hintText: 'Input Text', hintStyle: const TextStyle( color: Colors.grey, fontSize: 12.0, fontWeight: FontWeight.w500), suffixIcon: IconButton( onPressed: () {}, icon: const Icon(Icons.edit), ), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), ), contentPadding: const EdgeInsets.only(left: 16.0, top: 26.0), ), style: const TextStyle( color: Colors.black, fontSize: 16.0, fontWeight: FontWeight.w800), ),
Also another way to implement it is to have a Column of title text and textfield inside a container with borders. For example:
Column
Container( margin: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0), decoration: BoxDecoration( border: Border.all( color: const Color(0xFF5801E4), width: 5.0, ), color: Colors.white, ), child: Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('Label', style: TextStyle( color: Colors.purple, fontSize: 16.0, fontWeight: FontWeight.w800, )), TextFormField( maxLines: null, keyboardType: TextInputType.text, textCapitalization: TextCapitalization.sentences, decoration: InputDecoration( isDense: true, labelStyle: const TextStyle( color: Colors.purple, fontSize: 16.0, fontWeight: FontWeight.w800), hintText: 'Input Text', hintStyle: const TextStyle( color: Colors.grey, fontSize: 12.0, fontWeight: FontWeight.w500), suffixIcon: IconButton( onPressed: () {}, icon: const Icon(Icons.edit), ), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), ), contentPadding: const EdgeInsets.only(left: 16.0, top: 26.0), ), style: const TextStyle( color: Colors.black, fontSize: 16.0, fontWeight: FontWeight.w800), ), ]), );
TextField( decoration: InputDecoration( border: OutlineInputBorder(), hintText: 'Input text', labelText: 'Label', ), ),
The output of this code is as follows:
TextField( decoration: InputDecoration( hintText: 'Enter your name', label: Text('Name'), border: OutlineInputBorder(), ), ),
Click here to cancel reply.
3
Answers
Use the property
labelText
inTextFormField
:for example:
Also another way to implement it is to have a
Column
of title text and textfield inside a container with borders. For example:The output of this code is as follows: