Flutter自定义Widget传入参数后无法展示数据问题咨询
解决方法
你的问题核心很明确:自定义的studentText Widget虽然接收了学号、姓名、姓氏参数,但返回的Container里没有任何用于展示文本的子组件,导致传入的数据根本不会被渲染出来。
只需要修改studentText函数,在返回的Container中添加子组件来承载并显示这些参数即可。另外注意页面背景是深色,要把文本颜色设为浅色(比如白色),避免因颜色相近看不见内容:
Container studentText(int schoolNumber, String name, String surname) { return Container( width: double.infinity, // 建议调整宽度,避免文本被截断 height: 50, alignment: Alignment.center, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( '$schoolNumber', style: const TextStyle(color: Colors.white, fontSize: 16), ), Text( '$name $surname', style: const TextStyle(color: Colors.white, fontSize: 14), textAlign: TextAlign.center, ), ], ), ); }
如果希望横向排列数据,可将Column替换为Row并调整布局:
Container studentText(int schoolNumber, String name, String surname) { return Container( width: double.infinity, height: 50, alignment: Alignment.centerLeft, child: Row( children: [ Text( '$schoolNumber', style: const TextStyle(color: Colors.white, fontSize: 16), ), const SizedBox(width: 15), // 添加参数间的间距 Text( '$name $surname', style: const TextStyle(color: Colors.white, fontSize: 14), ), ], ), ); }
修改后重新运行,传入的学号、姓名和姓氏就会正常显示在页面上了。
内容的提问来源于stack exchange,提问作者ckot
相关产品推荐
相关产品推荐

