text
stringlengths
1
372
see retrieve the value of a text field,
from the flutter cookbook.
<topic_end>
<topic_start>
what is the equivalent of a “hint” on an input?
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>
center(
child: TextField(
decoration: InputDecoration(hintText: 'this is a hint'),
),
)
<code_end>
<topic_end>
<topic_start>
how do i show 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 MaterialApp(
title: 'sample app',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: const SampleAppPage(),
);
}
}
class SampleAppPage extends StatefulWidget {
const SampleAppPage({super.key});
@override
State<SampleAppPage> createState() => _SampleAppPageState();
}
class _SampleAppPageState extends State<SampleAppPage> {
string? _errorText;
@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;
}
});
},
decoration: InputDecoration(
hintText: 'this is a hint',
errorText: _getErrorText(),
),
),
),
);
}
string? _getErrorText() {
return _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);
}
}
<code_end>
<topic_end>
<topic_start>
flutter plugins
<topic_end>
<topic_start>
how do i access the GPS sensor?
use the geolocator community plugin.
<topic_end>
<topic_start>
how do i access the camera?
the image_picker plugin is popular
for accessing the camera.
<topic_end>