Flutter2.Flutter list展示数据

main.dart

import 'package:flutter/material.dart';
import 'SwipeToDismissDemo.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
        home: SwipeToDismissDemo()
    );
  }
}
SwipeToDismissDemo.dart
import 'package:flutter/material.dart';

class SwipeToDismissDemo extends StatefulWidget{

  @override
  State createState() {
    return new _SwipeToDismisState();
  }
}

class _SwipeToDismisState extends State{

  List list = List.generate(20, (index) => "This is title $index element");
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(title: Text("this is title"),),
      body: ListView.builder(itemCount:list.length,
      itemBuilder: (context, index){
        //左滑的空控件
        return Dismissible(
          key: Key(list[index]),
          direction: DismissDirection.endToStart,
          child: ListTile(title: Text('${list[index]}')),
          background: Container(
            color: Colors.redAccent,
          ),
          onDismissed: (direction) {
            setState(() {
              Scaffold.of(context).showSnackBar(
                  SnackBar(content: Text("${list[index]}")));
              list.removeAt(index);
            });
          },
        );
      }),
    );
  }
}

效果图如下:

Flutter2.Flutter list展示数据_第1张图片    Flutter2.Flutter list展示数据_第2张图片   Flutter2.Flutter list展示数据_第3张图片

你可能感兴趣的:(Flutter)