I'm working on a flutter app and having trouble dismissing a modal (fullscreendialog) view using Navigator.pop. I've also tried Navigator.of(context).pop and no luck there either. I'm sure it's something simple, but I can't seem to figure out what it is. If you scroll the code all the way to the bottom using the drag handle, the Navigator.pop call should be just about in the middle of the window; it's about 25 lines from the bottom. Thanks in advance for your help.
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import '../misc/custom_colors.dart';
import '../misc/validator.dart';
import '../widgets/app_bar_title.dart';
import '../widgets/custom_form_field.dart';
class AddRecord extends StatefulWidget {
// final FocusNode titleFocusNode;
// final FocusNode descriptionFocusNode;
const AddRecord({
Key? key,
}) : super(key: key);
@override
State<AddRecord> createState() => _AddRecordState();
}
class _AddRecordState extends State<AddRecord> {
// overhead
final _formKey = GlobalKey<FormState>();
bool isProcessing = false;
// focusnodes
final FocusNode fnDate = FocusNode();
final FocusNode fnAmount = FocusNode();
final FocusNode fnPayee = FocusNode();
final FocusNode fnAccount = FocusNode();
final FocusNode fnJob = FocusNode();
final FocusNode fnCostCode = FocusNode();
final FocusNode fnMemo = FocusNode();
// text controllers
final TextEditingController _dateController = TextEditingController();
final TextEditingController _amountController = TextEditingController();
final TextEditingController _payeeController = TextEditingController();
final TextEditingController _accountController = TextEditingController();
final TextEditingController _jobController = TextEditingController();
final TextEditingController _costCodeController = TextEditingController();
final TextEditingController _memoController = TextEditingController();
Future recordTransaction(
final String date,
final String amount,
final String payee,
final String account,
final String job,
final String costCode,
final String memo,
) async {
await FirebaseFirestore.instance.collection('transactions').add({
'date': date,
'amount': amount,
'payee': payee,
'account': account,
'job': job,
'cost code': costCode,
'memo': memo,
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
// modify appbar to remove back button before release!
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
title: const AppBarTitle(first: 'New', second: 'Record'),
),
backgroundColor: CustomColors.fbNavy,
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(
left: 16,
right: 16,
bottom: 20,
),
child: Form(
key: _formKey,
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(
left: 8,
right: 8,
bottom: 24,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 24),
CustomFormField(
controller: _dateController,
focusNode: fnDate,
keyboardType: TextInputType.text,
inputAction: TextInputAction.next,
label: 'Transaction Date',
hint: 'Date',
validator: (value) =>
Validator.validateField(value: value),
),
const SizedBox(height: 16),
CustomFormField(
controller: _amountController,
focusNode: fnAmount,
keyboardType: const TextInputType.numberWithOptions(
decimal: true),
inputAction: TextInputAction.next,
label: 'Transaction Amount',
hint: 'Amount',
validator: (value) =>
Validator.validateField(value: value),
),
const SizedBox(height: 16),
CustomFormField(
controller: _payeeController,
focusNode: fnPayee,
keyboardType: TextInputType.text,
inputAction: TextInputAction.next,
label: 'Payee',
hint: 'Payee name',
validator: (value) =>
Validator.validateField(value: value),
),
const SizedBox(height: 16),
CustomFormField(
controller: _accountController,
focusNode: fnAccount,
keyboardType: TextInputType.text,
inputAction: TextInputAction.next,
label: 'Paid-from Account',
hint: 'Account',
validator: (value) =>
Validator.validateField(value: value),
),
const SizedBox(height: 16),
CustomFormField(
controller: _jobController,
focusNode: fnJob,
keyboardType: TextInputType.text,
inputAction: TextInputAction.next,
label: 'Job',
hint: 'Job (optional)',
validator: (value) =>
Validator.validateField(value: value),
),
const SizedBox(height: 16),
CustomFormField(
controller: _costCodeController,
focusNode: fnCostCode,
keyboardType: TextInputType.text,
inputAction: TextInputAction.next,
label: 'Cost Code',
hint: 'Cost Code',
validator: (value) =>
Validator.validateField(value: value),
),
const SizedBox(height: 16),
CustomFormField(
maxLines: 5,
controller: _memoController,
focusNode: fnMemo,
keyboardType: TextInputType.text,
inputAction: TextInputAction.done,
label: 'Memo',
hint: 'Memo (Optional)',
validator: (value) =>
Validator.validateField(value: value),
),
],
),
),
isProcessing
? const Padding(
padding: EdgeInsets.all(16),
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
CustomColors.fbOrange,
),
),
)
: SizedBox(
width: double.maxFinite,
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
CustomColors.fbOrange,
),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
onPressed: () {
fnDate.unfocus();
fnAmount.unfocus();
fnPayee.unfocus();
fnAccount.unfocus();
fnJob.unfocus();
fnCostCode.unfocus();
fnMemo.unfocus();
if (_formKey.currentState!.validate()) {
setState(() {
isProcessing = true;
});
recordTransaction(
_dateController.text.trim(),
_amountController.text.trim(),
_payeeController.text.trim(),
_accountController.text.trim(),
_jobController.text.trim(),
_costCodeController.text.trim(),
_memoController.text.trim(),
);
setState(() {
isProcessing = false;
});
Navigator.of(context).pop;
}
},
child: const Padding(
padding: EdgeInsets.only(top: 16, bottom: 16),
child: Text(
'Add Transaction',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: CustomColors.fbGrey,
letterSpacing: 2,
),
),
),
),
),
],
),
),
),
),
);
}
}
CodePudding user response:
You missed () try
onPressed: () {
Navigator.pop(context);
}