lib/photo_view_screen.dart
import 'package:flutter/material.dart';
import 'package:photoapp/photo.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:photoapp/providers.dart';
class PhotoViewScreen extends StatefulWidget {
@override
_PhotoViewScreenState createState() => _PhotoViewScreenState();
}
class _PhotoViewScreenState extends State<PhotoViewScreen> {
late PageController _controller;
@override
void initState() {
super.initState();
_controller = PageController(
// Riverpodから初期値を受け取り設定
initialPage: context.read(photoViewInitialIndexProvider),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
// ...
body: Stack(
children: [
Consumer(builder: (context, watch, child) {
final asyncPhotoList = watch(photoListProvider);
return asyncPhotoList.when(
data: (photoList) {
return PageView(
controller: _controller,
onPageChanged: (int index) => {},
children: photoList.map((Photo photo) {
return Image.network(
photo.imageURL,
fit: BoxFit.cover,
);
}).toList(),
);
},
loading: () {
return Center(
child: CircularProgressIndicator(),
);
},
error: (e, stackTrace) {
return Center(
child: Text(e.toString()),
);
},
);
}),
// ...
],
),
);
}
}