text
stringlengths 1
372
|
|---|
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 {
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.