gpt4 book ai didi

带有提供程序模式的Flutter AnimatedList

转载 作者:行者123 更新时间:2023-12-03 16:27:43 24 4
gpt4 key购买 nike

我有实现ChangeNotifier的模型

class DataModel with ChangeNotifier{
List<Data> data = List<Data>();

void addData(Data data){
data.add(data);
notifyListeners();
}
}

还有一个ListView监听这些更改:
class DataListView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<DataModel>(
builder: (context, model, child) {
return ListView.builder(
itemCount: model.data.length,
itemBuilder: (context, index) {
return Text(model.data[index].value);
},
);
},
);
}
}

到目前为止,到目前为止,将项目添加到模型中的列表时,更改通知会触发Listview的重建,并且我会看到新数据。但是我无法将其与AnimatedList而不是ListView一起使用。最好id保持原样,因为动画是ui而不是逻辑的考虑。

changenotifier始终为我提供数据的最新版本,但是我真正需要的是“添加项目”或“删除项目”通知。

是否有最佳做法来做到这一点?

最佳答案

这是我的审判结果。
这是Riverpod版本,但我认为提供商也是如此。
有两点。

  • 在使用该小部件的父小部件中初始化状态
    AnimatedList。
  • 使用async异步添加/删除AnimatedList并添加/删除状态。

  • 主镖
    import 'package:animatedlist_riverpod_sample/provider.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_hooks/flutter_hooks.dart';
    import 'package:flutter_slidable/flutter_slidable.dart';
    import 'package:hooks_riverpod/all.dart';

    void main() {
    runApp(ProviderScope(child: 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: Home(),
    );
    }
    }

    class Home extends HookWidget {
    const Home({Key key}) : super(key: key);

    @override
    Widget build(BuildContext context) {
    final todoList = useProvider(todoListProvider.state);
    return Scaffold(appBar: AppBar(title: Text('Todo[${todoList.length}]')), body: TodoListView());
    }
    }

    class TodoListView extends HookWidget {
    TodoListView({Key key}) : super(key: key);
    final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();
    final todoList = useProvider(todoListProvider.state);

    @override
    Widget build(BuildContext context) {
    return AnimatedList(
    key: _listKey,
    initialItemCount: todoList.length,
    itemBuilder: (context, index, animation) =>
    _buildItem(todoList[index], animation, index, context),
    );
    }

    Slidable _buildItem(Todo todo, Animation<double> animation, int index, BuildContext context) {
    return Slidable(
    actionPane: SlidableDrawerActionPane(),
    child: SizeTransition(
    sizeFactor: animation,
    axis: Axis.vertical,
    child: ListTile(title: Text(todo.description), subtitle: Text(todo.id), onTap: () => {})),
    secondaryActions: <Widget>[
    IconSlideAction(
    caption: 'Delete',
    color: Colors.red,
    icon: Icons.delete,
    onTap: () {
    _listKey.currentState.removeItem(
    index, (context, animation) => _buildItem(todo, animation, index, context),
    duration: Duration(milliseconds: 200));
    _removeItem(context, todo);
    },
    ),
    ],
    );
    }

    void _removeItem(BuildContext context, Todo todo) async {
    await Future.delayed(
    Duration(milliseconds: 200), () => context.read(todoListProvider).remove(todo));
    }
    }
    provider.dart
    import 'package:hooks_riverpod/all.dart';

    final todoListProvider = StateNotifierProvider<TodoList>((ref) {
    return TodoList([
    Todo(id: '0', description: 'Todo1'),
    Todo(id: '1', description: 'Todo2'),
    Todo(id: '2', description: 'Todo3'),
    ]);
    });

    class Todo {
    Todo({
    this.id,
    this.description,
    });

    final String id;
    final String description;
    }

    class TodoList extends StateNotifier<List<Todo>> {
    TodoList([List<Todo> initialTodos]) : super(initialTodos ?? []);

    void add(String description) {
    state = [
    ...state,
    Todo(description: description),
    ];
    }

    void remove(Todo target) {
    state = state.where((todo) => todo.id != target.id).toList();
    }
    }
    样本存储库为 here

    关于带有提供程序模式的Flutter AnimatedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61816446/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com