Reputation: 139
The above image is the TextField. My email address is "[email protected]". After unfocused, I need to display the email from starting like below image. Is there anyway to achieve this easily in flutter?
For example : Refer gmail app.
Upvotes: 0
Views: 354
Reputation: 9019
You can set selection
on your TextEditingController
when you're unfocusing your TextField
. Here is an example:
import 'package:flutter/material.dart';
class TestScreen extends StatefulWidget {
@override
_TestScreenState createState() => _TestScreenState();
}
class _TestScreenState extends State<TestScreen> {
TextEditingController _textEditingController = TextEditingController();
FocusNode _focusNode = FocusNode();
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 48),
child: Column(
children: <Widget>[
Container(height: 16),
TextField(
controller: _textEditingController,
focusNode: _focusNode,
),
SizedBox(height: 36),
FlatButton(
onPressed: () async {
_textEditingController.selection =
TextSelection.fromPosition(TextPosition(offset: 0));
await Future.delayed(Duration(milliseconds: 100));
_focusNode.unfocus();
},
color: Colors.red,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
textColor: Colors.white,
child: Text('Unfocus'),
),
],
crossAxisAlignment: CrossAxisAlignment.stretch,
),
),
),
);
}
}
Upvotes: 1