1
0
mirror of https://github.com/nisrulz/flutter-examples.git synced 2025-11-08 20:50:04 +00:00

Add new flutter example of todo app with provider (#78)

This commit is contained in:
Rene Lazo Mendez
2021-07-25 14:51:35 -04:00
committed by GitHub
parent dd23466cd4
commit 89f48b8f1e
74 changed files with 1637 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'notifiers/todo_list.dart';
import 'views/home.dart';
void main() {
runApp(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: ChangeNotifierProvider<TodoList>(
create: (context) => TodoList(),
child: MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}

View File

@@ -0,0 +1,16 @@
import 'package:flutter/material.dart';
class Todo extends ChangeNotifier {
final String id;
String title;
bool done;
Todo({@required this.id, this.title, this.done = false});
/// Toggles the value of the item and notify to listeners
void toggle() {
this.done = !this.done;
notifyListeners();
}
}

View File

@@ -0,0 +1,25 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_new_provider_todo/models/todo.dart';
/// This class represents a model
/// that notify listeners when it has some changes
class TodoList extends ChangeNotifier {
List<Todo> _list = new List<Todo>();
List<Todo> get list => _list;
void add(Todo newItem) {
this._list.add(newItem);
notifyListeners();
}
void remove(Todo removedItem) {
this._list.remove(removedItem);
notifyListeners();
}
void clearDoneItems() {
this._list.removeWhere((Todo element) => element.done);
notifyListeners();
}
}

View File

@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import '../widgets/todo_list_wdt.dart';
import '../widgets/add_button.dart';
class MyHomePage extends StatelessWidget {
final String title;
const MyHomePage({Key key, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
print('rebuilding Home View');
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: TodoListWdt(),
),
floatingActionButton: AddButton(),
);
}
}

View File

@@ -0,0 +1,23 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/todo.dart';
import '../notifiers/todo_list.dart';
class AddButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
final todoList = Provider.of<TodoList>(context, listen: false);
print('build IncrementButton');
return FloatingActionButton(
onPressed: () {
todoList.add(
new Todo(id: '${todoList.list.length}', title: 'tituloTodo${todoList.list.length}')
);
},
tooltip: 'Increment',
child: Icon(Icons.add),
);
}
}

View File

@@ -0,0 +1,22 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/todo.dart';
class TodoListItemDisplay extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('rebuilding Todo List Item Display');
return Consumer<Todo>(
builder: (context, todo, child) {
print('rebuilding Consumer Todo List Item Display');
return Text(
'${todo.title}: ${todo.done}',
style: TextStyle(fontSize: 16.0),
);
},
);
}
}

View File

@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/todo.dart';
class TodoListItemToggleButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('build Toggle Item Button');
return Consumer<Todo>(
builder: (context, todo, child) {
print('rebuilding Consumer Todo List Item Toggle Item Button');
return new RaisedButton(
onPressed: (){
todo.toggle();
},
color: todo.done ? Colors.blue : Colors.red,
child: new Icon(Icons.refresh, color: Colors.white70,),
);
},
);
}
}

View File

@@ -0,0 +1,20 @@
import 'package:flutter/material.dart';
import 'todo_list_item_display.dart';
import 'todo_list_item_toggle_button.dart';
class TodoListItemWdt extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('rebuilding ToDo List Item: ');
return new Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
TodoListItemDisplay(),
TodoListItemToggleButton(),
],
);
}
}

View File

@@ -0,0 +1,30 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/todo.dart';
import '../notifiers/todo_list.dart';
import '../widgets/todo_list_item_wdt.dart';
class TodoListWdt extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('building Todo List Wdt');
return Consumer<TodoList>(
builder: (context, todoList, child) {
print('rebuilding Consumer Todo List Wdt');
return ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: todoList.list.length,
itemBuilder: (context, index) {
return ChangeNotifierProvider<Todo>(
create: (context) => todoList.list[index],
child: new TodoListItemWdt()
);
},
);
},
);
}
}