Commit dc5cd0b9 authored by m-spi's avatar m-spi

base for favorite page and appstate

parent 6a2d4880
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:english_words/english_words.dart';
const String filePath = '../assets/data.json';
class AppState extends ChangeNotifier {
final input;
final data;
var input = "";
var data = "";
var jfavorites = <dynamic>[];
const AppState(var input, var data):
input = await File(filePath).readAsString(),
data = jsonDecode(input).books;
Future<String> loadData() async {
input = await rootBundle.loadString(filePath);
data = jsonDecode(input);
return data;
}
// Implémenter fonction getData
var favorites = <dynamic>[];
void addToFav(dynamic obj){
favorites[] = obj
jfavorites.add(obj);
}
var current = WordPair.random();
void getNext(){
current = WordPair.random();
notifyListeners();
}
var favorites = <WordPair>[];
void toggleFavorite() {
if (favorites.contains(current)) {
favorites.remove(current);
} else {
favorites.add(current);
}
notifyListeners();
}
}
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'app_state.dart';
......@@ -7,29 +8,48 @@ class FavoritePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<AppState>();
var favorites = appState.favorites;
var favorites = appState.jfavorites;
return LayoutBuilder(builder: (context, constraints) {
return Scaffold(body: ListView.builder(
return Scaffold(backgroundColor: Colors.red[200], body: ListView.builder(
padding: const EdgeInsets.all(8),
color: Color.red[200],
itemCount: favorites.length,
itemBuilder: (BuildContext context, int index){
return Container(
return ListView(
padding: const EdgeInsets.all(8),
color: index%2 == 0 ? Color.red[500] : Color.red[400],
children: <Widget>[
Text(favorites[index]['title']),
Text(favorites[index]['subtitle']),
Text(favorites[index]['author']),
Text(favorites[index]['published']),
Text(favorites[index]['publisher']),
Text(favorites[index]['description']),
Text(favorites[index]['website']),
];
)
Container(
color: index%2 == 0 ? Colors.red[500] : Colors.red[400],
child: Text(favorites[index]['title']),
),
Container(
color: index%2 == 0 ? Colors.red[500] : Colors.red[400],
child: Text(favorites[index]['subtitle']),
),
Container(
color: index%2 == 0 ? Colors.red[500] : Colors.red[400],
child: Text(favorites[index]['author']),
),
Container(
color: index%2 == 0 ? Colors.red[500] : Colors.red[400],
child: Text(favorites[index]['published']),
),
Container(
color: index%2 == 0 ? Colors.red[500] : Colors.red[400],
child: Text(favorites[index]['publisher']),
),
Container(
color: index%2 == 0 ? Colors.red[500] : Colors.red[400],
child: Text(favorites[index]['description']),
),
Container(
color: index%2 == 0 ? Colors.red[500] : Colors.red[400],
child: Text(favorites[index]['website']),
)
]
);
}
))
));
});
}
}
......@@ -2,6 +2,8 @@ import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'dart:math';
import 'favorite_page.dart';
import 'app_state.dart';
void main() {
......@@ -14,7 +16,7 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => MyAppState(),
create: (context) => AppState(),
child: MaterialApp(
title: 'Namer App',
theme: ThemeData(
......@@ -116,7 +118,7 @@ class _MyHomePageState extends State<MyHomePage> {
class GeneratorPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
var appState = context.watch<AppState>();
var pair = appState.current;
IconData icon;
......@@ -186,27 +188,3 @@ class BigCard extends StatelessWidget {
);
}
}
class FavoritePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
var favorites = appState.favorites;
return Scaffold(
body: Column(children: [
Text('Favorites'),
for (var fav in favorites)
Text(fav.asLowerCase),
],)
);
}
}
\ No newline at end of file
......@@ -79,26 +79,26 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: cdd14e3836065a1f6302a236ec8b5f700695c803c57ae11a1c84df31e6bcf831
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
url: "https://pub.dev"
source: hosted
version: "10.0.3"
version: "10.0.4"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "9b2ef90589911d665277464e0482b209d39882dffaaf4ef69a3561a3354b2ebc"
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
url: "https://pub.dev"
source: hosted
version: "3.0.2"
version: "3.0.3"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: fd3cd66cb2bcd7b50dcd3b413af49d78051f809c8b3f6e047962765c15a0d23d
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
version: "3.0.1"
lints:
dependency: transitive
description:
......@@ -127,10 +127,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.12.0"
nested:
dependency: transitive
description:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment