Вы можете скопировать и вставить полный код ниже
Вы можете использовать List<Widget>
и вернуть Widget
фрагмент кода
List<Widget> _getListItems() => _items
.asMap()
.map((i, item) => MapEntry(i, _buildTenableListTile(item, i)))
.values
.toList();
Widget _buildTenableListTile(Song item, int index) {
return Dismissible(
key: Key(item.songId),
onDismissed: (direction) {
setState(() {
_items.removeAt(index);
});
},
background: Container(color: Colors.red),
child: ListTile(
key: ValueKey(item.songId),
рабочая демонстрация
введите описание изображения здесь
полный код
import 'package:flutter/material.dart';
class Song {
String songId;
String name;
String sequence;
String artist;
Song({this.name, this.songId, this.artist, this.sequence});
}
class TopTenList extends StatefulWidget {
@override
_TopTenListState createState() => _TopTenListState();
}
class _TopTenListState extends State<TopTenList> {
List<Song> _items = [
Song(
songId: "1",
name: "name 1",
artist: "artist 1",
sequence: "sequence 1"),
Song(
songId: "2",
name: "name 2",
artist: "artist 2",
sequence: "sequence 2"),
Song(
songId: "3",
name: "name 3",
artist: "artist 3",
sequence: "sequence 3"),
Song(
songId: "4",
name: "name 4",
artist: "artist 4",
sequence: "sequence 4"),
Song(
songId: "5",
name: "name 5",
artist: "artist 5",
sequence: "sequence 5"),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Top Ten"),
),
body: ReorderableListView(
onReorder: onReorder,
children: _getListItems(),
),
);
}
void onReorder(int oldIndex, int newIndex) {
if (newIndex > oldIndex) {
newIndex -= 1;
}
setState(() {
Song song = _items[oldIndex];
_items.removeAt(oldIndex);
_items.insert(newIndex, song);
});
}
List<Widget> _getListItems() => _items
.asMap()
.map((i, item) => MapEntry(i, _buildTenableListTile(item, i)))
.values
.toList();
Widget _buildTenableListTile(Song item, int index) {
return Dismissible(
key: Key(item.songId),
onDismissed: (direction) {
setState(() {
_items.removeAt(index);
});
},
background: Container(color: Colors.red),
child: ListTile(
key: ValueKey(item.songId),
title: Text(
'${item.sequence}. ${item.name}',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
subtitle: Text(
'${item.artist} ${item.songId}',
style: TextStyle(
color: Colors.black,
),
),
onTap: () {},
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: TopTenList(),
);
}
}