Flutter 颤振可重排序列表返回到它';原始状态

Flutter 颤振可重排序列表返回到它';原始状态,flutter,dart,sqflite,reorderable-list,Flutter,Dart,Sqflite,Reorderable List,我使列表可以重新排序,但现在它又回到了初始状态。有人能帮我吗?我的列表由使用Asynsnapshot从数据库生成的列表平铺组成。我使用的键与索引相同。似乎insert函数没有在新索引中插入注释。是因为未来的建设者正在重建吗 body: Container( padding: EdgeInsets.all(8.0), child: ListView( children: <Widget>[ Si

我使列表可以重新排序,但现在它又回到了初始状态。有人能帮我吗?我的列表由使用Asynsnapshot从数据库生成的列表平铺组成。我使用的键与索引相同。似乎insert函数没有在新索引中插入注释。是因为未来的建设者正在重建吗

body: Container(
          padding: EdgeInsets.all(8.0),
          child: ListView(
            children: <Widget>[
              SizedBox(
                  height: MediaQuery.of(context).size.height * 0.882,
                  child: FutureBuilder(
                      future: databaseHelper.getNoteList(),
                      builder: (BuildContext context, AsyncSnapshot snapshot) {
                        if (snapshot.data == null) {
                          return Text('Loading');
                        } else {
                          if (snapshot.data.length < 1) {
                            return Center(
                              child: Text('No Messages, Create New one'),
                            );
                          }
                          return ReorderableListView(
                            children: List.generate(
                              snapshot.data.length,
                              (index) {
                                return ListTile(
                                  key: Key('$index'),
                                  title: Text(
                                    snapshot.data[index].title,
                                    style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 20,
                                    ),
                                  ),
                                  subtitle: Text(snapshot.data[index].note,
                                      maxLines: 4),
                                  trailing: InkWell(
                                    child: Icon(Icons.add_box,
                                        color: Colors.green),
                                    onTap: () {
                                      TextEditingController txt =
                                          TextEditingController();

                                      txt.text = snapshot.data[index].note;
                                      print(txt);
                                      Route route = MaterialPageRoute(
                                          builder: (context) =>
                                              MyHomePage(custMessage: txt));
                                      Navigator.push(context, route);
                                      // addNewMessageDialog(txt);
                                    },
                                  ),
                                  // isThreeLine: true,
                                  onTap: () {
                                    Route route = MaterialPageRoute(
                                        builder: (context) => AddNote(
                                              note: snapshot.data[index],
                                            ));
                                    Navigator.push(context, route);
                                  },
                                );
                              },
                            ).toList(),
                            onReorder: _onReorder,
                          );
                        }
                      }))
            ],
          )),
主体:容器(
填充:边缘设置。全部(8.0),
子:ListView(
儿童:[
大小盒子(
高度:MediaQuery.of(context).size.height*0.882,
孩子:未来建设者(
future:databaseHelper.getNoteList(),
生成器:(BuildContext上下文,异步快照){
如果(snapshot.data==null){
返回文本(“加载”);
}否则{
如果(snapshot.data.length<1){
返回中心(
子项:文本(“无消息,创建新消息”),
);
}
返回ReorderableListView(
子项:List.generate(
snapshot.data.length,
(索引){
返回列表块(
key:key(“$index”),
标题:正文(
snapshot.data[index].title,
样式:TextStyle(
fontWeight:fontWeight.bold,
尺寸:20,
),
),
字幕:文本(快照.数据[索引])。注意,
最大线:4),
拖尾:墨水井(
子:图标(Icons.add_框,
颜色:颜色。绿色),
onTap:(){
文本编辑控制器=
TextEditingController();
txt.text=snapshot.data[index]。注意;
打印(txt);
路线=物料管理路线(
生成器:(上下文)=>
我的主页(custMessage:txt));
推送(上下文、路线);
//addNewMessageDialog(txt);
},
),
//伊斯特里琳:是的,
onTap:(){
路线=物料管理路线(
生成器:(上下文)=>AddNote(
注意:snapshot.data[索引],
));
推送(上下文、路线);
},
);
},
).toList(),
onReorder:_onReorder,
);
}
}))
],
)),
Reoder函数

void _onReorder(int oldIndex, int newIndex) async {
    var snapshot = await databaseHelper.getNoteList();

    if (newIndex > snapshot.length) newIndex = snapshot.length;
    if (oldIndex < newIndex) newIndex -= 1;
   
      setState(() {
        final Note item = snapshot[oldIndex];
        snapshot.removeAt(oldIndex);

        print(item.title);
        snapshot.insert(newIndex, item);
      });
   
  }
