import 'package:flutter/material.dart';
import './res/news.dart';
void main() {
runApp(const MyNewApp());
}
class MyNewApp extends StatelessWidget {
const MyNewApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.pink),
home: Scaffold(
appBar: AppBar(title: const Text('Flutter 之Card使用~')),
body: const MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Widget> _initData() {
return lists.map((item) {
return Card(
elevation: 10, // 配置阴影深度
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8), // 配置阴影效果
),
margin: const EdgeInsets.all(16),
color: Colors.white,
child: Column(
children: [
AspectRatio(
aspectRatio: 16 / 9,
child: Image.network(
item['cover'],
fit: BoxFit.cover,
),
),
ListTile(
leading: SizedBox(
width: 50,
height: 50,
child: CircleAvatar(
backgroundImage: NetworkImage(
item['cover'],
),
),
),
title: Text(
item['title'],
maxLines: 1,
style: const TextStyle(
overflow: TextOverflow.ellipsis,
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
subtitle: Text(
item['sub_title'],
maxLines: 1,
style: const TextStyle(
overflow: TextOverflow.ellipsis,
fontSize: 13,
),
),
)
],
),
);
}).toList();
}
Widget build(BuildContext context) {
return ListView(
children: _initData(),
);
}
}