I'm using a fullscreen dialog in my app to make the user input values into text fields. Some of these values are Strings and some are ints. So I want to change the keyboard accordingly to make the unnecessary keys disappear. Here's my implementation:
import 'package:flutter/material.dart';
import 'package:meal_tracker/model/food_model.dart';
class AddFoodDialog extends StatelessWidget {
TextEditingController nameController = TextEditingController(),
carbsController = TextEditingController(),
fatController = TextEditingController(),
proteinController = TextEditingController(),
amountController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Add Food"),
actions: <Widget>[
FlatButton(
child: Text(
"SAVE",
style: TextStyle(color: Colors.white),
),
onPressed: () {
_handleSave(context);
},
)
],
),
body: Form(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: "Name"),
controller: nameController,
),
TextFormField(
decoration: InputDecoration(labelText: "Carbs per 100g"),
controller: carbsController,
keyboardType: TextInputType.number,
),
TextFormField(
decoration: InputDecoration(labelText: "Fat per 100g"),
controller: fatController,
keyboardType: TextInputType.number,
),
TextFormField(
decoration: InputDecoration(labelText: "Protein per 100g"),
controller: proteinController,
keyboardType: TextInputType.number,
),
TextFormField(
decoration: InputDecoration(labelText: "Amount in g"),
controller: amountController,
keyboardType: TextInputType.number,
),
],
),
)),
);
}
void _handleSave(BuildContext context) {
print("Dialog text: " + nameController.text);
Navigator.of(context).pop(Food(
name: nameController.text,
carbsPer100g: double.parse(carbsController.text),
fatPer100g: double.parse(fatController.text),
proteinPer100g: double.parse(proteinController.text),
amount: double.parse(amountController.text)));
}
}
The problem is that when I input something in the "Name" TextField, which has TextInputType.text
and then tap into another one of the TextFields, which has TextInputType.number
, the "Name" field clears out for no apparent reason. That also happens the other way around.
Also, as you already mentioned, you need to configure an AutoFill service in the system settings. Working sample code: @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text("Login"), ), body: AutofillGroup( child: Column( children: [ TextField( autofillHints: [AutofillHints.
To remove TextField underline/border in Flutter, you can simply set the border property to InputBorder. none. This will remove the underline for all states such as Focused, Enabled, Error, Disabled.
I had to put the TextEditingControllers inside a Stateful Widget, that did end up working. Thanks to Jonah Williams for the fast reply!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With