Grid 如何使用浮动操作按钮将新网格项或图像添加到已构建的网格视图图像basd Flatter应用程序中
我有一个带有网格视图的12个网格项颤振应用程序。我在网格视图Ui中有一个浮动动作接口。现在,点击扁平动作按钮,我想添加一个新的网格项,它应该显示在网格视图中,现在计数应该是13个网格项 我的代码如下Grid 如何使用浮动操作按钮将新网格项或图像添加到已构建的网格视图图像basd Flatter应用程序中,grid,flutter-layout,Grid,Flutter Layout,我有一个带有网格视图的12个网格项颤振应用程序。我在网格视图Ui中有一个浮动动作接口。现在,点击扁平动作按钮,我想添加一个新的网格项,它应该显示在网格视图中,现在计数应该是13个网格项 我的代码如下 List<ItemModel> items = [ //These will have separate page ItemModel(item: "", icon: Icons.smartphone, page: MobilesPage()),
List<ItemModel> items = [
//These will have separate page
ItemModel(item: "", icon: Icons.smartphone, page: MobilesPage()),
ItemModel(
item: "", icon: Icons.headset, page: MobileAccessoriesPage()),
ItemModel(item: "", icon: Icons.computer, page: ComputerPage()),
ItemModel(item: "", icon: Icons.tv, page: TVPage()),
// These will use the next page
ItemModel(item: "", icon: Icons.local_laundry_service, page: LargeAppPage()),
ItemModel(item: "", icon: Icons.kitchen, page: KitchenApplsPage()),
ItemModel(item: "", icon: Icons.home, page: HomeApplsPage()),
ItemModel(item: "", icon: Icons.devices_other, page: HomeEntSysPage()),
ItemModel(item: "", icon: Icons.headset_mic,page: HeadphonesPage()),
ItemModel(item: "", icon: Icons.photo_camera,page:CamerasPage()),
ItemModel(item: "", icon: Icons.speaker,page:SpeakersPage()),
ItemModel(item: "", icon: Icons.camera_roll,page:CamAccrsPage()),
];
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
// final TextStyle textStyle = Theme.of(context).textTheme.bodyText1;
return Scaffold(
appBar: AppBar(
centerTitle: true,backgroundColor: Colors.lightBlueAccent,
title: new Text("Grid View",style:
TextStyle(fontSize: 20), textAlign: TextAlign.center),
),
body: Center(
child: GridView.count(
primary: false,
padding: const EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 2,
childAspectRatio: 1,
children: items
.map(
(item) => Container(
// color: Colors.cyan[100],
child: GestureDetector(
onTap: () {
if (item.page != null) {
//Navigate to the Page you have
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => item.page,
),
);
} else {
//Use the NextPage and pass in the model
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => NextPage(model: item),
),
);
}
},
child: Card(
elevation: 2,
margin: EdgeInsets.all(5.0),
//color: Colors.cyan[100],
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(20.0)),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(item.icon, size: 90),
SizedBox(height: 20),
Text(item.item,
style:
TextStyle(color: Colors.blue, fontSize: 14.0,fontWeight: FontWeight.bold ),
textAlign: TextAlign.center),
],
),
)),
),
)
.toList(),
)),
floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.red,
child: Icon(Icons.add),
onPressed: () {
setState(() {
items.add(Container(
margin: EdgeInsets.all(8.0),
));
});
},
),
],
),
),
//
);
}
}
class ItemModel {
String item;
IconData icon;
Widget page;
ItemModel({this.item, this.icon, this.page});
}
列表项=[
//这些将有单独的页面
ItemModel(项目:“”,图标:Icons.smartphone,页面:MobilesPage()),
项目模型(
项目:,图标:Icons.headset,页面:MobileAccessoriesPage(),
ItemModel(项目:“”,图标:Icons.computer,页面:ComputerPage()),
ItemModel(项目:“”,图标:Icons.tv,页面:TVPage()),
//这些将使用下一页
ItemModel(项目:“”,图标:Icons.local_洗衣房_服务,页面:LargeAppPage()),
ItemModel(项目:,图标:Icons.kitchen,页面:KitchenApplsPage()),
ItemModel(项目:“”,图标:Icons.home,页面:HomeApplsPage()),
ItemModel(项:“”,图标:Icons.devices_other,页面:HomeEntSysPage()),
ItemModel(项目:“”,图标:图标。耳机麦克风,第页:HeadspedSpage()),
ItemModel(项目:“”,图标:图标。照片\照相机,页面:CamerasPage()),
ItemModel(项:“”,图标:图标。扬声器,页面:SpeakersPage()),
ItemModel(项目:“”,图标:图标。摄像头滚动,页面:CamAccrPage()),
];
类MyHomePage扩展StatefulWidget{
MyHomePage({Key}):超级(Key:Key);
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
//final TextStyle TextStyle=Theme.of(context).textTheme.bodyText1;
返回脚手架(
appBar:appBar(
centerTitle:true,backgroundColor:Colors.lightBlueAccent,
标题:新文本(“网格视图”,样式:
TextStyle(fontSize:20),textAlign:textAlign.center),
),
正文:中(
子项:GridView.count(
主要:错误,
填充:常数边集。全部(20),
横轴间距:10,
平均间距:10,
交叉轴计数:2,
儿童方面:1,
儿童:项目
.地图(
(项目)=>集装箱(
//颜色:颜色。青色[100],
儿童:手势检测器(
onTap:(){
如果(item.page!=null){
//导航到您拥有的页面
导航器.of(上下文).push(
材料路线(
生成器:(上下文)=>item.page,
),
);
}否则{
//使用下一页并在模型中传递
导航器.of(上下文).push(
材料路线(
生成器:(上下文)=>下一页(模型:项目),
),
);
}
},
孩子:卡片(
标高:2,
边距:所有边缘集(5.0),
//颜色:颜色。青色[100],
形状:新的RoundedRectangleBorder(
borderRadius:新的borderRadius.圆形(20.0)),
子:列(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
图标(项目图标,尺寸:90),
尺寸箱(高度:20),
文本(item.item,
风格:
TextStyle(颜色:Colors.blue,fontSize:14.0,fontwweight:fontwweight.bold),
textAlign:textAlign.center),
],
),
)),
),
)
.toList(),
)),
floatingActionButtonLocation:floatingActionButtonLocation.endDocked,
浮动操作按钮:浮动操作按钮(
背景颜色:Colors.red,
子:图标(Icons.add),
已按下:(){
设置状态(){
项目。添加(容器)(
边距:所有边集(8.0),
));
});
},
),
],
),
),
//
);
}
}
类项模型{
字符串项;
Iconda图标;
小部件页面;
ItemModel({this.item,this.icon,this.page});
}
错误:无法将参数类型“容器””分配给参数类型“ItemModel”。打开文档
请查看项目。添加(容器)(至少我已经修复了它,并且正在成功运行。 现在我可以将新的网格项或容器添加到网格视图中 只要在Conatiner地方换车就行了` 完整代码如下:
floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.red,
child: Icon(Icons.add),
onPressed: () {
setState(() {
items.add(ItemModel(
item: "Mobiles", icon: Icons.smartphone
));
});
},
),
],
),
),
//
);
}
}
class ItemModel {
String item;
IconData icon;
Widget page;
ItemModel({this.item, this.icon, this.page});
}