Flutter中Card的使用

GIF.gif
import 'package:flutter/material.dart';
import 'package:flutter_first/DatasBean.dart';

class DataCard extends StatelessWidget {
  DatasBean data;
  Function delete;

  DataCard(this.data, {this.delete});

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text(
              data.name,
              style: TextStyle(fontSize: 20, color: Colors.amber),
            ),
            SizedBox(height: 10),
            Text(
              data.age,
              style: TextStyle(fontSize: 15, color: Colors.amberAccent),
            ),
            SizedBox(height: 10),
            FlatButton.icon(
                onPressed: delete,
                icon: Icon(Icons.delete),
                label: Text("删除"))
          ],
        ),
      ),
    );
  }
}

class DatasBean{
  String name;
  String age;

  DatasBean(this.name, this.age);
}
import 'package:flutter/material.dart';
import 'DatasBean.dart';
import 'DataCard.dart';

void main() => runApp(MaterialApp(
      home: Home(),
    ));

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State {
  List datas = [
    DatasBean("张三", "23"),
    DatasBean("李四", "24"),
    DatasBean("王五", "25")
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            "列表",
            style: TextStyle(fontSize: 20, color: Colors.amber),
          ),
        ),
        body: Column(
          children: datas.map((data) {
            return DataCard(data, delete: () {
              setState(() {
                datas.remove(data);
              });
            });
          }).toList(),
        ));
  }
}

你可能感兴趣的:(Flutter中Card的使用)