gpt4 book ai didi

flutter - 如何使用 flutter getx 从 TextEditingController 获取并显示值

转载 作者:行者123 更新时间:2023-12-03 08:03:20 26 4
gpt4 key购买 nike

我正在学习 flutter getx -mvc 模式。很长一段时间以来,我一直在努力使用 Homecontroller 从文本框中检索值,并希望在单击按钮时显示该值。这是我编写的代码。我的总体任务是添加从输入框获取的两个数字并显示。但现在我正在努力显示从输入框获取的数字。

我的家庭 Controller 中有以下代码。


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();
},

将此代码添加到您的文本字段以及其他所有内容中(如果可以的话)。

关于flutter - 如何使用 flutter getx 从 TextEditingController 获取并显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73207823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com