Commit 638273e1 authored by m-spi's avatar m-spi

AppState

parent 87a941eb
......@@ -9,6 +9,7 @@
"publisher": "No Starch Press",
"pages": 472,
"description": "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.",
"image": "assets/images/passeur.png",
"website": "http://eloquentjavascript.net/"
},
{
......@@ -20,6 +21,7 @@
"publisher": "O'Reilly Media",
"pages": 254,
"description": "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.",
"image": "assets/images/1.jpg",
"website": "http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/"
},
{
......@@ -31,6 +33,7 @@
"publisher": "O'Reilly Media",
"pages": 460,
"description": "Like it or not, JavaScript is everywhere these days-from browser to server to mobile-and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.",
"image": "assets/images/passeur.png",
"website": "http://speakingjs.com/"
},
{
......@@ -42,6 +45,7 @@
"publisher": "O'Reilly Media",
"pages": 254,
"description": "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your code base grows.",
"image": "assets/images/1.jpg",
"website": "http://chimera.labs.oreilly.com/books/1234000000262/index.html"
},
{
......@@ -53,6 +57,7 @@
"publisher": "No Starch Press",
"pages": 352,
"description": "ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript.",
"image": "assets/images/passeur.png",
"website": "https://leanpub.com/understandinges6/read"
},
{
......@@ -64,6 +69,7 @@
"publisher": "O'Reilly Media",
"pages": 278,
"description": "No matter how much experience you have with JavaScript, odds are you don’t fully understand the language. As part of the \"You Don’t Know JS\" series, this compact guide focuses on new features available in ECMAScript 6 (ES6), the latest version of the standard upon which JavaScript is built.",
"image": "assets/images/2.jpg",
"website": "https://github.com/getify/You-Dont-Know-JS/tree/master/es6%20&%20beyond"
},
{
......@@ -75,6 +81,7 @@
"publisher": "O'Reilly Media",
"pages": 234,
"description": "This pocket guide is the perfect on-the-job companion to Git, the distributed version control system. It provides a compact, readable introduction to Git for new users, as well as a reference to common commands and procedures for those of you with Git experience.",
"image": "assets/images/passeur.png",
"website": "http://chimera.labs.oreilly.com/books/1230000000561/index.html"
},
{
......@@ -86,6 +93,7 @@
"publisher": "O'Reilly Media",
"pages": 538,
"description": "Design and build Web APIs for a broad range of clients—including browsers and mobile devices—that can adapt to change over time. This practical, hands-on guide takes you through the theory and tools you need to build evolvable HTTP services with Microsoft’s ASP.NET Web API framework. In the process, you’ll learn how design and implement a real-world Web API.",
"image": "assets/images/passeur.png",
"website": "http://chimera.labs.oreilly.com/books/1234000001708/index.html"
}
]
......
......@@ -6,36 +6,33 @@ import 'package:english_words/english_words.dart';
const String filePath = '../assets/data.json';
class AppState extends ChangeNotifier {
var input = "";
var data = "";
var jfavorites = <dynamic>[];
Future<String> loadData() async {
input = await rootBundle.loadString(filePath);
data = jsonDecode(input);
return data;
class TindBookState extends ChangeNotifier {
var input;
var data;
var favorites = <int>[];
void loadData() async {
this.input = await rootBundle.loadString(filePath);
this.data = jsonDecode(input)['books'];
print(this.data);
notifyListeners();
}
// Implémenter fonction getData
Map<String, dynamic> getData(int index) {
return this.data[index];
}
void addToFav(dynamic obj){
jfavorites.add(obj);
Map<String, dynamic> getFav(int index) {
return this.data[this.favorites[index]];
}
var current = WordPair.random();
void getNext(){
current = WordPair.random();
void addDataToFav(Map<String, dynamic> obj){
favorites.add(this.data.indexOf(obj));
notifyListeners();
}
var favorites = <WordPair>[];
void toggleFavorite() {
if (favorites.contains(current)) {
favorites.remove(current);
} else {
favorites.add(current);
}
void addIndexToFav(int index) {
favorites.add(index);
notifyListeners();
}
}
......@@ -7,11 +7,13 @@ class FavoritePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<AppState>();
var favorites = appState.jfavorites;
var appState = context.watch<TindBookState>();
var favorites = appState.favorites;
return LayoutBuilder(builder: (context, constraints) {
return Scaffold(backgroundColor: Colors.red[200], body: ListView.builder(
return Scaffold(
backgroundColor: Colors.red[200],
body: favorites.length > 0 ? ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: favorites.length,
itemBuilder: (BuildContext context, int index){
......@@ -49,7 +51,8 @@ class FavoritePage extends StatelessWidget {
]
);
}
));
) : Text("No favorites yet !"),
);
});
}
}
......@@ -12,7 +12,11 @@ class TindBook extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => TindBookState(),
create: (context) {
final appState = TindBookState();
appState.loadData();
return appState;
},
child: MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
......@@ -36,35 +40,6 @@ class TindBook extends StatelessWidget {
}
}
class TindBookState extends ChangeNotifier {
// List of image paths
List<String> imagePaths = [
'assets/images/passeur.png',
'assets/images/1.jpg',
'assets/images/2.jpg',
// Add more image paths as needed
];
// Index of the currently displayed image
int currentImageIndex = 0;
// Function to handle swiping in any direction
void handleSwipe(int direction) {
if (direction == 1) {
// Swipe right
if (currentImageIndex < imagePaths.length - 1) {
currentImageIndex++;
notifyListeners();
}
} else {
// Swipe left
if (currentImageIndex > 0) {
currentImageIndex--;
notifyListeners();
}
}
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
......@@ -81,10 +56,10 @@ class _MyHomePageState extends State<MyHomePage> {
Widget page;
switch (selectedIndex) {
case 0:
page = TindBookContent();
page = FavoritePage();
break;
case 1:
page = FavoritePage();
page = TindBookContent();
break;
case 2:
page = Placeholder();
......@@ -155,6 +130,7 @@ class _TindBookContentState extends State<TindBookContent> {
double _currentX = 0.0;
double _deltaX = 0.0;
bool _isSwiping = false;
int currentImageIndex = 0;
@override
void didChangeDependencies() {
......@@ -169,6 +145,20 @@ class _TindBookContentState extends State<TindBookContent> {
double containerWidth = constraints.maxWidth;
double containerHeight = constraints.maxHeight;
void handleSwipe(int direction) {
if (direction == 1) {
// Swipe right
if (currentImageIndex < state.data.length - 1) {
currentImageIndex++;
}
} else {
// Swipe left
if (currentImageIndex > 0) {
currentImageIndex--;
}
}
}
return GestureDetector(
onHorizontalDragStart: (details) {
_startX = details.globalPosition.dx;
......@@ -184,12 +174,15 @@ class _TindBookContentState extends State<TindBookContent> {
onHorizontalDragEnd: (details) {
if (_isSwiping) {
_isSwiping = false;
print(_deltaX.toString());
print(currentImageIndex.toString());
print(state.data[currentImageIndex]['image']);
if (_deltaX > 50) {
// Swipe right
state.handleSwipe(1);
handleSwipe(1);
} else if (_deltaX < -50) {
// Swipe left
state.handleSwipe(-1);
handleSwipe(-1);
}
}
_startX = 0.0;
......@@ -217,7 +210,7 @@ class _TindBookContentState extends State<TindBookContent> {
child: ClipRRect(
borderRadius: BorderRadius.circular(100), // Adjust the radius as needed
child: Image.asset(
state.imagePaths[state.currentImageIndex],
state.data[currentImageIndex]['image'],
height: containerHeight / 3, // Adjust image height
width: containerWidth / 3, // Adjust image width
fit: BoxFit.contain,
......@@ -266,16 +259,3 @@ class _TindBookContentState extends State<TindBookContent> {
);
}
}
class FavoritePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Text('Test'),
],
),
);
}
}
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