我正在学习flutter getx-mvc模式。长期以来,我一直在努力使用Homecontroller从文本框中检索值,并希望在单击按钮时显示值。这是我写的代码。我的总体任务是将从输入框中获得的两个数字相加并显示。但现在我很难显示从输入框获得的数字。
我的家庭控制器中有以下代码。
import 'package:get/get.dart';
import 'package:getxmvc/Allpackages.dart';
class Homecontroller extends GetxController {
var status = "online".obs;
var followers = 0.obs;
RxString textVal = "a".obs;
final editingController = TextEditingController().obs;
void updateString() {
textVal.value = editingController.value.text;
}
}
I have following code in my main.dart
import 'Allpackages.dart';
import 'src/ProjectSample/Login/Controller/Homecontroller.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final Homecontroller homeCtrl = Get.put(Homecontroller());
@override
Widget build(BuildContext context) {
return GetMaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("MVC Controller"),
),
body: Center(
// child: Addingpage(),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(
width: 300.0,
height: 50.0,
child: TextField(
decoration: InputDecoration(
hintText: "Enter first no",
border: InputBorder.none,
focusedBorder: InputBorder.none,
),
style: TextStyle(
fontSize: 25.0, height: 2.0, color: Colors.black),
),
),
OutlinedButton(
onPressed: () {
homeCtrl.updateString();
},
child: Text('add')),
Obx(
() => Text(
'${homeCtrl.textVal.value}'.toString(),
style: Theme.of(context).textTheme.headline4,
),
),
],
),
),
),
);
}
}
When I click the button nothing is displayed.Can anyone let me know what is happening.
onChanged: (value) {
homeCtrl.textVal.value = value.toString();
},
如果可以的话,将此代码添加到textfield
和其他所有内容中。
在TextField:上添加控制器
TextField(controller: homeCtrl.editingController)
当值更改时,它将自动更新您的TextEditingController.value。