Untitled
unknown
plain_text
2 years ago
3.6 kB
5
Indexable
import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:todo_app/provider/done_todo_provider.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return ChangeNotifierProvider( create: (context) => DoneTodoProvider(), child: MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const TodoPage(), ), ); } } class TodoPage extends StatefulWidget { const TodoPage({super.key}); @override State<TodoPage> createState() => _TodoPageState(); } class _TodoPageState extends State<TodoPage> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Todo List Harian'), actions: [ IconButton( onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => DoneTodoList(), ), ); }, icon: const Icon( Icons.done, ), ) ], ), body: TodoList( doneTodoList: doneTodoList, ), ); } } class TodoList extends StatefulWidget { const TodoList({super.key, required this.doneTodoList}); final List<String> doneTodoList; @override State<TodoList> createState() => _TodoListState(); } class _TodoListState extends State<TodoList> { final List<String> todoList = const [ '1. Mandi', '2. Sholat Malam', '3. Sholat Subuh Berjamaah', '4. Membaca Al Quran', '5. Menghafal Al Quran', '6. Sarapan', '7. Olahraga Pagi', ]; @override Widget build(BuildContext context) { return ListView.builder( itemCount: todoList.length, itemBuilder: (context, index) { return Consumer<DoneTodoProvider>( builder:(context,data,widget){ return TodoTile( todoName: TodoList[index], isDone: data.doneTodoList.contains(todoList[index]), onClick: (){ data.finish(todoList[index]); }, ); }, ); }, ); } } class TodoTile extends StatelessWidget { const TodoTile( {super.key, required this.todoName, required this.isDone, required this.onClick}); final String todoName; final bool isDone; final Function() onClick; @override Widget build(BuildContext context) { return ListTile( title: Text(todoName), trailing: isDone ? const Icon(Icons.done) : ElevatedButton(onPressed: onClick, child: const Text('Done')), ); } } class DoneTodoList extends StatelessWidget { const DoneTodoList({super.key}); @override Widget build(BuildContext context) { final doneTodoList = Provider.of<DoneTodoProvider>(context,listen:false).doneTodoList; return Scaffold( appBar: AppBar( title: const Text('Done Todo List'), ), body: ListView.builder( itemCount: doneTodoList.length, itemBuilder: (context, index) { return ListTile(title: Text(doneTodoList[index])); }, ), ); } }
Editor is loading...
Leave a Comment