我完全是个新手。
我想要做的是(通过TextController)将数据从StatefulWidget传递给另一个数据。
这是我的代码(被动Widget)
import 'package:flutter/material.dart';
class VocabularyText extends StatefulWidget {
final String text;
// ignore: sort_constructors_first
const VocabularyText ({ Key key, this.text }): super(key: key);
@override
_VocabularyTextState createState() => _VocabularyTextState();
}
class _VocabularyTextState extends State<VocabularyText> {
Offset offset = Offset.zero;
@override
Widget build(BuildContext context) {
return Container(
child: Positioned(
left: offset.dx,
top: offset.dy,
child: GestureDetector(
onPanUpdate: (details) {
setState(() {
offset = Offset(
offset.dx + details.delta.dx, offset.dy + details.delta.dy);
});
},
child: const SizedBox(
width: 300,
height: 300,
child: Padding(
padding: EdgeInsets.all(8),
child: Center(
child: Text(
'a',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 28,
color: Colors.red
)
),
),
),
)),
),
);
}
}东西就在这里
child: Text(
//
widget.text,
//
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 28,
color: Colors.red
)
),根据我的研究,这应该是可行的,但它不起作用。为什么我会犯错?
以下是参考资料
提前谢谢你。
编辑


发布于 2020-07-16 23:43:44
我回答说,在看到图像之前,它没有在那里,在看到图像之后,是什么导致了这个问题
widget.text在文本小部件中使用它的正确方法如下
Text('${widget.text}'),我建议你做下面的事
要首先将数据发送到,您可以使用Navigator或任何其他方法向用户打开这个小部件,如下所示
return GestureDetector(
onTap: () => {
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>
//note here between the () you pass the variable
Categories(companies[index].id, companies[index].name)),
)},然后,为了接收它们,您会这样做,在我的例子中,它的类别类
class Categories extends StatefulWidget {
//getting company id from home page
final int companyId;
final companyName;
Categories(this.companyId , this.companyName);
@override
_CategoriesState createState() => _CategoriesState();
}
class _CategoriesState extends State<Categories> {
@override
Widget build(BuildContext context) {
...... rest of the code现在,要使用像这样的数据,例如
widget.companyId这是我的代码中的一个示例,现在让我们跳到您的代码
若要从文本编辑控制器接收文本,请执行以下操作
class TextReceiver extends StatefulWidget {
//getting company id from home page
final String userInput;
TextReceiver(this.userInput);
@override
TextReceiver createState() => _TextReceiver();
}
//to use it
widget.userInput现在要发送它,您通过发送它
onTap: () => {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => TextReceiver(TextEditingController.text)),
)},注意,您应该将它作为TextEditingController.text传递,因为TextReceiver中的构造函数指定要字符串的类型,如果您传递了TextEditingController,那么类型将不是String,而是TextEditingController类型
例如,所有这些代码都不像您的代码,但是它会给您提供这样的想法
参考官方文档https://flutter.dev/docs/cookbook/navigation/passing-data
编辑:从该行中删除const
child: const SizedBox(
rest of the code
)
to this
child: SizedBox(
rest of the code
)https://stackoverflow.com/questions/62944452
复制相似问题