私は、flutterでGridViewを使った画像一覧ページを作っています。しかし、画像がまとまった状態で表示されているので、見た目上では、全部同じ写真になっています。スクロールすれば、他の写真も表示される状態です。もしわかる方がいらしたら、やり方を教えて頂きたいです。
now_list.dart
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_storage/firebase_storage.dart'; import 'package:image_picker/image_picker.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'dart:io'; import 'dart:math'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', home: NowList(), ); } } class NowList extends StatefulWidget { @override _NowListState createState() => _NowListState(); } class _NowListState extends State<NowList> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Firebase Storage'), ), body: GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2), itemBuilder: (BuildContext context, int index) { return _buildBody(context); } ), floatingActionButton: FloatingActionButton( onPressed: getImage, child: Icon(Icons.add_a_photo), ), ); } Widget _buildBody(BuildContext context) { return StreamBuilder<QuerySnapshot>( stream: Firestore.instance.collection('books').snapshots(), builder: (context, snapshot) { if (!snapshot.hasData) return LinearProgressIndicator(); return _buildList(context, snapshot.data.documents); }, ); } Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) { return ListView( padding: const EdgeInsets.only(top: 20.0), children: snapshot.map((data) => _buildListItem(context, data)).toList() ); } Widget _buildListItem(BuildContext context, DocumentSnapshot data) { final record = Record.fromSnapshot(data); return Padding( key: ValueKey(record.title), padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), child: Container( decoration: BoxDecoration( border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(5.0), ), child: ListTile( title: Column( children: <Widget>[ Image.network(record.imageURL), ], ), ), ), ); } Future getImage() async { // Get image from gallery. var image = await ImagePicker.pickImage(source: ImageSource.gallery); _uploadImageToFirebase(image); } Future<void> _uploadImageToFirebase(File image) async { try { // Make random image name. int randomNumber = Random().nextInt(100000); String imageLocation = 'images/image${randomNumber}.jpg'; // Upload image to firebase. final StorageReference storageReference = FirebaseStorage().ref().child(imageLocation); final StorageUploadTask uploadTask = storageReference.putFile(image); await uploadTask.onComplete; _addPathToDatabase(imageLocation); }catch(e){ print(e.message); } } Future<void> _addPathToDatabase(String text) async { try { // Get image URL from firebase final ref = FirebaseStorage().ref().child(text); var imageString = await ref.getDownloadURL(); // Add location and url to database await Firestore.instance.collection('books').document().setData({'imageURL':imageString , 'title':text}); }catch(e){ print(e.message); showDialog( context: context, builder: (context) { return AlertDialog( content: Text(e.message), ); } ); } } } class Record { final String title; final String imageURL; final DocumentReference reference; Record.fromMap(Map<String, dynamic> map, {this.reference}) : assert(map['title'] != null), assert(map['imageURL'] != null), title = map['title'], imageURL = map['imageURL']; Record.fromSnapshot(DocumentSnapshot snapshot) : this.fromMap(snapshot.data, reference: snapshot.reference); @override String toString() => "Record<$title:$imageURL>"; }
あなたの回答
tips
プレビュー