我在我的应用程序中使用全屏对话框,以便用户将值输入到文本字段中。其中一些值是字符串,一些是整数。因此,我希望相应更改键盘,以使不必要的键消失。这是我的实现:
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)));
}
}
问题在于当我在“名称”文本框中输入一些内容,该文本框具有 TextInputType.text
,然后点击另一个具有TextInputType.number
的文本框时,“名称”字段出现了不明原因的清空。这种情况也会反向发生。