post_page.dart
1import 'dart:io'; 2 3import 'package:chat_app/firestore/post_firestore.dart'; 4import 'package:chat_app/model/post.dart'; 5import 'package:chat_app/utils/authentication.dart'; 6import 'package:chat_app/utils/function_utils.dart'; 7import 'package:cloud_firestore/cloud_firestore.dart'; 8import 'package:flutter/material.dart'; 9 10class PostPage extends StatefulWidget { 11 const PostPage({super.key}); 12 13 @override 14 State<PostPage> createState() => _PostPageState(); 15} 16 17class _PostPageState extends State<PostPage> { 18 TextEditingController contentController = TextEditingController(); 19 File? image; 20 21 @override 22 Widget build(BuildContext context) { 23 return Scaffold( 24 backgroundColor: Colors.transparent, 25 appBar: AppBar( 26 shape: const RoundedRectangleBorder( 27 borderRadius: BorderRadius.only( 28 topLeft: Radius.circular(30), 29 topRight: Radius.circular(30), 30 ), 31 ), 32 elevation: 0, 33 iconTheme: const IconThemeData( 34 color: Colors.black, 35 ), 36 centerTitle: false, 37 title: const Text( 38 "Create post", 39 style: TextStyle( 40 color: Colors.black, 41 fontWeight: FontWeight.bold, 42 ), 43 ), 44 actions: [ 45 OutlinedButton( 46 onPressed: () async { 47 if (contentController.text.isNotEmpty && image != null) { 48 Post newPost = Post( 49 content: contentController.text, 50 postAccountId: Authentication.myAccount!.id, 51 id: '', 52 imagePath: '', 53 postTime: Timestamp.now(), 54 ); 55 await FunctionUtils.uploadImage(uid, image); 56 var result = await PostFirestore.addPost(newPost); 57 if (result == true) { 58 if (!mounted) return; 59 Navigator.pop(context); 60 } 61 } 62 }, 63 child: const Text( 64 "Post", 65 style: TextStyle( 66 color: Colors.black, 67 fontWeight: FontWeight.bold, 68 fontSize: 20, 69 ), 70 ), 71 ), 72 ], 73 ), 74 body: Padding( 75 padding: const EdgeInsets.all(16.0), 76 child: Column( 77 children: [ 78 Expanded( 79 child: TextField( 80 controller: contentController, 81 maxLines: null, 82 decoration: const InputDecoration( 83 hintText: "What's on your mind?", 84 border: InputBorder.none, 85 ), 86 cursorColor: Colors.black, 87 ), 88 ), 89 const SizedBox( 90 height: 30, 91 ), 92 image == null 93 ? const SizedBox() 94 : ClipRRect( 95 borderRadius: BorderRadius.circular(24.0), 96 child: SizedBox( 97 height: MediaQuery.of(context).size.height * 0.3, 98 width: MediaQuery.of(context).size.width * 0.9, 99 child: Image.file( 100 image!, 101 fit: BoxFit.cover, 102 ), 103 ), 104 ), 105 const Divider(height: 1), 106 ListTile( 107 leading: const Icon(Icons.photo_library), 108 title: const Text("Add a photo"), 109 onTap: () async { 110 var result = await FunctionUtils.getImageFromGallery(); 111 setState(() { 112 image = File(result.path); 113 }); 114 }, 115 ), 116 ], 117 ), 118 ), 119 ); 120 } 121} 122 123

0 コメント