Skip to content

Instantly share code, notes, and snippets.

@yjbanov
Last active March 4, 2025 18:15
Show Gist options
  • Save yjbanov/9337a9fd1c808f6d9372adff263bca75 to your computer and use it in GitHub Desktop.
Save yjbanov/9337a9fd1c808f6d9372adff263bca75 to your computer and use it in GitHub Desktop.
Text field with a label
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