import 'package:flutter/material.dart'; import…
- User
import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:uuid/uuid.dart';
void main() { runApp(ProviderScope(child: MyApp())); }
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Plant care API')), body: CheckList(), ), ); } }
class CheckList extends ConsumerStatefulWidget { const CheckList({super.key});
@override ConsumerState createState() => _CheckListState(); }
class _CheckListState extends ConsumerState { @override Widget build(BuildContext context) { final todoList = ref.watch(todoListProvider); final completedPercentage = ref.watch(completedPercentageProvider).toStringAsFixed(2); final averageCompletedPercentage = ref.watch(averageCompletedPercentageProvider).toStringAsFixed(2);
return Column( children: [ Padding( padding: const EdgeInsets.all(8.0), child: Text('Completed todos: $completedPercentage%'), ), Padding( padding: const EdgeInsets.all(8.0), child: Text( 'Average completed todos over last 7 days: $averageCompletedPercentage%'), ), Expanded( child: ListView.builder( itemCount: todoList.length, itemBuilder: (context, index) { final todo = todoList[index]; return ListTile( title: Text(todo.description), trailing: Checkbox( value: todo.completed, onChanged: (value) { ref.read(todoListProvider.notifier).toggle(todo.id); }, ), ); }, ), ), ], );} }
final todoListProvider = NotifierProvider<TodoList, List>(() => TodoList());
final completedPercentageProvider = Provider((ref) { final todoList = ref.watch(todoListProvider); return _calculateCompletedPercentage(todoList); });
final averageCompletedPercentageProvider = StateNotifierProvider<CumulativeNotifier, double>( (ref) => CumulativeNotifier(ref), );
class Todo { Todo({ required this.description, required this.id, this.completed = false, });
final String id; final String description; bool completed; }
class TodoList extends Notifier<List> { @override List build() { _scheduleDailyReset(); return _generateDailyTodoList(); }
void add(String description) { state = [ ...state, Todo(id: _uuid.v4(), description: description), ]; }
void toggle(String id) { state = [ for (final todo in state) if (todo.id == id) Todo( id: todo.id, completed: !todo.completed, description: todo.description, ) else todo, ]; }
void edit({required String id, required String description}) { state = [ for (final todo in state) if (todo.id == id) Todo( id: todo.id, completed: todo.completed, description: description, ) else todo, ]; }
void remove(Todo target) { state = state.where((todo) => todo.id != target.id).toList(); }
- Assistant