void\u onReorder(int-oldIndex,int-newIndex)异步{
var snapshot=await databaseHelper.getNoteList();
如果(newIndex>snapshot.length)newIndex=snapshot.length;
如果(oldIndex
我试图增加未来的延迟,但没有用。

您可以复制下面的粘贴运行完整代码
您不需要在重新排序时再次调用
您可以使用
noteList=snapshot.data并操作
注释列表

代码片段

void _onReorder(int oldIndex, int newIndex) async {
    if (newIndex > noteList.length) newIndex = noteList.length;
    if (oldIndex < newIndex) newIndex -= 1;

    setState(() {
      final Note item = noteList[oldIndex];
      noteList.removeAt(oldIndex);

      print(item.title);
      noteList.insert(newIndex, item);
    });
  }
...
noteList = snapshot.data;
      return ReorderableListView(
void\u onReorder(int-oldIndex,int-newIndex)异步{
如果(newIndex>noteList.length)newIndex=noteList.length;
如果(oldIndex
工作演示

完整代码

import 'package:flutter/material.dart';

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: 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();
}

class Note {
  String title;
  String note;

  Note({this.title, this.note});
}

class databaseHelper {
  static Future<List<Note>> getNoteList() {
    return Future.value([
      Note(title: "1", note: "n1"),
      Note(title: "2", note: "n2"),
      Note(title: "3", note: "n3"),
      Note(title: "4", note: "n4"),
      Note(title: "5", note: "n5")
    ]);
  }
}

class _MyHomePageState extends State<MyHomePage> {
  List<Note> noteList = [];
  Future<List<Note>> _future;

  void _onReorder(int oldIndex, int newIndex) async {
    if (newIndex > noteList.length) newIndex = noteList.length;
    if (oldIndex < newIndex) newIndex -= 1;

    setState(() {
      final Note item = noteList[oldIndex];
      noteList.removeAt(oldIndex);

      print(item.title);
      noteList.insert(newIndex, item);
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _future = databaseHelper.getNoteList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
          padding: EdgeInsets.all(8.0),
          child: ListView(
            children: <Widget>[
              SizedBox(
                  height: MediaQuery.of(context).size.height * 0.882,
                  child: FutureBuilder(
                      future: _future,
                      builder: (BuildContext context, AsyncSnapshot snapshot) {
                        if (snapshot.data == null) {
                          return Text('Loading');
                        } else {
                          if (snapshot.data.length < 1) {
                            return Center(
                              child: Text('No Messages, Create New one'),
                            );
                          }

                          noteList = snapshot.data;
                          return ReorderableListView(
                            children: List.generate(
                              snapshot.data.length,
                              (index) {
                                return ListTile(
                                  key: Key('$index'),
                                  title: Text(
                                    snapshot.data[index].title,
                                    style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 20,
                                    ),
                                  ),
                                  subtitle: Text(snapshot.data[index].note,
                                      maxLines: 4),
                                  trailing: InkWell(
                                    child: Icon(Icons.add_box,
                                        color: Colors.green),
                                    onTap: () {
                                      /*TextEditingController txt =
                                      TextEditingController();

                                      txt.text = snapshot.data[index].note;
                                      print(txt);
                                      Route route = MaterialPageRoute(
                                          builder: (context) =>
                                              MyHomePage(custMessage: txt));
                                      Navigator.push(context, route);*/
                                      // addNewMessageDialog(txt);
                                    },
                                  ),
                                  // isThreeLine: true,
                                  onTap: () {
                                    /*Route route = MaterialPageRoute(
                                        builder: (context) => AddNote(
                                          note: snapshot.data[index],
                                        ));
                                    Navigator.push(context, route);*/
                                  },
                                );
                              },
                            ).toList(),
                            onReorder: _onReorder,
                          );
                        }
                      }))
            ],
          )),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
课堂笔记{
字符串标题;
弦乐;
注释({this.title,this.Note});
}
类数据库助手{
静态未来getNoteList(){
返回未来值([
注(标题:“1”,注:“n1”),
注(标题:“2”,注:“n2”),
注(标题:“3”,注:“n3”),
注(标题:“4”,注:“n4”),
注(标题:“5”,注:“n5”)
]);
}
}
类_MyHomePageState扩展状态{
列表注释列表=[];
未来,未来;;
void _onReorder(int-oldIndex,int-newIndex)异步{
如果(newIndex>noteList.length)newIndex=noteList.length;