I want to achieve textFormField label floating up above input text, staying INSIDE textFormField and NOT cutting the border off. This is achieved here with a textForm but I cannot get this working with a textFormField!
This seems like missing functionality, and a missing aesthetic UI option. Why must the label float up and cut the border? It does not always look nice, so there should be the option to make the label stay within the field. Possibly will need to post a request to github...
Neither of the two below works for textFormField...
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const appTitle = 'Form Styling Demo';
return MaterialApp(
title: appTitle,
home: Scaffold(
appBar: AppBar(
title: const Text(appTitle),
),
body: TextFieldDesignPage(),
),
);
}
}
class MyCustomForm extends StatelessWidget {
const MyCustomForm({super.key});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 16),
child: Center(
child: Container(
margin: EdgeInsets.all(8),
decoration: BoxDecoration(
border: Border.all(color: Colors.transparent),
borderRadius: BorderRadius.circular(4),
),
child: TextFormField(
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.auto,
contentPadding: EdgeInsets.fromLTRB(25, 0, 0, 0),
labelText: 'Email',
labelStyle: GoogleFonts.lato(
color: Colors.blue,
fontWeight: FontWeight.w500,
fontSize: 12.0,
),
hintStyle: GoogleFonts.lato(
color: Colors.blue,
fontWeight: FontWeight.w500,
fontSize: 12.0,
),
filled: true,
fillColor: Colors.grey,
iconColor: Colors.white,
floatingLabelStyle: null,
alignLabelWithHint: true,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide.none),
disabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: const BorderSide(
style: BorderStyle.none,
),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide(
color: Colors.red,
width: 2,
),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide(
color: Colors.redAccent,
width: 2,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide(
color: Colors.green,
width: 2,
),
),
),
),
),
),
),
],
);
}
}
///
///
/// TEST
class TextFieldDesignPage extends StatefulWidget {
TextFieldDesignPage({Key? key}) : super(key: key);
@override
_TextFieldDesignPageState createState() => _TextFieldDesignPageState();
}
class _TextFieldDesignPageState extends State<TextFieldDesignPage> {
// Use it to change color for border when textFiled in focus
FocusNode _focusNode = FocusNode();
// Color for border
Color _borderColor = Colors.grey;
@override
void initState() {
super.initState();
// Change color for border if focus was changed
_focusNode.addListener(() {
setState(() {
_borderColor = _focusNode.hasFocus ? Colors.orange : Colors.grey;
});
});
}
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
margin: EdgeInsets.all(8),
decoration: BoxDecoration(
border: Border.all(color: _borderColor),
borderRadius: BorderRadius.circular(4),
),
child: TextFormField(
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.auto,
contentPadding: EdgeInsets.fromLTRB(25, 0, 0, 0),
labelText: 'Email',
labelStyle: GoogleFonts.lato(
color: Colors.blue,
fontWeight: FontWeight.w500,
fontSize: 12.0,
),
hintStyle: GoogleFonts.lato(
color: Colors.blue,
fontWeight: FontWeight.w500,
fontSize: 12.0,
),
filled: true,
fillColor: Colors.grey,
iconColor: Colors.white,
floatingLabelStyle: null,
alignLabelWithHint: true,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide.none),
disabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: const BorderSide(
style: BorderStyle.none,
),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide(
color: Colors.red,
width: 2,
),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide(
color: Colors.redAccent,
width: 2,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide(
color: Colors.green,
width: 2,
),
),
),
),
),
),
);
}
}
CodePudding user response:
Use
floatingLabelBehavior: FloatingLabelBehavior.never,
CodePudding user response:
Question is already answered here by someone else in relation to textField. This configuration also works with textFormField but defeats the purpose of all the inherent workings of the textFormField as a container wraps the field with a border and this gets changed with state changes...
Code from solution post:
class TextFieldDesignPage extends StatefulWidget {
TextFieldDesignPage({Key? key}) : super(key: key);
@override
_TextFieldDesignPageState createState() => _TextFieldDesignPageState();
}
class _TextFieldDesignPageState extends State<TextFieldDesignPage> {
// Use it to change color for border when textFiled in focus
FocusNode _focusNode = FocusNode();
// Color for border
Color _borderColor = Colors.grey;
@override
void initState() {
super.initState();
// Change color for border if focus was changed
_focusNode.addListener(() {
setState(() {
_borderColor = _focusNode.hasFocus ? Colors.orange : Colors.grey;
});
});
}
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
margin: EdgeInsets.all(8),
decoration: BoxDecoration(
border: Border.all(color: _borderColor),
borderRadius: BorderRadius.circular(4),
),
child: TextField(
focusNode: _focusNode,
style: TextStyle(color: Colors.grey),
keyboardType: TextInputType.number,
decoration: InputDecoration(
contentPadding: EdgeInsets.zero,
border: InputBorder.none,
labelText: "Amount",
prefixIconConstraints: BoxConstraints(minWidth: 0, minHeight: 0),
prefixIcon: Padding(
padding: EdgeInsets.symmetric(vertical: 18, horizontal: 8),
child: Text("₦", style: TextStyle(fontSize: 16, color: Colors.grey)),
),
),
),
),
),
);
}
}