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

AppState

parent 87a941eb
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
"publisher": "No Starch Press", "publisher": "No Starch Press",
"pages": 472, "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.", "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/" "website": "http://eloquentjavascript.net/"
}, },
{ {
...@@ -20,6 +21,7 @@ ...@@ -20,6 +21,7 @@
"publisher": "O'Reilly Media", "publisher": "O'Reilly Media",
"pages": 254, "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.", "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/" "website": "http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/"
}, },
{ {
...@@ -31,6 +33,7 @@ ...@@ -31,6 +33,7 @@
"publisher": "O'Reilly Media", "publisher": "O'Reilly Media",
"pages": 460, "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.", "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/" "website": "http://speakingjs.com/"
}, },
{ {
...@@ -42,6 +45,7 @@ ...@@ -42,6 +45,7 @@
"publisher": "O'Reilly Media", "publisher": "O'Reilly Media",
"pages": 254, "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.", "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" "website": "http://chimera.labs.oreilly.com/books/1234000000262/index.html"
}, },
{ {
...@@ -53,6 +57,7 @@ ...@@ -53,6 +57,7 @@
"publisher": "No Starch Press", "publisher": "No Starch Press",
"pages": 352, "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.", "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" "website": "https://leanpub.com/understandinges6/read"
}, },
{ {
...@@ -64,6 +69,7 @@ ...@@ -64,6 +69,7 @@
"publisher": "O'Reilly Media", "publisher": "O'Reilly Media",
"pages": 278, "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.", "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" "website": "https://github.com/getify/You-Dont-Know-JS/tree/master/es6%20&%20beyond"
}, },
{ {
...@@ -75,6 +81,7 @@ ...@@ -75,6 +81,7 @@
"publisher": "O'Reilly Media", "publisher": "O'Reilly Media",
"pages": 234, "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.", "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" "website": "http://chimera.labs.oreilly.com/books/1230000000561/index.html"
}, },
{ {
...@@ -86,6 +93,7 @@ ...@@ -86,6 +93,7 @@
"publisher": "O'Reilly Media", "publisher": "O'Reilly Media",
"pages": 538, "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.", "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" "website": "http://chimera.labs.oreilly.com/books/1234000001708/index.html"
} }
] ]
......
...@@ -6,36 +6,33 @@ import 'package:english_words/english_words.dart'; ...@@ -6,36 +6,33 @@ import 'package:english_words/english_words.dart';
const String filePath = '../assets/data.json'; const String filePath = '../assets/data.json';
class AppState extends ChangeNotifier { class TindBookState extends ChangeNotifier {
var input = ""; var input;
var data = ""; var data;
var jfavorites = <dynamic>[]; var favorites = <int>[];
Future<String> loadData() async { void loadData() async {
input = await rootBundle.loadString(filePath); this.input = await rootBundle.loadString(filePath);
data = jsonDecode(input); this.data = jsonDecode(input)['books'];
return data; print(this.data);
notifyListeners();
} }
// Implémenter fonction getData Map<String, dynamic> getData(int index) {
return this.data[index];
}
Map<String, dynamic> getFav(int index) {
return this.data[this.favorites[index]];
}
void addToFav(dynamic obj){ void addDataToFav(Map<String, dynamic> obj){
jfavorites.add(obj); favorites.add(this.data.indexOf(obj));
notifyListeners();
} }
var current = WordPair.random();
void addIndexToFav(int index) {
void getNext(){ favorites.add(index);
current = WordPair.random(); notifyListeners();
notifyListeners();
}
var favorites = <WordPair>[];
void toggleFavorite() {
if (favorites.contains(current)) {
favorites.remove(current);
} else {
favorites.add(current);
} }
notifyListeners();
}
} }
...@@ -7,49 +7,52 @@ class FavoritePage extends StatelessWidget { ...@@ -7,49 +7,52 @@ class FavoritePage extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var appState = context.watch<AppState>(); var appState = context.watch<TindBookState>();
var favorites = appState.jfavorites; var favorites = appState.favorites;
return LayoutBuilder(builder: (context, constraints) { return LayoutBuilder(builder: (context, constraints) {
return Scaffold(backgroundColor: Colors.red[200], body: ListView.builder( return Scaffold(
padding: const EdgeInsets.all(8), backgroundColor: Colors.red[200],
itemCount: favorites.length, body: favorites.length > 0 ? ListView.builder(
itemBuilder: (BuildContext context, int index){ padding: const EdgeInsets.all(8),
return ListView( itemCount: favorites.length,
padding: const EdgeInsets.all(8), itemBuilder: (BuildContext context, int index){
children: <Widget>[ return ListView(
Container( padding: const EdgeInsets.all(8),
color: index%2 == 0 ? Colors.red[500] : Colors.red[400], children: <Widget>[
child: Text(favorites[index]['title']), Container(
), color: index%2 == 0 ? Colors.red[500] : Colors.red[400],
Container( child: Text(favorites[index]['title']),
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],
Container( child: Text(favorites[index]['subtitle']),
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],
Container( child: Text(favorites[index]['author']),
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],
Container( child: Text(favorites[index]['published']),
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],
Container( child: Text(favorites[index]['publisher']),
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],
Container( child: Text(favorites[index]['description']),
color: index%2 == 0 ? Colors.red[500] : Colors.red[400], ),
child: Text(favorites[index]['website']), Container(
) color: index%2 == 0 ? Colors.red[500] : Colors.red[400],
] child: Text(favorites[index]['website']),
); )
} ]
)); );
}
) : Text("No favorites yet !"),
);
}); });
} }
} }
...@@ -12,7 +12,11 @@ class TindBook extends StatelessWidget { ...@@ -12,7 +12,11 @@ class TindBook extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ChangeNotifierProvider( return ChangeNotifierProvider(
create: (context) => TindBookState(), create: (context) {
final appState = TindBookState();
appState.loadData();
return appState;
},
child: MaterialApp( child: MaterialApp(
theme: ThemeData( theme: ThemeData(
primarySwatch: Colors.blue, primarySwatch: Colors.blue,
...@@ -36,35 +40,6 @@ class TindBook extends StatelessWidget { ...@@ -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 { class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key); const MyHomePage({Key? key}) : super(key: key);
...@@ -81,10 +56,10 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -81,10 +56,10 @@ class _MyHomePageState extends State<MyHomePage> {
Widget page; Widget page;
switch (selectedIndex) { switch (selectedIndex) {
case 0: case 0:
page = TindBookContent(); page = FavoritePage();
break; break;
case 1: case 1:
page = FavoritePage(); page = TindBookContent();
break; break;
case 2: case 2:
page = Placeholder(); page = Placeholder();
...@@ -155,6 +130,7 @@ class _TindBookContentState extends State<TindBookContent> { ...@@ -155,6 +130,7 @@ class _TindBookContentState extends State<TindBookContent> {
double _currentX = 0.0; double _currentX = 0.0;
double _deltaX = 0.0; double _deltaX = 0.0;
bool _isSwiping = false; bool _isSwiping = false;
int currentImageIndex = 0;
@override @override
void didChangeDependencies() { void didChangeDependencies() {
...@@ -169,6 +145,20 @@ class _TindBookContentState extends State<TindBookContent> { ...@@ -169,6 +145,20 @@ class _TindBookContentState extends State<TindBookContent> {
double containerWidth = constraints.maxWidth; double containerWidth = constraints.maxWidth;
double containerHeight = constraints.maxHeight; 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( return GestureDetector(
onHorizontalDragStart: (details) { onHorizontalDragStart: (details) {
_startX = details.globalPosition.dx; _startX = details.globalPosition.dx;
...@@ -184,12 +174,15 @@ class _TindBookContentState extends State<TindBookContent> { ...@@ -184,12 +174,15 @@ class _TindBookContentState extends State<TindBookContent> {
onHorizontalDragEnd: (details) { onHorizontalDragEnd: (details) {
if (_isSwiping) { if (_isSwiping) {
_isSwiping = false; _isSwiping = false;
print(_deltaX.toString());
print(currentImageIndex.toString());
print(state.data[currentImageIndex]['image']);
if (_deltaX > 50) { if (_deltaX > 50) {
// Swipe right // Swipe right
state.handleSwipe(1); handleSwipe(1);
} else if (_deltaX < -50) { } else if (_deltaX < -50) {
// Swipe left // Swipe left
state.handleSwipe(-1); handleSwipe(-1);
} }
} }
_startX = 0.0; _startX = 0.0;
...@@ -217,7 +210,7 @@ class _TindBookContentState extends State<TindBookContent> { ...@@ -217,7 +210,7 @@ class _TindBookContentState extends State<TindBookContent> {
child: ClipRRect( child: ClipRRect(
borderRadius: BorderRadius.circular(100), // Adjust the radius as needed borderRadius: BorderRadius.circular(100), // Adjust the radius as needed
child: Image.asset( child: Image.asset(
state.imagePaths[state.currentImageIndex], state.data[currentImageIndex]['image'],
height: containerHeight / 3, // Adjust image height height: containerHeight / 3, // Adjust image height
width: containerWidth / 3, // Adjust image width width: containerWidth / 3, // Adjust image width
fit: BoxFit.contain, fit: BoxFit.contain,
...@@ -266,16 +259,3 @@ class _TindBookContentState extends State<TindBookContent> { ...@@ -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