Flutter 颤振:-如何在屏幕上显示动态小部件?
我想以混乱的形式显示输入的文本。也就是说,单词的每个字母都需要显示在一行中的单个容器中。为此,我将获取文本输入,将其存储在列表中,然后使用shuffle对其进行置乱,然后使用List.generate返回包含文本的容器,如下所示:Flutter 颤振:-如何在屏幕上显示动态小部件?,flutter,Flutter,我想以混乱的形式显示输入的文本。也就是说,单词的每个字母都需要显示在一行中的单个容器中。为此,我将获取文本输入,将其存储在列表中,然后使用shuffle对其进行置乱,然后使用List.generate返回包含文本的容器,如下所示: List<Widget> _generateJumble(String input) { inputList = input.split(''); var shuffleList = inputList.toList()..shuffle();
List<Widget> _generateJumble(String input) {
inputList = input.split('');
var shuffleList = inputList.toList()..shuffle();
print(shuffleList);
return List<Widget>.generate(shuffleList.length, (int index) {
return Container(
width: 50,
color: Colors.blue,
child: Text(shuffleList[index].toString(),
style: TextStyle(color: Colors.white),
)
);
});
}
这是我的解决方案:
1按下按钮,清除字符串并将其设置为a列表
2设置状态并向用户显示列表
这是小部件代码:
类_MyHomePageState扩展状态{
列表输入列表=[];
@凌驾
小部件构建上下文上下文{
返回脚手架
appBar:appBar
标题:Textwidget.title,
,
主体:包裹
子项:inputList.maps{
返回容器
宽度:50,
颜色:颜色,蓝色,
子:文本
s
样式:text样式颜色:Colors.white,
,
;
}托利斯特先生,
,
浮动操作按钮:浮动操作按钮
按下按钮:{
设定状态{
_生成伞形“随机字符串”;
};
},
工具提示:“增量”,
子项:IconIcons.add,
,
;
}
列表_生成UMBLESTRING输入{
inputList=input.split;
inputList=inputList.toList..shuffle;
打印输入列表;
}
}
我使用了widget Wrap,因为当没有可用空间时,它会自动包装widget。你想用什么就用什么
这是屏幕结果:
按下按钮前:
按下按钮后:
这是我的解决方案:
1按下按钮,清除字符串并将其设置为a列表
2设置状态并向用户显示列表
这是小部件代码:
类_MyHomePageState扩展状态{
列表输入列表=[];
@凌驾
小部件构建上下文上下文{
返回脚手架
appBar:appBar
标题:Textwidget.title,
,
主体:包裹
子项:inputList.maps{
返回容器
宽度:50,
颜色:颜色,蓝色,
子:文本
s
样式:text样式颜色:Colors.white,
,
;
}托利斯特先生,
,
浮动操作按钮:浮动操作按钮
按下按钮:{
设定状态{
_生成伞形“随机字符串”;
};
},
工具提示:“增量”,
子项:IconIcons.add,
,
;
}
列表_生成UMBLESTRING输入{
inputList=input.split;
inputList=inputList.toList..shuffle;
打印输入列表;
}
}
我使用了widget Wrap,因为当没有可用空间时,它会自动包装widget。你想用什么就用什么
这是屏幕结果:
按下按钮前:
按下按钮后:
请检查下面的解决方案,我已经使用了Wrap小部件
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutterlearningapp/colors.dart';
class HomeScreen extends StatefulWidget {
var inputVales;
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _HomeScreen();
}
}
class _HomeScreen extends State<HomeScreen> {
List<String> charcaterArray = new List<String>();
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Home"),
),
body: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: TextField(
decoration: InputDecoration(labelText: 'Enter Words'),
onChanged: (text) {
setState(() {
widget.inputVales = text;
charcaterArray.clear();
for (var i = 0; i < widget.inputVales.length; i++) {
var character = widget.inputVales[i];
if (character != " ") {
charcaterArray.add(character);
}
}
});
},
),
),
Wrap(
spacing: 6.0,
runSpacing: 6.0,
children:
List<Widget>.generate(charcaterArray.length, (int index) {
return Container(
height: MediaQuery.of(context).size.height * 0.1,
width: MediaQuery.of(context).size.width * 0.1,
decoration: BoxDecoration(
color: Colors.lightGreen,
borderRadius: BorderRadius.all(Radius.elliptical(4.0, 4.0)),
),
child: Center(
child: Text(
charcaterArray[index],
style:
TextStyle(color: Colors.deepOrange, fontSize: 20.0),
),
),
);
/*Chip(
label: Text(charcaterArray[index]),
onDeleted: () {
setState(() {
charcaterArray.removeAt(index);
});
},
);*/
}),
)
],
));
}
}
这是它的输出
请检查下面的解决方案,我已经使用了Wrap小部件
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutterlearningapp/colors.dart';
class HomeScreen extends StatefulWidget {
var inputVales;
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _HomeScreen();
}
}
class _HomeScreen extends State<HomeScreen> {
List<String> charcaterArray = new List<String>();
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Home"),
),
body: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: TextField(
decoration: InputDecoration(labelText: 'Enter Words'),
onChanged: (text) {
setState(() {
widget.inputVales = text;
charcaterArray.clear();
for (var i = 0; i < widget.inputVales.length; i++) {
var character = widget.inputVales[i];
if (character != " ") {
charcaterArray.add(character);
}
}
});
},
),
),
Wrap(
spacing: 6.0,
runSpacing: 6.0,
children:
List<Widget>.generate(charcaterArray.length, (int index) {
return Container(
height: MediaQuery.of(context).size.height * 0.1,
width: MediaQuery.of(context).size.width * 0.1,
decoration: BoxDecoration(
color: Colors.lightGreen,
borderRadius: BorderRadius.all(Radius.elliptical(4.0, 4.0)),
),
child: Center(
child: Text(
charcaterArray[index],
style:
TextStyle(color: Colors.deepOrange, fontSize: 20.0),
),
),
);
/*Chip(
label: Text(charcaterArray[index]),
onDeleted: () {
setState(() {
charcaterArray.removeAt(index);
});
},
);*/
}),
)
],
));
}
}
这是它的输出
字数是固定的还是动态的?@RavindraKushwaha dynamic。用户可以输入任何文本。就像用户在EdditText中输入RAVINDRA KUSHWAHA一样,我们需要在容器中解除播放,对吗?是的。每个容器中的每个字母都是连续的。好吧,兄弟,让我们试试,给我时间:单词的数量是固定的还是动态的?@RavindraKushwaha dynamic。用户可以输入任何文本。就像用户在EdditText中输入RAVINDRA KUSHWAHA一样,我们需要在容器中解除播放,对吗?是的。每封信都放在一个单独的容器里。好的,兄弟,让我们试试,给我时间:谢谢。你的答案突出显示了我代码中缺少的部分。你能用+1表示我的答案吗?谢谢。你的答案突出显示了我代码中缺少的部分。你能用+1来喜欢我的答案吗?很高兴帮助你兄弟你需要在按钮单击或编辑文本文本更改时添加容器吗?很高兴帮助你兄弟你需要在按钮单击或编辑文本文本更改时添加容器吗??