flutter - 我们如何从 Flutter 中的自定义小部件中获取价值
问题描述
我想将 TextBox 类中的文本值访问到其他 dart 文件,怎么做?
我已经给出了下面的代码
import 'package:flutter/material.dart';
class TextBox extends StatelessWidget {
final String hint;
final String text;
TextBox({this.hint});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 0.0,horizontal: 10.0),
child: TextField(
decoration: InputDecoration(
hintText: hint,
contentPadding: EdgeInsets.symmetric(vertical: 10.0,horizontal: 10.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
),
),
onChanged: (value){
text=value;
return text;
},
),
);
}
}
解决方案
你需要state management
考虑到。目前您的TextBox
小部件不保持任何状态。
有不同的方法可以做到这一点,您可以检查Flutter 中的状态管理以进行概述。
一般来说(实际实现很大程度上取决于您最终选择的内容),您想要“提升状态”。
引用文档中还说明的内容:
In Flutter, it makes sense to keep the state above the widgets that use it.
推荐阅读
- javascript - react-responsive-carousel 没有正确显示
- android - 移动调试开发环境
- amazon-web-services - 使用什么 AWS 服务作为 OAUTH2 与微服务一起使用
- javascript - 为什么 DOM 我的事件不工作并且样式改变?
- php - 根据 id 从同一个 MySql 表中选择
- kubernetes - 无法访问 Kubenetes 负载均衡器
- r - geom_point() 中的缺失值
- prometheus - PromQL 查询中的动态阈值(组过滤器中使用的两个标签
- python - 根据列表中包含字符串的名称选择熊猫数据框列
- c# - 使用 JWT 在 Web API 的每个请求中设置声明主体