text
stringlengths 1
474
|
---|
void dispose() {
|
// Clean up the controller when disposing of the Widget.
|
myController.dispose();
|
super.dispose();
|
}
|
@override
|
Widget build(BuildContext context) {
|
return Scaffold(
|
appBar: AppBar(title: const Text('Retrieve Text Input')),
|
body: Padding(
|
padding: const EdgeInsets.all(16),
|
child: TextField(controller: myController),
|
),
|
floatingActionButton: FloatingActionButton(
|
// When the user presses the button, show an alert dialog with the
|
// text the user has typed into our text field.
|
onPressed: () {
|
showDialog(
|
context: context,
|
builder: (context) {
|
return AlertDialog(
|
// Retrieve the text the user has typed in using our
|
// TextEditingController.
|
content: Text(myController.text),
|
);
|
},
|
);
|
},
|
tooltip: 'Show me the value!',
|
child: const Icon(Icons.text_fields),
|
),
|
);
|
}
|
}<code_end>
|
You can find more information and the full code listing in
|
Retrieve the value of a text field,
|
from the Flutter cookbook.<topic_end>
|
<topic_start>
|
Placeholder in a text field
|
In Flutter, you can easily show a “hint” or a placeholder text
|
for your field by adding an InputDecoration object
|
to the decoration constructor parameter for the Text widget:
|
<code_start>Center(
|
child: TextField(
|
decoration: InputDecoration(hintText: 'This is a hint'),
|
),
|
)<code_end>
|
<topic_end>
|
<topic_start>
|
Showing validation errors
|
Just as you would with a “hint”, pass an InputDecoration object
|
to the decoration constructor for the Text widget.However, you don’t want to start off by showing an error.
|
Instead, when the user has entered invalid data,
|
update the state, and pass a new InputDecoration object.
|
<code_start>import 'package:flutter/material.dart';
|
void main() {
|
runApp(const SampleApp());
|
}
|
class SampleApp extends StatelessWidget {
|
const SampleApp({super.key});
|
// This widget is the root of your application.
|
@override
|
Widget build(BuildContext context) {
|
return const MaterialApp(
|
title: 'Sample App',
|
home: SampleAppPage(),
|
);
|
}
|
}
|
class SampleAppPage extends StatefulWidget {
|
const SampleAppPage({super.key});
|
@override
|
State<SampleAppPage> createState() => _SampleAppPageState();
|
}
|
class _SampleAppPageState extends State<SampleAppPage> {
|
String? _errorText;
|
bool isEmail(String em) {
|
String emailRegexp =
|
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|'
|
r'(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|'
|
r'(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
|
RegExp regExp = RegExp(emailRegexp);
|
return regExp.hasMatch(em);
|
}
|
@override
|
Widget build(BuildContext context) {
|
return Scaffold(
|
appBar: AppBar(
|
title: const Text('Sample App'),
|
),
|
body: Center(
|
child: TextField(
|
onSubmitted: (text) {
|
setState(() {
|
if (!isEmail(text)) {
|
_errorText = 'Error: This is not an email';
|
} else {
|
_errorText = null;
|
}
|
});
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.