import 'package:flutter/material.dart';
main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
// just pass the String path in Image.network
const List<Choice> choices = const <Choice>[
const Choice(
title: 'Car', networkImage: "https://via.placeholder.com/150"),
const Choice(
title: 'Bicycle', networkImage: "https://via.placeholder.com/150"),
const Choice(
title: 'Boat', networkImage: "https://via.placeholder.com/150"),
const Choice(
title: 'Bus', networkImage: "https://via.placeholder.com/150"),
];
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: GridView.count(
crossAxisCount: 2,
children: List.generate(choices.length, (index) {
return Center(
child: Container(
child: ChoiceCard(choice: choices[index])),
);
}))),
),
),
),
);
}
}
class Choice {
const Choice({this.title, this.networkImage});
final String title;
final String networkImage;
}
class ChoiceCard extends StatelessWidget {
const ChoiceCard({Key key, this.choice}) : super(key: key);
final Choice choice;
@override
Widget build(BuildContext context) {
final TextStyle textStyle = Theme.of(context).textTheme.display1;
return Container(
child: Card(
color: Colors.white,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image.network(
choice.networkImage,
width: 150,
),
]),
)),
);
}
}
只需检查示例,您就会明白