Flutter 使用Provider和SQLfLite在flatter中创建动画列表

Flutter 使用Provider和SQLfLite在flatter中创建动画列表,flutter,mobile,flutter-animation,provider,flutter-provider,Flutter,Mobile,Flutter Animation,Provider,Flutter Provider,要求 主页 将新时刻显示为卡片小部件 按钮添加一个新的时刻 按钮上的时刻从数据库中删除时刻 新力矩流 逐步完成问题1、2和3 关于第三个问题,在主页和数据库中添加新的时刻 描述 使用这个简单的应用程序,流程是创建一个新时刻,然后删除该新时刻,新时刻存储在动画列表中。我想动画的时刻删除 我正在使用slqflite数据库和提供者进行交互。但是,在所有这些情况下,在动画列表状态下,在何处适应都很困难 我想我的问题是如何为动画列表设置listkey,以及如何使用provider和sqlflite

要求

主页

  • 将新时刻显示为卡片小部件
  • 按钮添加一个新的时刻
  • 按钮上的时刻从数据库中删除时刻
新力矩流

  • 逐步完成问题1、2和3
  • 关于第三个问题,在主页和数据库中添加新的时刻
描述

使用这个简单的应用程序,流程是创建一个新时刻,然后删除该新时刻,新时刻存储在动画列表中。我想动画的时刻删除

我正在使用slqflite数据库和提供者进行交互。但是,在所有这些情况下,在动画列表状态下,在何处适应都很困难

我想我的问题是如何为动画列表设置listkey,以及如何使用provider和sqlflite触发对动画列表键的删除和添加新时刻

下面是代码片段:

