text stringlengths 1 474 |
|---|
<topic_end> |
<topic_start> |
How do I retrieve user input? |
Xamarin.Forms elements allow you to directly query the element |
to determine the state of its properties, |
or whether it’s bound to a property in a ViewModel.Retrieving information in Flutter is handled by specialized widgets |
and is different from how you are used to. |
If you have a TextFieldor a TextFormField, |
you can supply a TextEditingController |
to retrieve user input: |
<code_start>import 'package:flutter/material.dart'; |
class MyForm extends StatefulWidget { |
const MyForm({super.key}); |
@override |
State<MyForm> createState() => _MyFormState(); |
} |
class _MyFormState extends State<MyForm> { |
/// Create a text controller and use it to retrieve the current value |
/// of the TextField. |
final TextEditingController myController = TextEditingController(); |
@override |
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 that the user has typed into our text field. |
onPressed: () { |
showDialog( |
context: context, |
builder: (context) { |
return AlertDialog( |
// Retrieve the text that the user has entered using the |
// 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> |
What is the equivalent of a Placeholder on an Entry? |
In Xamarin.Forms, some Elements support a Placeholder property |
that you can assign a value to. For example:In Flutter, you can easily show a “hint” or a placeholder text |
for your input by adding an InputDecoration object |
to the decoration constructor parameter for the text widget. |
<code_start>TextField( |
decoration: InputDecoration(hintText: 'This is a hint'), |
),<code_end> |
<topic_end> |
<topic_start> |
How do I show validation errors? |
With Xamarin.Forms, if you wished to provide a visual hint of a |
validation error, you would need to create new properties and |
VisualElements surrounding the Elements that had validation errors.In Flutter, you pass through 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 { |
/// This widget is the root of your application. |
const SampleApp({super.key}); |
@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; |
String? _getErrorText() { |
return _errorText; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.