How to get Text input in TextField on clicking suffix icon of TextField

2,478 views
Skip to first unread message

mahantappa b k

unread,
May 21, 2019, 5:51:06 AM5/21/19
to Flutter Dev
I am having text input filed (see attachment) which shows suffix icon. on clicking suffix icon i want to get text in textfield.
Please can anyone help me out.

My code is below but its not getting the value to text field

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:geolocator/geolocator.dart';
import 'package:flutter_map/flutter_map.dart';

class NoteScreen extends StatefulWidget {
final Note note;
NoteScreen(this.note);
@override
State<StatefulWidget> createState() => new _NoteScreenState();
}
class _NoteScreenState extends State<NoteScreen> {
DatabaseHelper db = new DatabaseHelper();
TextEditingController _titleController;
TextEditingController _descriptionController;
@override
void initState() {
super.initState();
getUserLocation();
_titleController = new TextEditingController(text: widget.note.title);
_descriptionController = new TextEditingController(text: widget.note.description);
}

LatLng _center ;
Position currentLocation;

MapController _mapctl = MapController();


Future<Position> locateUser() async {
try{
return await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}on PlatformException catch(e){
currentLocation = null;
}
return currentLocation;
}

getUserLocation() async {
currentLocation = await locateUser();
setState(() {
_center = LatLng(currentLocation.latitude, currentLocation.longitude);
});
print('center $_center');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Add Plant'),
actions: <Widget>[
new IconButton(
icon: const Icon(Icons.view_list),
tooltip: 'Next choice',
onPressed: () {
navigateToPlantList();
},
),
]
),
body: Container(
margin: EdgeInsets.all(15.0),
alignment: Alignment.center,
child: Column(
children: <Widget>[
TextField(
controller: _descriptionController,
decoration: InputDecoration(
labelText: 'Get current location',
suffixIcon: IconButton(
icon: Icon(Icons.location_on),
onPressed: (){
Text(_center.toString());
print(_center);
}
)),
),
TextField(
controller: _titleController,
decoration: InputDecoration(labelText: 'Title'),
),
Padding(padding: new EdgeInsets.all(5.0)),
TextField(
controller: _descriptionController,
decoration: InputDecoration(labelText: 'Description'),
),
Padding(padding: new EdgeInsets.all(5.0)),
RaisedButton(
child: (widget.note.id != null) ? Text('Update') : Text('Add'),
onPressed: () {
if (widget.note.id != null) {
db.updateNote(Note.fromMap({
'title': _titleController.text,
'description': _descriptionController.text
})).then((_) {
Navigator.pop(context, 'update');
});
}else {
db.saveNote(Note(_titleController.text, _descriptionController.text)).then((_) {
Navigator.pop(context, 'save');
});
}
},
),
],
),
),
);
}
void navigateToPlantList() {
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new ListViewNote()),);
}
}
Screenshot_1558431683.png

Karthik Ponnam

unread,
May 21, 2019, 6:09:50 AM5/21/19
to Flutter Dev
try this inside getUserLocation

setState(() {
    _center
= LatLng(currentLocation.latitude, currentLocation.longitude);

    _descriptionController
.text = _center.toString()
});

mahantappa b k

unread,
May 21, 2019, 6:32:09 AM5/21/19
to Flutter Dev
Thanks Karthik it worked for me.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages