The default Hint alignment is Left in TextField widget in flutter. But flutter gives us facility to change Alignment in 6 different directions Center, End, Left, Right, Justify and Start. Application developer can change TextField inside text align using textAlign prop with 6 different properties. So in this tutorial we would Flutter Set PlaceHolder Hint Text Alignment Center in TextField Android iOS Example Tutorial.
Contents in this project Flutter Set PlaceHolder Hint Text Alignment Center in TextField Android iOS Example Tutorial:
1. Open your project’s main.dart file and import material.dart package.
1 |
import 'package:flutter/material.dart'; |
2. Creating void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main MyApp class extends State less widget. This is our main Root class.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
4. Creating Widget Build Area -> Material App Widget -> Scaffold Widget -> Safe Area Widget -> Center Widget -> Container Widget.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: SafeArea( child: Center( child: Container( width: 290, padding: EdgeInsets.all(10.0), child: ) ) ) ) ); } |
5. Creating TextField Widget in Container Widget as Child.
- textAlign : TextAlign.center is used to Align the Hint Text in Center of Text Field widget.
1 2 3 4 5 6 7 |
TextField( autocorrect: true, textAlign: TextAlign.center, decoration: InputDecoration( hintText: 'Type Text Here', ) ) |
6. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: SafeArea( child: Center( child: Container( width: 290, padding: EdgeInsets.all(10.0), child: TextField( autocorrect: true, textAlign: TextAlign.center, decoration: InputDecoration( hintText: 'Type Text Here', ))))))); } } |
Screenshot:
