Last active
March 4, 2025 18:15
-
-
Save yjbanov/9337a9fd1c808f6d9372adff263bca75 to your computer and use it in GitHub Desktop.
Text field with a label
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/semantics.dart'; | |
void main() { | |
runApp(MyApp()); | |
SemanticsBinding.instance.ensureSemantics(); | |
} | |
// Shared label | |
const _fieldLabel = 'Enter Text'; | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: Text('Text Field with Label')), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Row( | |
children: <Widget>[ | |
Text(_fieldLabel), // the label is used here... | |
SizedBox(width: 8), | |
Expanded( | |
child: Semantics( | |
label: _fieldLabel, // ...and here | |
child: TextField( | |
decoration: InputDecoration(border: OutlineInputBorder()), | |
), | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment