flutter从0开始自学

flutter列表的使用

2020-03-09  本文已影响0人  it奔跑在路上

1.列表的内容是String类型的

image.png
class Home extends StatelessWidget {
  List<String> datas = ["文本一", "文本二", "文本三"];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "列表",
          style: TextStyle(fontSize: 20, color: Colors.amber),
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          children: datas.map((data) {
            return Text(data);
          }).toList(),
        ),
      ),
    );
  }
}

2.列表的内容是对象类型的

image.png

DatasBean

class DatasBean{
  String name;
  int age;

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

void main() => runApp(MaterialApp(
//第二步
      home: Home(),
    ));

class Home extends StatelessWidget {
  List<DatasBean> 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: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          children: datas.map((data) {
            return Text("${data.name} ${data.age}");
          }).toList(),
        ),
      ),
    );
  }
}
上一篇下一篇

猜你喜欢

热点阅读