// 引入material UI
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// 返回Material风格的App
return new MaterialApp(
title: "list demo测试",
// 页面脚手架
home: Scaffold(
// 标题栏
appBar: AppBar(
// title
title: new Text("list view 测试"),
// 标题栏背景
backgroundColor: Colors.blueGrey,
),
// 内容模块 ->这里设置居中
body: Center(
// 内容设置一个容器,高度为200,容器内 填充内容 为 listview
child: Container(
height: 200,
// 将listview控件抽离出去 并传递默认宽度
child: new MyList(300.0),
)),
),
);
}
}
// Listview 抽离
class MyList extends StatelessWidget {
double width = 200;
MyList(double w){
this.width = w;
}
@override
Widget build(BuildContext context) {
return new ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
new Container(
width: this.width,
color: Colors.deepOrange,
),
new Container(
width: this.width,
color: Colors.blueGrey,
),
new Container(
width: this.width,
color: Colors.lightBlue,
),
new Container(
width: this.width,
color: Colors.green,
),
new Container(
width: this.width,
color: Colors.yellowAccent,
),
],
);
}
}