feat: refactor RecipesScreen to use grid layout and save column preference
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -1,18 +1,77 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
import '../../../core/api/api_error_mapper.dart';
|
import '../../../core/api/api_error_mapper.dart';
|
||||||
import '../../../core/ui/async_state_views.dart';
|
import '../../../core/ui/async_state_views.dart';
|
||||||
import '../data/recipe_providers.dart';
|
import '../data/recipe_providers.dart';
|
||||||
|
|
||||||
class RecipesScreen extends ConsumerWidget {
|
class RecipesScreen extends ConsumerStatefulWidget {
|
||||||
const RecipesScreen({super.key});
|
const RecipesScreen({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
ConsumerState<RecipesScreen> createState() => _RecipesScreenState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _RecipesScreenState extends ConsumerState<RecipesScreen> {
|
||||||
|
int _selectedColumns = 2;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_loadSelectedColumns();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _loadSelectedColumns() async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
setState(() {
|
||||||
|
_selectedColumns = prefs.getInt('selectedColumns') ?? 2;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _saveSelectedColumns(int columns) async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
await prefs.setInt('selectedColumns', columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
final recipesAsync = ref.watch(recipesProvider);
|
final recipesAsync = ref.watch(recipesProvider);
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: const Text('Recept'),
|
||||||
|
actions: [
|
||||||
|
PopupMenuButton<int>(
|
||||||
|
icon: const Icon(Icons.grid_view),
|
||||||
|
onSelected: (int columns) {
|
||||||
|
setState(() {
|
||||||
|
_selectedColumns = columns;
|
||||||
|
});
|
||||||
|
_saveSelectedColumns(columns);
|
||||||
|
},
|
||||||
|
itemBuilder: (BuildContext context) => <PopupMenuEntry<int>>[
|
||||||
|
const PopupMenuItem<int>(
|
||||||
|
value: 2,
|
||||||
|
child: Text('2 kolumner'),
|
||||||
|
),
|
||||||
|
const PopupMenuItem<int>(
|
||||||
|
value: 4,
|
||||||
|
child: Text('4 kolumner'),
|
||||||
|
),
|
||||||
|
const PopupMenuItem<int>(
|
||||||
|
value: 6,
|
||||||
|
child: Text('6 kolumner'),
|
||||||
|
),
|
||||||
|
const PopupMenuItem<int>(
|
||||||
|
value: 8,
|
||||||
|
child: Text('8 kolumner'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
body: recipesAsync.when(
|
body: recipesAsync.when(
|
||||||
loading: () => const LoadingStateView(label: 'Laddar recept...'),
|
loading: () => const LoadingStateView(label: 'Laddar recept...'),
|
||||||
error: (error, _) => ErrorStateView(
|
error: (error, _) => ErrorStateView(
|
||||||
@@ -27,26 +86,32 @@ class RecipesScreen extends ConsumerWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ListView.builder(
|
return GridView.count(
|
||||||
itemCount: recipes.length,
|
crossAxisCount: _selectedColumns,
|
||||||
itemBuilder: (context, index) {
|
children: List.generate(
|
||||||
|
recipes.length,
|
||||||
|
(index) {
|
||||||
final recipe = recipes[index];
|
final recipe = recipes[index];
|
||||||
return ListTile(
|
return GestureDetector(
|
||||||
leading: recipe.imageUrl != null
|
onTap: () => context.push('/recipes/${recipe.id}'),
|
||||||
? Image.network(recipe.imageUrl!, width: 56, fit: BoxFit.cover)
|
child: Container(
|
||||||
: const Icon(Icons.restaurant),
|
margin: const EdgeInsets.all(4.0),
|
||||||
title: Text(recipe.title),
|
decoration: BoxDecoration(
|
||||||
subtitle: recipe.description != null
|
borderRadius: BorderRadius.circular(8.0),
|
||||||
? Text(
|
image: recipe.imageUrl != null
|
||||||
recipe.description!,
|
? DecorationImage(
|
||||||
maxLines: 1,
|
image: NetworkImage(recipe.imageUrl!),
|
||||||
overflow: TextOverflow.ellipsis,
|
fit: BoxFit.cover,
|
||||||
)
|
)
|
||||||
: null,
|
: null,
|
||||||
trailing: const Icon(Icons.chevron_right),
|
),
|
||||||
onTap: () => context.push('/recipes/${recipe.id}'),
|
child: recipe.imageUrl == null
|
||||||
|
? const Center(child: Icon(Icons.restaurant))
|
||||||
|
: null,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user