const_columnId='id';
const_columnMoment=‘moment’;
const_columnControlable=‘可控’;
const _columnPlanWhat='planwhat';
const_columnPlanHow='planhhow';
常量_columnPlanWhen='planwhen';
const_column assemption=‘assemption’;
类动量模型{
动量模型({
这个.listKey,
这个.itemBuilderTest,
这个身份证,
这一刻,
这是可以控制的,
这是什么,
这是怎么回事,
这个,什么时候,,
这个,肯定,,
});
MomentModel.fromMap(地图地图){
id=映射[_columnId]为int;
矩=映射[_columnMoment]为字符串;
可控=映射[_columnControlable]为int;
planWhat=map[\u columnPlanWhat]作为字符串;
planHow=映射[_columnPlanHow]作为字符串;
planWhen=映射[_columnPlanWhen]为int;
assignment=map[\u columnassignment]作为字符串;
}
映射toMap(){
最终地图={
_columnId:id,
_时刻:时刻,
_可控:可控,,
_专栏计划什么:计划什么,
_专栏策划:策划,
_columnPlanWhen:planWhen,
_确认:确认,
};
返回图;
}
GlobalKey-listKey;
动态项目构建测试;
int-id;
弦力矩;
int可控;
什么;
弦平面;
int planWhen;
字符串确认;
静态常量createMomentTableString=''
创建表${DBTables.moments}(
$\u columnId INT主键,
$\u柱形文字,
$\u/INT,
$\u ColumnPlan什么文本,
$\u columnPlanHow文本,
$\u ColumnPlant如果为INT,
$\u列确认文本
)''';
}
class MomentViewModel扩展了ChangeNotifier{
MomentViewModel();
///动画提供者
///
最终的GlobalKey _listKey=GlobalKey();
GlobalKey get listKey=>\u listKey;
void insertAnimatedListKey(){
listKey.currentState.insertItem(矩.长度);
}
void deleteAnimatedListKey(){
listKey.currentState.removeItem(矩量.length,矩量模型.itemBuilderTest,
持续时间:持续时间(毫秒:2000));
}
///数据库的提供者内容
///
最终动量模型动量模型=动量模型(
可控:0,
计划时间:0,
);
列表_矩=[];
列表获取时刻=>UnmodifiableListView(_时刻);
int newId(){
返回力矩。长度+1;
}
void insertMoment(){
DatabaseProvider().insert(MomentModel(
listKey:_listKey,
itemBuilderTest:(uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu{},
id:newId(),
时刻:时刻值,
可控:可控开关,
planWhat:planWhatValue,
planHow:planHowValue,
planWhen:planWhenSwitch,
肯定:肯定价值,
));
getMoments();
insertAnimatedListKey();
}
无效删除力矩(整数id){
DatabaseProvider().delete(MomentModel(id:id));
getMoments();
deleteAnimatedListKey();
}
void getMoments(){
DatabaseProvider().getData().then((瞬间){
_力矩=力矩;
notifyListeners();
});
}
}
const _columnId = 'id';
const _columnMoment = 'moment';
const _columnControllable = 'controllable';
const _columnPlanWhat = 'planwhat';
const _columnPlanHow = 'planhow';
const _columnPlanWhen = 'planwhen';
const _columnAffirmation = 'affirmation';

class MomentModel {
  MomentModel({
    this.listKey,
    this.itemBuilderTest,
    this.id,
    this.moment,
    this.controllable,
    this.planWhat,
    this.planHow,
    this.planWhen,
    this.affirmation,
  });

  MomentModel.fromMap(Map<String, dynamic> map) {
    id = map[_columnId] as int;
    moment = map[_columnMoment] as String;
    controllable = map[_columnControllable] as int;
    planWhat = map[_columnPlanWhat] as String;
    planHow = map[_columnPlanHow] as String;
    planWhen = map[_columnPlanWhen] as int;
    affirmation = map[_columnAffirmation] as String;
  }

  Map<String, dynamic> toMap() {
    final map = <String, dynamic>{
      _columnId: id,
      _columnMoment: moment,
      _columnControllable: controllable,
      _columnPlanWhat: planWhat,
      _columnPlanHow: planHow,
      _columnPlanWhen: planWhen,
      _columnAffirmation: affirmation,
    };
    return map;
  }

  GlobalKey<AnimatedListState> listKey;
  dynamic itemBuilderTest;
  int id;
  String moment;
  int controllable;
  String planWhat;
  String planHow;
  int planWhen;
  String affirmation;

  static const createMomentTableString = '''
          CREATE TABLE ${DBTables.moments} (
            $_columnId INT PRIMARY KEY,
            $_columnMoment TEXT,
            $_columnControllable INT,
            $_columnPlanWhat TEXT,
            $_columnPlanHow TEXT,
            $_columnPlanWhen INT,
            $_columnAffirmation TEXT
          )''';
}

class MomentViewModel extends ChangeNotifier {
  MomentViewModel();
  /// PROVIDER STUFF FOR ANIMATION
  ///
  final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();
  GlobalKey<AnimatedListState> get listKey => _listKey;

  void insertAnimatedListKey() {
    listKey.currentState.insertItem(moments.length);
  }

  void deleteAnimatedListKey() {
    listKey.currentState.removeItem(moments.length, momentModel.itemBuilderTest,
        duration: Duration(milliseconds: 2000));
  }

  /// PROVIDER STUFF FOR DATABASE
  ///
  final MomentModel momentModel = MomentModel(
    controllable: 0,
    planWhen: 0,
  );

  List<MomentModel> _moments = [];
  List<MomentModel> get moments => UnmodifiableListView(_moments);

  int newId() {
    return moments.length + 1;
  }

  void insertMoment() {
    DatabaseProvider().insert(MomentModel(
      listKey: _listKey,
      itemBuilderTest: (_, __, ___) {},
      id: newId(),
      moment: momentValue,
      controllable: controllableSwitch,
      planWhat: planWhatValue,
      planHow: planHowValue,
      planWhen: planWhenSwitch,
      affirmation: affirmationValue,
    ));
    getMoments();
    insertAnimatedListKey();
  }

  void deleteMoment(int id) {
    DatabaseProvider().delete(MomentModel(id: id));
    getMoments();
    deleteAnimatedListKey();
  }

  void getMoments() {
    DatabaseProvider().getData().then((moments) {
      _moments = moments;
      notifyListeners();
    });
  }
}