【发布时间】:2020-11-24 00:40:32
【问题描述】:
我对 fultter 很陌生,我正在尝试制作一个具有可点击卡片的网格。我想选择要种植什么并更改图标以及该植物中的信息以保存到该单独的网格中。我想知道一旦触发特定网格,您如何为它设置新状态?我附上了下面的代码。我似乎无法使用有状态小部件将网格上的图标从铲子图标更改为新芽图标。
import 'package:flutter/material.dart';
import 'my_flutter_app_icons.dart';
class WindowSillGrid extends StatefulWidget {
@override
_WindowSillGridState createState() => _WindowSillGridState();
}
class _WindowSillGridState extends State<WindowSillGrid> {
@override
Widget build(BuildContext context) {
final title = "Window Sill Grid";
return MaterialApp(
title: title,
home: Scaffold(
backgroundColor: Colors.brown[400],
appBar: AppBar(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(20))),
centerTitle: true,
backgroundColor: Colors.green[600],
title: Text(title),
),
body: GridView.count(
childAspectRatio: 1.0,
crossAxisCount: 2,
children: List.generate(6, (index) {
return Center(
child: ChoiceCard(choice: PlantGrowth[index]),
);
}))));
}
}
class PlantGrowthIcons {
const PlantGrowthIcons({this.title, this.icon});
final String title;
final IconData icon;
}
const List<PlantGrowthIcons> PlantGrowth = const <PlantGrowthIcons>[
const PlantGrowthIcons(title: 'Add Plant', icon: MyFlutterApp.shovel),
const PlantGrowthIcons(title: 'Add Plant', icon: MyFlutterApp.shovel),
const PlantGrowthIcons(title: 'Add Plant', icon: MyFlutterApp.shovel),
const PlantGrowthIcons(title: 'Add Plant', icon: MyFlutterApp.shovel),
const PlantGrowthIcons(title: 'Add Plant', icon: MyFlutterApp.shovel),
const PlantGrowthIcons(title: 'Add Plant', icon: MyFlutterApp.shovel),
];
class ChoiceCard extends StatelessWidget {
const ChoiceCard({Key key, this.choice}) : super(key: key);
final PlantGrowthIcons choice;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => WindowSillGridEditPage()));},
child: Container(
color: Colors.brown[300],
child: GridTile(
child: Card(
color: Colors.brown[400],
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(choice.icon, size: 80.0, color: Colors.white),
Text(choice.title, style: TextStyle(color: Colors.white),),
],
),
),
),
),
),
);
}
}
const List<Widget> items = const[
ListTile(
leading: Icon(MyFlutterApp.sprout, size: 50),
title: Text('Asparagus'),
subtitle: Text('Description here'),
),
ListTile(
leading: Icon(MyFlutterApp.sprout, size: 50),
title: Text('Egg Plant'),
subtitle: Text('Description here')
),
ListTile(
leading: Icon(MyFlutterApp.sprout, size: 50),
title: Text('Tomato'),
subtitle: Text('Description here'),
),
ListTile(
leading: Icon(MyFlutterApp.sprout, size: 50),
title: Text('Cucumber'),
subtitle: Text('Description here'),
),
class WindowSillGridEditPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: Colors.green[600],
title: Text('Plant3r'),
),
body: Container(
child:
ListView(
itemExtent: 60,
children: items,
),
),
);}
}
【问题讨论】:
-
图标可以是变量。将图标分配给图标变量并将该变量用于引导。例如。图标 myIcon=Icon(MyFlutterApp.sprout, size: 50);然后把它放在前导:myIcon
标签: flutter dart gridview choice