虽然它没有告诉我有错误,但名字和姓氏没有出现在我的 QRPageRegistered 中
这是我的 ManifestInformationWidgets.dart 代码,我在其中获取名字和姓氏。
import 'package:flutter/material.dart';
import 'package:namer_app/Passenger_Widgets/PRFAlertDialog.dart';
import 'package:namer_app/Passenger_Widgets/DropBoxes.dart';
import 'package:philippines_rpcmb/philippines_rpcmb.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:namer_app/Passenger_Widgets/CustomTextboxes.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'QRPageRegistered.dart';
class TextBoxContainer extends StatefulWidget {
final SharedPreferences prefs;
const TextBoxContainer({Key? key, required this.prefs}) : super(key: key);
@override
_TextBoxContainerState createState() => _TextBoxContainerState();
}
class _TextBoxContainerState extends State<TextBoxContainer> {
final _formKey = GlobalKey<FormState>();
bool isEditing = false;
bool isEnabled = false;
Region? region;
Province? province;
Municipality? municipality;
String? barangay;
String? selectedGender;
late TextEditingController _firstNameController;
late TextEditingController _middleInitialController;
late TextEditingController _lastNameController;
late TextEditingController _contactNumberController;
late TextEditingController _professionController;
late TextEditingController _ageController;
@override
void initState() {
super.initState();
_firstNameController =
TextEditingController(text: widget.prefs.getString('firstName') ?? '');
_middleInitialController = TextEditingController(
text: widget.prefs.getString('middleInitial') ?? '');
_lastNameController =
TextEditingController(text: widget.prefs.getString('lastName') ?? '');
_contactNumberController = TextEditingController(
text: widget.prefs.getString('contactNumber') ?? '');
_professionController =
TextEditingController(text: widget.prefs.getString('profession') ?? '');
_ageController =
TextEditingController(text: widget.prefs.getString('age') ?? '');
selectedGender = widget.prefs.getString('gender');
}
@override
void dispose() {
_firstNameController.dispose();
_middleInitialController.dispose();
_lastNameController.dispose();
_contactNumberController.dispose();
_professionController.dispose();
_ageController.dispose();
super.dispose();
}
void _enableEditing() async {
if (!isEditing) {
PRFAlertDialog(context, (success) {
if (success) {
setState(() {
isEditing = true;
isEnabled = true;
});
}
});
} else if (isEditing) {
if (_formKey.currentState?.validate() ?? false) {
final response = await Supabase.instance.client
.from('User')
.update({
'first_name': _firstNameController.text,
'middle_initial': _middleInitialController.text,
'last_name': _lastNameController.text,
'phone': _contactNumberController.text,
'profession': _professionController.text,
'age': _ageController.text,
})
.eq('email', widget.prefs.getString('email') ?? '')
.select();
if (response.isNotEmpty) {
setState(() {
isEditing = false;
isEnabled = false;
});
}
}
}
}
void handleGenderSelected(String? gender) {
setState(() {
selectedGender = gender;
});
}
void navigateToQRPageRegistered() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomePage(
firstName: _firstNameController,
lastName: _lastNameController,
),
),
);
}
@override
Widget build(BuildContext context) {
return Container(
child: Form(
key: _formKey,
child: Column(
children: [
SizedBox(height: 40),
Padding(
padding: const EdgeInsets.only(left: 40),
child: Row(
children: [
MIWCustomTextBox(
textController: _firstNameController,
isEnabled: isEnabled,
hintText: 'Enter firstname',
labelText: 'First name',
containerWidth: 190,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your first name';
}
return null;
},
),
SizedBox(width: 10),
MIWCustomTextBox(
textController: _middleInitialController,
isEnabled: isEnabled,
hintText: 'Enter MI',
labelText: 'Middle Initial',
containerWidth: 80,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your middle initial';
}
return null;
},
),
],
),
),
SizedBox(height: 20),
MIWCustomTextBox(
textController: _lastNameController,
isEnabled: isEnabled,
hintText: 'Enter Surname',
labelText: 'Surname',
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your surname';
}
return null;
}),
SizedBox(height: 20),
LocationDropdowns(
isEnabled: isEnabled,
onRegionChanged: (selectedRegion) {
setState(() {
region = selectedRegion;
province = null;
municipality = null;
barangay = null;
});
},
onProvinceChanged: (selectedProvince) {
setState(() {
province = selectedProvince;
municipality = null;
barangay = null;
});
},
onMunicipalityChanged: (selectedMunicipality) {
setState(() {
municipality = selectedMunicipality;
barangay = null;
});
},
onBarangayChanged: (selectedBarangay) {
setState(() {
barangay = selectedBarangay;
});
},
region: region,
province: province,
municipality: municipality,
barangay: barangay,
),
SizedBox(height: 20),
MIWCustomTextBox(
textController: _contactNumberController,
isEnabled: isEnabled,
hintText: 'Enter Contact Number',
labelText: 'Contact Number',
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your contact number';
}
return null;
}),
SizedBox(height: 20),
MIWCustomTextBox(
textController: _professionController,
isEnabled: isEnabled,
hintText: 'Enter Profession',
labelText: 'Profession',
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your profession';
}
return null;
}),
SizedBox(height: 20),
Padding(
padding: EdgeInsets.only(left: 40),
child: Row(
children: [
MIWCustomTextBox(
textController: _ageController,
isEnabled: isEnabled,
hintText: 'Enter Age',
labelText: 'Age',
containerWidth: 135,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your age';
}
return null;
},
),
SizedBox(width: 10),
GenderDropBox(
onGenderChanged: handleGenderSelected,
isEnabled: isEnabled,
selectedGender: selectedGender,
),
],
),
),
SizedBox(height: 40),
EditButton(
isEditing: isEditing,
toggleEditing: _enableEditing,
),
],
),
),
);
}
}
class EditButton extends StatelessWidget {
final bool isEditing;
final VoidCallback toggleEditing;
const EditButton(
{Key? key, required this.isEditing, required this.toggleEditing})
: super(key: key);
@override
Widget build(BuildContext context) {
return Container(
width: 280,
height: 50,
child: ElevatedButton(
onPressed: toggleEditing,
child: Text(isEditing ? "CONFIRM" : "EDIT"),
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xFF4FCAD2),
textStyle: TextStyle(
color: Color(0xFF33385F),
fontWeight: FontWeight.bold,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
),
),
);
}
}
这是我的 QRPageRegistered.dart 代码,我希望在其中显示名字和姓氏:
import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';
class HomePage extends StatefulWidget {
final String? origin;
final String? destination;
final TextEditingController? firstName;
final TextEditingController? lastName;
HomePage({
super.key,
this.origin,
this.destination,
this.firstName,
this.lastName,
});
@override
HomePageState createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('E-Ticket'),
backgroundColor: const Color(0xFF7ADFC1),
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
),
),
body: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Color(0xFF7ADFC1), Color(0xFF21A5D1)],
),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 350,
height: 550,
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(35),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
QrImageView(
data: widget.destination ?? '',
version: QrVersions.auto,
size: 300.0,
),
const SizedBox(height: 20),
Text(
'${widget.firstName ?? ''} ${widget.lastName ?? ''}',
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
const Text(
'Student',
style: TextStyle(
fontSize: 16,
color: Colors.grey,
),
),
const SizedBox(height: 20),
Text(
'${widget.origin ?? ''} - ${widget.destination ?? ''}',
style: const TextStyle(
fontSize: 16,
),
),
const SizedBox(height: 10),
const Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Total Fee:',
style: TextStyle(
fontSize: 25,
),
),
Text(
'₱0.00',
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
],
),
],
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
showConfirmationDialog(context);
},
child: const Text('Complete Transaction'),
),
],
),
),
),
);
}
void showConfirmationDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Are you sure?'),
content: const Text('Do you want to complete the transaction?'),
actions: [
TextButton(
child: const Text('No'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: const Text('Yes'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
我一直在尝试编辑主页部分中的构造函数以及两个代码上的参数名称,但它要么显示错误,要么在 QRPageRegistered 中不显示任何内容。
您正在使用
最终的文本编辑控制器?名; 最终的文本编辑控制器?姓氏;
如果您需要使用 TextEditingController,您需要使用firstName.text 但如果你不需要TextEditingController,你可以将firstName和lastName的类型更改为String类型并使用它。