Flutter: Calling function when clicking outside the TextField - flutter

I want to call a function when users tap outside the TextField, after clicking the textfield to unfocus.
return new GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
print('hey');
},
child: Container(
color: Colors.white,
child: TextField(
onChanged: (text) {},
),
),
);
I am trying to use this GestureDetector but it is not working as I would expect. How can I unfocus the textfield by clicking outside of the textfield?

Wrap the whole Scaffold in GestureDetector will do
return GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
},
child: Scaffold(...));

Related

flutter reset form key when with Navigator context change

New to flutter, so maybe quoting the question in a misleading way. Please correct if so.
I have a flutter form that I want to reset when a button (not the InkWell) is pressed. Something like this
Form(
key: formKey,
child: Column(
children: <Widget>[
TextFormField( ),
SizedBox(height: 15),
TextFormField( ),
SizedBox(height: 15),
DateTimePicker( ),
SizedBox(height: 15),
InkWell(
onTap: () {
// navigate to another page, leaving the form as it is,
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SharePage( ),
),
);
}
},
child: Container(...)
),
TextButton(
child : Text('Reset Form'),
onpressed : (){
//doesn't reset the form when the InkWell above is used to navigate to another page and back
formKey.currentState.reset()
}
)
],
),
),
It works normally when the formKey.currentState.reset is called. But when I press the InkWell to go to the SharePage and come back from there, the reset method of the formKey.currentState doesn't resets the form to its initial state, instead it resets the form to the state it was just before going to the SharePage. How do I reset the form to the complete initial state even after coming from the SharePage?
did you try to update
InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SharePage( ),
),
);
}
},
child: Container(...)
),
to be like so,
InkWell(
onTap: () {
/// add following line
formKey.reset
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SharePage( ),
),
);
}
},
child: Container(...)
),
--UPDATE--
to use resetting functionality from anywhere on the screen
TextButton(
child : Text('Reset Form'),
onPressed: () {
formKey.currentState.reset();
},)

Flutter: floating modal sheet

Is there a widget that is kind of like the Modal BottomSheet() where you call using the showModalBottomSheet function, but instead it is not sticking at the bottom but floating in the middle of the screen? like an alert dialog but you can add textField into it.
Do you mean SimpleDialog?
SimpleDialog(
title: Text('Choose food'),
children:[
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, "Pizza"); },
child: const Text('Pizza'),
),
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, "Burger");
},
child: const Text('Burger'),
),
],
elevation: 10,
//backgroundColor: Colors.green,
)

Flutter GestureDetector onTap not working

Trying to make a TextFormField unfocus with a GestureDetector when user taps outside but I can't get it to work. onTap never fires.
class EditScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: new GestureDetector(
onTap: () {
print('this does not fire, why???????????');
// this is my attempt to unfocus textformfield when click away
FocusScope.of(context).requestFocus(new FocusNode());
},
child: SingleChildScrollView(
child: Column(
children: <Widget>[
TextFormField(
maxLines: null,
),
],
),
),
),
);
}
}
Try wrap your Scaffold with Gesture Detector and then onTap function:
onTap: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
So it will fire everytime you tap the scaffold

Changing the Slider Height

I've tried a lot, but I can't change the height of AlertDialog when I use Slider. I saw here on the Forum the suggestion to use ConstrainedBox. Did n't work. Is there a better way to show the Slider?
#override
Widget build(BuildContext context) {
return ConstrainedBox(
constraints: BoxConstraints(maxHeight: 100.0),
child: AlertDialog(
title: Text('Selecione a velocidade'),
content: Container(
child: Slider(
value: _fontSize,
label: _fontSize.round().toString(),
min: 20,
max: 200,
divisions: 18,
onChanged: (value) {
setState(() {
_fontSize = value;
});
},
),
),
actions: <Widget>[
FlatButton(
onPressed: () {
print('cancelar');
// Use the second argument of Navigator.pop(...) to pass
// back a result to the page that opened the dialog
Navigator.pop(context, _fontSize);
},
child: Text('Cancelar'),
),
FlatButton(
onPressed: () {
print('salvar');
// Use the second argument of Navigator.pop(...) to pass
// back a result to the page that opened the dialog
Navigator.pop(context, _fontSize);
},
child: Text('Salvar'),
),
FlatButton(
onPressed: () {
print('aplicar');
// Use the second argument of Navigator.pop(...) to pass
// back a result to the page that opened the dialog
Navigator.pop(context, _fontSize);
},
child: Text('Aplicar'),
),
],
),
);
}
I attached an image showing how AlertDialog looks.
You need to define the height for the Container which outside the Slider.
content: Container(
height:50, // define any height you want here
child: Slider(
This is how the output looked like.
Try this.
Dart pad to show your output
Let me know if it work out for you or not.
Code is here
#override
Widget build(BuildContext context) {
return FittedBox(
child: AlertDialog(
title: Text('Selecione a velocidade'),
content: Container(
child: Slider(
value: _fontSize,
label: _fontSize.round().toString(),
min: 20,
max: 200,
divisions: 18,
onChanged: (value) {
setState(() {
_fontSize = value;
});
},
),
),
actions: <Widget>[
FlatButton(
onPressed: () {
print('cancelar');
// Use the second argument of Navigator.pop(...) to pass
// back a result to the page that opened the dialog
Navigator.pop(context, _fontSize);
},
child: Text('Cancelar'),
),
FlatButton(
onPressed: () {
print('salvar');
// Use the second argument of Navigator.pop(...) to pass
// back a result to the page that opened the dialog
Navigator.pop(context, _fontSize);
},
child: Text('Salvar'),
),
FlatButton(
onPressed: () {
print('aplicar');
// Use the second argument of Navigator.pop(...) to pass
// back a result to the page that opened the dialog
Navigator.pop(context, _fontSize);
},
child: Text('Aplicar'),
),
],
),
);
}
}

show dialog when long pressed and pop it when finger up

I want to show a dialog when user long pressed on an item and pop it when finger up but it can't detect tap up.
I put dialog on another GestureDetector and use onTapUp property of it to pop dialog.
GestureDetector(
child: studentIcon(index, context),
onLongPress: () {
showDialog(
context: context,
builder: (context) {
return GestureDetector(
onTapUp: (detail) {
Navigator.pop(context);
},
child: DialogDetail(
index: index,
),
);
});
},
I expect to pop dialog after finger up after long pressed.
You can't do that as there is an context problem in GestureDetector.
Please follow this answer to implement this thing.
Try to make method to open dialog . i provide alert dialog code..
void _showText(BuildContext context) {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
content: Text(
"User name :${nameEditText.text} \nPassword : ${passwordEditText.text}"),
actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: new Text("OK"))
],
);
});
}
}
after that called on button click..
child: RaisedButton(
padding: EdgeInsets.all(15.0),
onPressed: () {
_showText(context);
},
child: Text(
"Submit",
style: TextStyle(fontSize: 15, color: Colors.white),
),
color: Colors.blue,
),