我正在创建应用程序,我在其中使用了 ShowModelBottomSheet,在其中创建了两个选项卡栏 我用这些来编辑我的记录 就像当用户按下编辑按钮时,底部工作表打开并且记录已经存在于文本字段中 但问题是,当我更改文本时,文本会恢复为更新之前的原始文本。 我创建了另一个底表来添加记录,效果很好
这是我的代码,任何人都可以告诉我如何克服
bool _editAdvanceUpArrow = false;
bool _editTextFieldUpArrow = false;
bool _editIncomeTextFieldUpArrow = false;
bool _editIncomeAdvanceUpArrow = false;
bool editIncomeTogle = false;
final editExpenseForm = GlobalKey<FormState>();
final editIncomeForm = GlobalKey<FormState>();
bool editTogle = true;
TextEditingController editLabel = TextEditingController();
TextEditingController editAmount = TextEditingController();
TextEditingController editDescription = TextEditingController();
final FocusNode editLabeFocusNode = FocusNode();
final FocusNode editAmmountFocusNode = FocusNode();
final FocusNode editDescriptionFocusNode = FocusNode();
void editBottomSheet(BudgetRecord record) {
int initialTabIndex = (record.type.toLowerCase() == 'expense') ? 0 : 1;
showModalBottomSheet(
isScrollControlled: true,
backgroundColor: Colors.white,
elevation: 15,
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return DefaultTabController(
length: 2,
initialIndex: initialTabIndex,
child: SingleChildScrollView(
padding: EdgeInsets.only(
top: 10,
bottom: MediaQuery.of(context).viewInsets.bottom,
),
child: Column(
children: [
const SizedBox(
height: 10,
),
Image.asset('assets/bottomSheetBar.png'),
const SizedBox(
height: 20,
),
TabBar(
indicatorSize: TabBarIndicatorSize.tab,
indicatorColor: AppColors.mainColor,
unselectedLabelColor: AppColors.lightTextColor,
tabs: [
Tab(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('assets/expense_tab_logo.png'),
const SizedBox(
width: 10,
),
const Text(
'Expense',
style: TextStyle(color: AppColors.expenseColor),
),
],
),
),
Tab(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('assets/income_tab_logo.png'),
const SizedBox(
width: 10,
),
const Text(
'Income',
style: TextStyle(color: AppColors.incomeColor),
),
],
),
),
],
),
SizedBox(
height: 440,
child: TabBarView(
children: [
editExpenseTabContent(setState, record),
editIncomeTabContent(setState, record),
],
),
),
],
),
),
);
},
);
},
);
}
Widget editExpenseTabContent(StateSetter setState, BudgetRecord record) {
editLabel.text = record.label;
editAmount.text = record.amount;
editDescription.text = record.description;
String formattedDate =
CommonData.formatDateForEdit(record.date ?? record.createdAt);
String formattedTime = CommonData.formatTimeForEdit(
record.date ?? record.createdAt, record.time ?? record.createdAt);
editTogle = record.done;
return Stack(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 28.0, vertical: 65),
child: Form(
key: editExpenseForm,
child: Stack(
children: [
SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 50,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: const BorderRadius.all(
Radius.circular(30),
),
),
child: TextFormField(
scrollPadding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
focusNode: editLabeFocusNode,
validator: (value) {
if (value!.isEmpty) {
return 'Field is required';
}
return null;
},
textInputAction: TextInputAction.done,
controller: editLabel,
onChanged: (value) {
},
keyboardType: TextInputType.name,
cursorColor: AppColors.mainColor,
decoration: InputDecoration(
suffixIcon: IconButton(
onPressed: () {
setState(() {
_editTextFieldUpArrow =
!_editTextFieldUpArrow;
});
},
icon: _editTextFieldUpArrow
? Image.asset('assets/up_arrow.png')
: Image.asset('assets/down_arrow.png')),
labelStyle: const TextStyle(color: Colors.black),
hintText: 'Label',
contentPadding: const EdgeInsets.only(left: 20),
border: const OutlineInputBorder(
borderSide: BorderSide.none,
)),
),
),
const SizedBox(
height: 10,
),
Container(
height: 50,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: const BorderRadius.all(
Radius.circular(30),
),
),
child: TextFormField(
scrollPadding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
focusNode: editAmmountFocusNode,
validator: (value) {
if (value!.isEmpty) {
return 'Field is required';
}
return null;
},
textInputAction: TextInputAction.done,
controller: editAmount,
keyboardType: const TextInputType.numberWithOptions(),
inputFormatters: [
FilteringTextInputFormatter.digitsOnly
],
cursorColor: AppColors.mainColor,
decoration: const InputDecoration(
labelStyle: TextStyle(color: Colors.black),
hintText: 'Amount',
contentPadding: EdgeInsets.only(left: 20),
border: OutlineInputBorder(
borderSide: BorderSide.none,
)),
),
),
const SizedBox(
height: 10,
),
Container(
height: 50,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: const BorderRadius.all(
Radius.circular(30),
),
),
child: TextFormField(
scrollPadding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
focusNode: editDescriptionFocusNode,
controller: editDescription,
keyboardType: TextInputType.name,
cursorColor: AppColors.mainColor,
decoration: const InputDecoration(
labelStyle: TextStyle(color: Colors.black),
hintText: 'Description',
contentPadding: EdgeInsets.only(left: 20),
border: OutlineInputBorder(
borderSide: BorderSide.none,
)),
),
),
const SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: 35,
child: TextButton.icon(
onPressed: () {
setState(() {
_editAdvanceUpArrow = !_editAdvanceUpArrow;
});
},
icon: _expenseAdvanceUpArrow
? Image.asset('assets/up_arrow.png')
: Image.asset('assets/down_arrow.png'),
label: const Text(
'Advance',
style: TextStyle(
color: AppColors.mainColor,
),
)),
),
Row(
children: [
const Text('Paid ?'),
Switch(
activeColor: AppColors.mainColor,
activeTrackColor:
const Color.fromARGB(200, 124, 183, 191),
inactiveThumbColor: AppColors.lightTextColor,
inactiveTrackColor: Colors.grey.shade300,
value: editTogle,
onChanged: (value) {
setState(() {
editTogle = !editTogle;
});
}),
],
),
],
),
_editAdvanceUpArrow
? Column(
children: [
GestureDetector(
onTap: () => _selectDate(context, setState),
child: AbsorbPointer(
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Date'),
controller: TextEditingController(
text: formattedDate),
),
),
),
GestureDetector(
onTap: () => _selectTime(context, setState),
child: AbsorbPointer(
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Time'),
controller: TextEditingController(
text: formattedTime),
),
),
),
],
)
: Container(),
SizedBox(
height: _editAdvanceUpArrow ? 20 : 15,
),
ElevatedButton(
onPressed: () {
final recordd = BudgetRecord(
uuid: record.uuid,
type: 'Expense',
label: editLabel.text,
description: editDescription.text,
amount: editAmount.text,
done: editTogle,
createdAt:
DateTime.now().toUtc().toIso8601String(),
addedBy: UserData().id,
device: Platform.isAndroid ? 'Android' : "IOS",
date: date.isEmpty ? null : date,
time: time.isEmpty ? null : time,
updatedAt:
DateTime.now().toUtc().toIso8601String(),
deletedAt: "");
addEditIRecord(recordd, true);
CommonData.saveDataToMonthlyBudgetPrefrences(
UserData().budget, UserData().email);
setState(() {
_editAdvanceUpArrow = false;
Navigator.of(context).pop();
editLabel.clear();
editAmount.clear();
editDescription.clear();
});
CommonData.showCustomSnackbar(
context, 'Record updated Successfully');
},
style: ElevatedButton.styleFrom(
elevation: 5, backgroundColor: AppColors.mainColor,
minimumSize: const Size.fromHeight(55), // NEW
),
child: const Text(
"Update",
style: TextStyle(color: AppColors.tabsColor),
),
),
],
),
),
],
),
),
),
if (_editTextFieldUpArrow)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25, vertical: 60),
child: Padding(
padding: const EdgeInsets.only(top: 60),
child: Container(
height: 250,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
child: ListView.builder(
itemCount: expenseLabelList.length,
itemBuilder: (context, index) {
final name = expenseLabelList[index];
return Align(
alignment: Alignment.topLeft,
child: GestureDetector(
onTap: () {
setState(() {
editLabel.text = name;
_editTextFieldUpArrow = !_editTextFieldUpArrow;
});
},
child: ListTile(
leading: Text(
name,
style: const TextStyle(
color: AppColors.darkTextColor, fontSize: 15),
)),
),
);
},
),
),
),
)
],
);
}
尽量不要更新 editExpenseTabContent() 内的文本编辑控制器值。 或者为 editExpenseTabContent 创建一个有状态类而不是方法并更新
中的值 @override
void initState() {
super.initState();
}