Flutter 字符串列表:如何在 Text() 函数中添加字符串列表
Flutter list of String: How to add list of string in Text() functio
我正在为我的 Quizzler 应用构建问题列表,想逐一访问问题,但无法将我的 questions
列表添加到我的 Text
功能中app...那么我应该如何将我的字符串列表添加到 Text
以便将它们一一显示在屏幕上?
enter image description here
class QuizPage extends StatefulWidget {
@override
_QuizPageState createState() => _QuizPageState();
}
class _QuizPageState extends State<QuizPage> {
List<Widget> scorekeeper = [];
List<String> questions = [
'You can lead a cow down stairs but not up stairs.',
'Approximately one quarter of human bones are in the feet.',
'A slug\'s blood is green.'
];
int questionNum = 0;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const Expanded(
flex: 3,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
questions[questionNum],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
您可以这样使用 ListView.builder
:
ListView.builder(
itemCount: questions.length,
itemBuilder: (_, i) {
return Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
questions[i],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
)
)
);
}
);
并删除关键字 const
。
我正在为我的 Quizzler 应用构建问题列表,想逐一访问问题,但无法将我的 questions
列表添加到我的 Text
功能中app...那么我应该如何将我的字符串列表添加到 Text
以便将它们一一显示在屏幕上?
enter image description here
class QuizPage extends StatefulWidget {
@override
_QuizPageState createState() => _QuizPageState();
}
class _QuizPageState extends State<QuizPage> {
List<Widget> scorekeeper = [];
List<String> questions = [
'You can lead a cow down stairs but not up stairs.',
'Approximately one quarter of human bones are in the feet.',
'A slug\'s blood is green.'
];
int questionNum = 0;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const Expanded(
flex: 3,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
questions[questionNum],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
您可以这样使用 ListView.builder
:
ListView.builder(
itemCount: questions.length,
itemBuilder: (_, i) {
return Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
questions[i],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
)
)
);
}
);
并删除关键字 const
。