FlutterFlutter圈子Flutter中文社区

Flutter布局之ListView的使用

2019-01-07  本文已影响10人  TryEnough

教程推荐


效果图

TryEnough

代码

//import 'package:flutter/rendering.dart' show debugPaintSizeEnabled; 
import 'package:flutter/material.dart';

void main() {
  //debugPaintSizeEnabled = true; //开通调试模式
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

List<Widget> list = <Widget>[
  ListTile(
    title: Text('标题',
        style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
    subtitle: Text('子标题'),
    leading: Icon(  //代表cell左边的图
      Icons.theaters,
      color: Colors.blue[500],
    ),
  ),
  ListTile(
    title: Text(
      'title',
      style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0),
    ),
    subtitle: Text('subtitle'),
    leading: Icon(
        Icons.theaters,
      color: Colors.red,
    ),
  ),
  Divider(),
  ListTile(
    title: Text('title',
        style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
    subtitle: Text('subtitle'),
    leading: Icon(
      Icons.restaurant,
      color: Colors.blue[500],
    ),
    trailing: Icon(
      Icons.restaurant,
      color: Colors.red[500],
    ),
  ),
  ListTile(
    title: Text('Emmy\'s Restaurant',
        style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
    subtitle: Text('1923 Ocean Ave'),
    leading: Icon(
      Icons.restaurant,
      color: Colors.blue[500],
    ),
    trailing: Icon(   //代表cell右边的图
      Icons.restaurant,
      color: Colors.red[500],
    ),
  ),
  ListTile(
    title: Text('Chaiya Thai Restaurant',
        style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
    subtitle: Text('272 Claremont Blvd'),
    leading: Icon(
      Icons.restaurant,
      color: Colors.blue[500],
    ),
    trailing: Icon(
      Icons.restaurant,
      color: Colors.red[500],
    ),
  ),
  ListTile(
    title: Text('La Ciccia',
        style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
    subtitle: Text('291 30th St'),
    leading: Icon(
      Icons.restaurant,
      color: Colors.blue[500],
    ),
    trailing: Icon(
      Icons.restaurant,
      color: Colors.red[500],
    ),
  ),
];

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
      title: Text(widget.title),
      elevation: 5.0, //AppBar底部,内容顶部的阴影
    ),
      body: Center(
        child: ListView(  //在body中心添加一个ListView,并设置children为上面的list数组
          children: list,
        ),
      ),
    );
  }
}
上一篇下一篇

猜你喜欢

热点阅读