我正在尝试在 flutter 中制作一个表单,这里我使用带有 Getx 控制器的 Flutter Stateless 小部件,但是当我尝试添加单选列表图块时,仅获得白色空白输出,如果我评论该部分,使用文本数据获取渐变背景。
myProfile.dart:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:money_transfer/controller/profileController.dart';
class MyProfile extends StatelessWidget {
MyProfile({super.key});
final controller = Get.find<ProfileController>();
// final controller = Get.put(ProfileController());
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.red.withOpacity(0.4),
Colors.yellowAccent.withOpacity(0.5),
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text("data"),
Form(
key: controller.globalFormKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RadioListTile(
value: "Mr",
groupValue: controller.title.value,
onChanged: (String? value) {
controller.setGenderRadio(value);
},
),
RadioListTile(
value: "Ms",
groupValue: controller.title.value,
onChanged: (String? value) {
controller.setGenderRadio(value);
},
),
RadioListTile(
value: "Mrs",
groupValue: controller.title.value,
onChanged: (String? value) {
controller.setGenderRadio(value);
},
),
],
),
],
),
),
],
),
),
),
);
}
}
profileController.dart:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class ProfileController extends GetxController{
GlobalKey<FormState> globalFormKey = GlobalKey<FormState>();
RxString title="Mr".obs;
String? firstName;
String? middleName;
String? LastName;
String? mob;
String? email;
String? postalCode;
String? town;
String? country;
String? street;
String? buildingNo;
String? dateOfBirth;
setGenderRadio(String? val){
title.value = val.toString();
}
}
在main.dart中:
Get.put(ProfileController());
也在那里。
我在代码中发现了问题,我使用RadioListTile小部件,并尝试放入行而不是列,为了解决问题并放入行,我使用RadioMenuButton而不是RadioListTile。这是我更新的代码:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:money_transfer/controller/profileController.dart';
class MyProfile extends StatelessWidget {
MyProfile({super.key});
final controller = Get.find<ProfileController>();
// final controller = Get.put(ProfileController());
GlobalKey<FormState> globalFormKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.red.withOpacity(0.4),
Colors.yellowAccent.withOpacity(0.5),
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Obx(
() => Column(
children: [
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RadioMenuButton(
value: "Mr",
groupValue: controller.title.value,
onChanged: (String? value) {
controller.setGenderRadio(value);
},
child: Text("Mr"),
),
RadioMenuButton(
value: "Ms",
groupValue: controller.title.value,
onChanged: (String? value) {
controller.setGenderRadio(value);
},
child: Text("Ms"),
),
RadioMenuButton(
value: "Mrs",
groupValue: controller.title.value,
onChanged: (String? value) {
controller.setGenderRadio(value);
},
child: Text("Mrs"),
),
// RadioListTile(
// value: "Mr",
// groupValue: controller.title.value,
// onChanged: (String? value) {
// controller.setGenderRadio(value);
// },
// ),
// RadioListTile(
// value: "Ms",
// groupValue: controller.title.value,
// onChanged: (String? value) {
// controller.setGenderRadio(value);
// },
// ),
// RadioListTile(
// value: "Mrs",
// groupValue: controller.title.value,
// onChanged: (String? value) {
// controller.setGenderRadio(value);
// },
// ),
],
),
),
],
),
),
),
),
);
}
}