Flutter 如何在flatter中显示一组相同的小部件?
我已经创建了一个Flutter 如何在flatter中显示一组相同的小部件?,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我已经创建了一个GameBoard小部件,我想用我创建的16个GameTile小部件填充它。在游戏中,GameTile小部件使用Positioned小部件。如何生成16个互动程序而不必键入每个互动程序 这是我的GameBoard小部件代码: return Container( width: _myScreenWidth * 0.80, height: _myScreenWidth * 0.80, child: Stack( children: [ List.gen
GameBoard
小部件,我想用我创建的16个GameTile
小部件填充它。在游戏中,GameTile
小部件使用Positioned
小部件。如何生成16个互动程序而不必键入每个互动程序
这是我的GameBoard
小部件代码:
return Container(
width: _myScreenWidth * 0.80,
height: _myScreenWidth * 0.80,
child: Stack(
children: [
List.generate(16, (index) {
return GameTile(value: tileValue[index], color: tileColor[index], x: tileXCoordinate[index], y: tileYCoordinate[index]);
}),
],
),
);
这是我的GameTile
代码:
return Positioned(
left: x,
bottom: y,
child: PhysicalModel(
borderRadius: BorderRadius.circular(10.0),
color: color,
child: Container(
width: _tileWidth,
height: _tileHeight,
child: Center(
child: Text(
value.toString(),
textAlign: TextAlign.center,
style: new TextStyle(
fontSize: _myScreenWidth * 0.07,
color: backgroundColor,
fontWeight: FontWeight.bold,
),
),
),
),
),
);
非常感谢您的帮助。使用函数和列表存储生成的小部件
并以功能显示它
代码片段
List<Widget> getList() {
List<Widget> widgeList = [];
for (var i = 0; i < 10; i++) {
for (var item in widget.items) {
widgeList.add(Positioned(
left: item.x,
bottom: item.y,
child: PhysicalModel(
borderRadius: BorderRadius.circular(10.0),
color: Colors.blueAccent,
child: Container(
width: item.tileWidth,
height: item.tileHeight,
child: Center(
child: Text(
item.value,
textAlign: TextAlign.center,
style: new TextStyle(
//fontSize: _myScreenWidth * 0.07,
//color: backgroundColor,
fontWeight: FontWeight.bold,
),
),
),
),
),
));
}
}
return widgeList;
}
...
body: Stack(
children: getList(),
),
List getList(){
列表widgeList=[];
对于(变量i=0;i<10;i++){
for(widget.items中的变量项){
widgeList.add(已定位)(
左:第x项,
底部:第.y项,
孩子:物理模型(
边界半径:边界半径。圆形(10.0),
颜色:Colors.blueAccent,
子:容器(
宽度:item.tileWidth,
高度:item.tileHeight,
儿童:中心(
子:文本(
项目价值,
textAlign:textAlign.center,
样式:新文本样式(
//fontSize:_myScreenWidth*0.07,
//颜色:背景色,
fontWeight:fontWeight.bold,
),
),
),
),
),
));
}
}
返回传道者;
}
...
主体:堆栈(
子项:getList(),
),
您的游戏类是这样的,它只是一个演示,我没有重新创建您的所有属性class Game {
double x;
double y;
double tileWidth;
double tileHeight;
String value;
Game({
this.x,
this.y,
this.tileWidth,
this.tileHeight,
this.value,
});
factory Game.fromJson(Map<String, dynamic> json) => Game(
x: json["x"],
y: json["y"],
tileWidth: json["tileWidth"],
tileHeight: json["tileHeight"],
value: json["value"],
);
Map<String, dynamic> toJson() => {
"x": x,
"y": y,
"tileWidth": tileWidth,
"tileHeight": tileHeight,
"value": value,
};
}
类游戏{
双x;
双y;
双瓦宽;
双瓦重量;
字符串值;
游戏({
这个,x,,
这个,嗯,,
这是蒂莱维特,
这个,提利八,
这个,这个价值,,
});
factory Game.fromJson(映射json)=>游戏(
x:json[“x”],
y:json[“y”],
tileWidth:json[“tileWidth”],
tileHeight:json[“tileHeight”],
value:json[“value”],
);
映射到JSON()=>{
“x”:x,
“y”:y,
“tileWidth”:tileWidth,
“tileHeight”:tileHeight,
“价值”:价值,
};
}
完整代码import 'package:flutter/material.dart';
import 'dart:convert';
void main() => runApp(MyApp());
List<Game> gameFromJson(String str) =>
List<Game>.from(json.decode(str).map((x) => Game.fromJson(x)));
String gameToJson(List<Game> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Game {
double x;
double y;
double tileWidth;
double tileHeight;
String value;
Game({
this.x,
this.y,
this.tileWidth,
this.tileHeight,
this.value,
});
factory Game.fromJson(Map<String, dynamic> json) => Game(
x: json["x"],
y: json["y"],
tileWidth: json["tileWidth"],
tileHeight: json["tileHeight"],
value: json["value"],
);
Map<String, dynamic> toJson() => {
"x": x,
"y": y,
"tileWidth": tileWidth,
"tileHeight": tileHeight,
"value": value,
};
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
static String data =
'[ {"x" : 1.0, "y" : 2.0, "tileWidth" : 100.0, "tileHeight" : 200.0, "value" : "test"}, {"x" : 10.0, "y" : 20.0, "tileWidth" : 100.0, "tileHeight" : 200.0, "value" : "test 2"} ] ';
List<Game> items = gameFromJson(data);
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
List<Widget> getList() {
List<Widget> widgeList = [];
for (var i = 0; i < 10; i++) {
for (var item in widget.items) {
widgeList.add(Positioned(
left: item.x,
bottom: item.y,
child: PhysicalModel(
borderRadius: BorderRadius.circular(10.0),
color: Colors.blueAccent,
child: Container(
width: item.tileWidth,
height: item.tileHeight,
child: Center(
child: Text(
item.value,
textAlign: TextAlign.center,
style: new TextStyle(
//fontSize: _myScreenWidth * 0.07,
//color: backgroundColor,
fontWeight: FontWeight.bold,
),
),
),
),
),
));
}
}
return widgeList;
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Stack(
children: getList(),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
导入“包装:颤振/材料.省道”;
导入“dart:convert”;
void main()=>runApp(MyApp());
列表gameFromJson(字符串str)=>
List.from(json.decode(str.map)((x)=>Game.fromJson(x));
字符串gameToJson(列表数据)=>
encode(List.from(data.map((x)=>x.toJson());
班级游戏{
双x;
双y;
双瓦宽;
双瓦重量;
字符串值;
游戏({
这个,x,,
这个,嗯,,
这是蒂莱维特,
这个,提利八,
这个,这个价值,,
});
factory Game.fromJson(映射json)=>游戏(
x:json[“x”],
y:json[“y”],
tileWidth:json[“tileWidth”],
tileHeight:json[“tileHeight”],
value:json[“value”],
);
映射到JSON()=>{
“x”:x,
“y”:y,
“tileWidth”:tileWidth,
“tileHeight”:tileHeight,
“价值”:价值,
};
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
//这是应用程序的主题。
//
//尝试使用“flutter run”运行应用程序。您将看到
//应用程序有一个蓝色工具栏。然后,在不退出应用程序的情况下,重试
//将下面的primarySwatch更改为Colors.green,然后调用
//“热重新加载”(在运行“颤振运行”的控制台中按“r”,
//或者只需将更改保存到颤振IDE中的“热重新加载”。
//请注意,计数器没有重置回零;应用程序
//未重新启动。
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
//此小部件是应用程序的主页。它是有状态的,表示
//它有一个状态对象(定义如下),其中包含影响
//看起来怎么样。
//此类是状态的配置。它保存值(在此
//案例名称)由家长(在本例中为应用程序小部件)提供,以及
//由State的build方法使用。小部件子类中的字段包括
//始终标记为“最终”。
最后的字符串标题;
静态字符串数据=
“[{“x”:1.0,“y”:2.0,“tileWidth”:100.0,“tileHeight”:200.0,“value”:“test”},{“x”:10.0,“y”:20.0,“tileWidth”:100.0,“tileHeight”:200.0,“value”:“test 2”}”;
列表项=gameFromJson(数据);
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
void _incrementCounter(){
设置状态(){
//这个对setState的调用告诉颤振框架,某些东西
//已在此状态下更改,这将导致它重新运行下面的生成方法
//以便显示能够反映更新的值。如果我们更改
//_计数器,而不调用setState(),则生成方法将不可用
//再打一次电话,似乎什么也没发生。
_计数器++;
});
}
List getList(){
列表widgeList=[];
对于(变量i=0;i<10;i++){
for(widget.items中的变量项){
widgeList.add(已定位)(
左:第x项,
底部:第.y项,
孩子:物理模型(
边界半径:边界半径。圆形(10.0),
颜色:Colors.blueAccent,
子:容器(
宽度:item.tileWidth,
高度:item.tileHeight,
儿童:中心(
子:文本(
项目价值,
textAlign:textAlign.center,
样式:新文本样式(
//