将用户选择的照片传递到应用程序中的其他页面- Flutter

dsf9zpds  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(113)

我是新来的Flutter,需要一些帮助。我使用ImagePicker允许用户选择他们想要用于特定事件的图像(在“添加新事件页面”),但是,我不知道如何将所选图像传递到其他页面(“即将发生的事件平铺”和“主页”),并在查看特定事件时显示它。我还没有数据库设置,只是一个本地模型页面。我该怎么做?下面是代码(我为草率的代码提前道歉...我还在学习):
添加新事件页面(用户从该页面选择图像):

  1. import 'dart:io';
  2. import 'package:evnt_app/pages/choose_icon_page.dart';
  3. import 'package:evnt_app/pages/navigation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:google_fonts/google_fonts.dart';
  7. import 'package:image_picker/image_picker.dart';
  8. import 'package:provider/provider.dart';
  9. import '../data/evnt_data.dart';
  10. class AddNewEventPage extends StatefulWidget {
  11. const AddNewEventPage({super.key});
  12. @override
  13. State<AddNewEventPage> createState() => _AddNewEventPageState();
  14. }
  15. class _AddNewEventPageState extends State<AddNewEventPage> {
  16. ///////////////////////////////////////////////// Controller Methods /////////////////////////////////////////////////
  17. // Clear Controllers
  18. void clearControllers() {
  19. eventNameController.clear();
  20. eventDateController.clear();
  21. eventTimeController.clear();
  22. eventLocationController.clear();
  23. }
  24. // Support Dialog Box - Text Controller
  25. final eventNameController = TextEditingController();
  26. final eventDateController = TextEditingController();
  27. final eventTimeController = TextEditingController();
  28. final eventLocationController = TextEditingController();
  29. /////////////////////////////////////////////////// "Add" Button Method /////////////////////////////////////////////////////
  30. // "Add New Event" Save Button Method
  31. void saveNewEvent() {
  32. Provider.of<EvntData>(context, listen: false).addEvent(
  33. eventNameController.text,
  34. eventDateController.text,
  35. eventTimeController.text,
  36. eventLocationController.text,
  37. );
  38. Navigator.push(
  39. context,
  40. MaterialPageRoute(
  41. builder: (context) => const Navigation(),
  42. ),
  43. );
  44. }
  45. // Pick Image Method
  46. File? image;
  47. Future pickImage(ImageSource source) async {
  48. try {
  49. final image = await ImagePicker().pickImage(source: source);
  50. if (image == null) return;
  51. final imageTemporary = File(image.path);
  52. setState(() => this.image = imageTemporary);
  53. } on PlatformException catch (e) {
  54. print('Failed to pick image: $e');
  55. }
  56. }
  57. @override
  58. Widget build(BuildContext context) {
  59. return Scaffold(
  60. backgroundColor: Colors.white,
  61. // App Bar
  62. appBar: AppBar(
  63. leading: IconButton(
  64. onPressed: () {
  65. Navigator.of(context).push(
  66. MaterialPageRoute(
  67. builder: (context) => const Navigation(),
  68. ),
  69. );
  70. },
  71. icon: const Icon(
  72. Icons.close,
  73. size: 30,
  74. )),
  75. elevation: 0,
  76. backgroundColor: Colors.red.shade400,
  77. title: Text(
  78. 'Add Event Details',
  79. style: GoogleFonts.poppins(fontSize: 20, fontWeight: FontWeight.bold),
  80. ),
  81. ),
  82. body: Padding(
  83. padding: const EdgeInsets.only(left: 20.0, right: 20.0),
  84. child: Column(
  85. crossAxisAlignment: CrossAxisAlignment.start,
  86. children: [
  87. // Choose Event Photo Icon
  88. Padding(
  89. padding: const EdgeInsets.only(top: 25.0, bottom: 25.0),
  90. child: Column(
  91. children: [
  92. // Image Placeholder
  93. image != null
  94. ? ClipOval(
  95. child: Image.file(image!,
  96. width: 125, height: 125, fit: BoxFit.cover),
  97. )
  98. : Image.asset(
  99. 'lib/assets/images/event-default.png',
  100. width: 125,
  101. height: 125,
  102. ),
  103. const SizedBox(height: 25.0),
  104. // Select Image From Gallery
  105. Row(
  106. mainAxisAlignment: MainAxisAlignment.center,
  107. children: [
  108. // Add Photo From Gallery Button
  109. Container(
  110. decoration: BoxDecoration(
  111. borderRadius: BorderRadius.circular(50),
  112. color: Colors.red.shade400),
  113. child: IconButton(
  114. onPressed: () => pickImage(ImageSource.gallery),
  115. icon: const Icon(
  116. Icons.add_photo_alternate_rounded,
  117. color: Colors.white,
  118. size: 30.0,
  119. ),
  120. ),
  121. ),
  122. const SizedBox(width: 20.0),
  123. // Add Photo From Camera
  124. Container(
  125. decoration: BoxDecoration(
  126. borderRadius: BorderRadius.circular(50.0),
  127. color: Colors.red.shade400),
  128. child: IconButton(
  129. onPressed: () => pickImage(ImageSource.camera),
  130. icon: const Icon(
  131. Icons.add_a_photo_outlined,
  132. color: Colors.white,
  133. size: 30.0,
  134. ),
  135. ),
  136. ),
  137. const SizedBox(width: 20.0),
  138. // Add Icon from Icons List
  139. Container(
  140. decoration: BoxDecoration(
  141. borderRadius: BorderRadius.circular(50.0),
  142. color: Colors.red.shade400),
  143. child: IconButton(
  144. onPressed: () => Navigator.of(context).push(
  145. MaterialPageRoute(
  146. builder: (context) => const ChooseIconPage(),
  147. ),
  148. ),
  149. icon: const Icon(
  150. Icons.apps,
  151. color: Colors.white,
  152. size: 30.0,
  153. ),
  154. ),
  155. ),
  156. ],
  157. ),
  158. ],
  159. ),
  160. ),
  161. // Event Title Header
  162. Row(
  163. children: [
  164. Text(
  165. 'Event Name',
  166. style: GoogleFonts.poppins(
  167. fontSize: 16,
  168. fontWeight: FontWeight.w600,
  169. color: Colors.red.shade400),
  170. ),
  171. ],
  172. ),
  173. // Event Name Text Field
  174. TextField(
  175. controller: eventNameController,
  176. decoration: InputDecoration(
  177. hintText: "Enter Event Name",
  178. hintStyle: GoogleFonts.poppins(
  179. fontSize: 18, fontWeight: FontWeight.w500),
  180. ),
  181. style: GoogleFonts.poppins(
  182. fontSize: 18, fontWeight: FontWeight.w500),
  183. ),
  184. const SizedBox(height: 25.0),
  185. // Event Date Header
  186. Text(
  187. 'Date',
  188. style: GoogleFonts.poppins(
  189. fontSize: 16,
  190. fontWeight: FontWeight.w600,
  191. color: Colors.red.shade400),
  192. ),
  193. // Event Date
  194. TextField(
  195. controller: eventDateController,
  196. decoration: InputDecoration(
  197. hintText: "Enter Date",
  198. hintStyle: GoogleFonts.poppins(
  199. fontSize: 18, fontWeight: FontWeight.w500),
  200. ),
  201. style: GoogleFonts.poppins(
  202. fontSize: 18, fontWeight: FontWeight.w500),
  203. ),
  204. const SizedBox(height: 25.0),
  205. // Event Time Header
  206. Text(
  207. 'Time',
  208. style: GoogleFonts.poppins(
  209. fontSize: 16,
  210. fontWeight: FontWeight.w600,
  211. color: Colors.red.shade400),
  212. ),
  213. // Event Time
  214. TextField(
  215. controller: eventTimeController,
  216. decoration: InputDecoration(
  217. hintText: "Enter Time",
  218. hintStyle: GoogleFonts.poppins(
  219. fontSize: 18, fontWeight: FontWeight.w500),
  220. ),
  221. style: GoogleFonts.poppins(
  222. fontSize: 18, fontWeight: FontWeight.w500),
  223. ),
  224. const SizedBox(height: 25.0),
  225. // Event Location Header
  226. Text(
  227. 'Location',
  228. style: GoogleFonts.poppins(
  229. fontSize: 16,
  230. fontWeight: FontWeight.w600,
  231. color: Colors.red.shade400),
  232. ),
  233. // Event Location
  234. TextField(
  235. controller: eventLocationController,
  236. decoration: InputDecoration(
  237. hintText: "Enter Location",
  238. hintStyle: GoogleFonts.poppins(
  239. fontSize: 18, fontWeight: FontWeight.w500),
  240. ),
  241. style: GoogleFonts.poppins(
  242. fontSize: 18, fontWeight: FontWeight.w500),
  243. ),
  244. const SizedBox(height: 50.0),
  245. // Add Event Button
  246. Column(
  247. children: [
  248. Center(
  249. child: Container(
  250. width: 150,
  251. decoration: BoxDecoration(
  252. borderRadius: BorderRadius.circular(50.0),
  253. color: Colors.red.shade400),
  254. child: MaterialButton(
  255. shape: const RoundedRectangleBorder(
  256. borderRadius: BorderRadius.all(
  257. Radius.circular(50.0),
  258. ),
  259. ),
  260. minWidth: 150.0,
  261. height: 50.0,
  262. onPressed: saveNewEvent,
  263. child: Row(
  264. mainAxisAlignment: MainAxisAlignment.center,
  265. children: [
  266. Text(
  267. 'Add',
  268. style: GoogleFonts.poppins(
  269. fontSize: 18,
  270. fontWeight: FontWeight.w600,
  271. color: Colors.white),
  272. ),
  273. ],
  274. ),
  275. ),
  276. ),
  277. ),
  278. const SizedBox(height: 25.0),
  279. ],
  280. ),
  281. ],
  282. ),
  283. ),
  284. );
  285. }
  286. }

即将到来的事件平铺(我希望图像显示在此平铺上)

  1. import 'package:flutter/material.dart';
  2. import 'package:google_fonts/google_fonts.dart';
  3. import '../models/upcoming_event_model.dart';
  4. class UpcomingEventTile extends StatelessWidget {
  5. final UpcomingEvent upcomingEvents;
  6. void Function()? onTap;
  7. UpcomingEventTile({super.key, required this.upcomingEvents, this.onTap});
  8. @override
  9. Widget build(BuildContext context) {
  10. return Column(
  11. children: [
  12. GestureDetector(
  13. onTap: onTap,
  14. child: Container(
  15. padding: const EdgeInsets.only(top: 20.0, bottom: 20.0),
  16. width: 400,
  17. decoration: BoxDecoration(
  18. color: Colors.grey.shade100,
  19. borderRadius: BorderRadius.circular(15.0),
  20. ),
  21. child: Row(
  22. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  23. children: [
  24. // Icon & Description Column
  25. Padding(
  26. padding: const EdgeInsets.only(left: 20.0),
  27. child: Column(
  28. children: [
  29. /////// USER SELECTED IMAGE HERE
  30. Row(
  31. children: [
  32. const SizedBox(width: 20),
  33. // Event Description
  34. Column(
  35. crossAxisAlignment: CrossAxisAlignment.start,
  36. children: [
  37. Text(
  38. upcomingEvents.upcomingEventName,
  39. style: GoogleFonts.poppins(
  40. fontSize: 15, fontWeight: FontWeight.w600),
  41. ),
  42. const SizedBox(
  43. height: 5.0,
  44. ),
  45. Text(
  46. upcomingEvents.upcomingEventDate,
  47. style: GoogleFonts.poppins(
  48. fontSize: 13,
  49. fontWeight: FontWeight.w600,
  50. color: Colors.red.shade400),
  51. ),
  52. Text(
  53. upcomingEvents.upcomingEventLocation,
  54. style: GoogleFonts.poppins(
  55. fontSize: 13,
  56. fontWeight: FontWeight.w600,
  57. color: Colors.grey.shade600),
  58. ),
  59. ],
  60. ),
  61. ],
  62. ),
  63. ],
  64. ),
  65. ),
  66. // Arrow Icon
  67. Column(
  68. children: [
  69. Padding(
  70. padding: const EdgeInsets.only(right: 20.0),
  71. child: Icon(Icons.keyboard_arrow_right_rounded,
  72. size: 35, color: Colors.red.shade400),
  73. ),
  74. ],
  75. ),
  76. ],
  77. ),
  78. ),
  79. ),
  80. ],
  81. );
  82. }
  83. }

主页(即将到来的活动图在本页检索):

  1. import 'dart:io';
  2. import 'package:evnt_app/models/upcoming_event_model.dart';
  3. import 'package:evnt_app/pages/individual_past_event_page.dart';
  4. import 'package:evnt_app/pages/individual_upcoming_event_page.dart';
  5. import 'package:evnt_app/tiles/past_event_tile.dart';
  6. import 'package:evnt_app/tiles/upcoming_event_tile.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:google_fonts/google_fonts.dart';
  9. import 'package:provider/provider.dart';
  10. import '../data/evnt_data.dart';
  11. import '../models/past_event_model.dart';
  12. class HomePage extends StatefulWidget {
  13. const HomePage({super.key});
  14. @override
  15. State<HomePage> createState() => _HomePageState();
  16. }
  17. class _HomePageState extends State<HomePage> {
  18. @override
  19. Widget build(BuildContext context) {
  20. // Curved Main Tile
  21. return Consumer<EvntData>(
  22. builder: (context, value, child) => Scaffold(
  23. backgroundColor: Colors.grey.shade300,
  24. body: Column(
  25. children: [
  26. Container(
  27. margin: const EdgeInsets.only(top: 100),
  28. height: 612,
  29. decoration: const BoxDecoration(
  30. color: Colors.white,
  31. borderRadius: BorderRadius.only(
  32. topLeft: Radius.circular(50.0),
  33. topRight: Radius.circular(50.0),
  34. ),
  35. ),
  36. child: Padding(
  37. padding: const EdgeInsets.only(top: 20.0),
  38. child: Column(
  39. children: [
  40. //////////////////////////////// PAST EVENTS SECTION ////////////////////////////////
  41. Row(
  42. crossAxisAlignment: CrossAxisAlignment.start,
  43. mainAxisAlignment: MainAxisAlignment.center,
  44. children: [
  45. // Header
  46. Text(
  47. 'Past Events',
  48. style: GoogleFonts.poppins(
  49. fontSize: 20, fontWeight: FontWeight.bold),
  50. ),
  51. ],
  52. ),
  53. // Past Events Icons List
  54. Container(
  55. height: 175,
  56. child: Expanded(
  57. child: ListView.builder(
  58. scrollDirection: Axis.horizontal,
  59. itemCount: value.pastEvents.length,
  60. itemBuilder: (context, index) {
  61. PastEvent individualPastEvent =
  62. value.pastEvents[index];
  63. return Padding(
  64. padding: const EdgeInsets.only(bottom: 10),
  65. child: PastEventTile(
  66. pastEvents: individualPastEvent,
  67. onTap: () {
  68. Navigator.of(context).push(
  69. MaterialPageRoute(
  70. builder: (context) =>
  71. IndividualPastEventPage(
  72. pastEvents: individualPastEvent,
  73. ),
  74. ),
  75. );
  76. },
  77. ),
  78. );
  79. },
  80. ),
  81. ),
  82. ),
  83. //////////////////////////////// UPCOMING EVENTS SECTION ////////////////////////////////
  84. Column(
  85. children: [
  86. // Header
  87. Row(
  88. mainAxisAlignment: MainAxisAlignment.center,
  89. children: [
  90. Padding(
  91. padding: const EdgeInsets.all(10.0),
  92. child: Text(
  93. 'Upcoming Events',
  94. style: GoogleFonts.poppins(
  95. fontSize: 20,
  96. fontWeight: FontWeight.bold,
  97. color: Colors.red.shade400),
  98. ),
  99. )
  100. ],
  101. ),
  102. const SizedBox(height: 10.0),
  103. // Upcoming Events Icons List
  104. Container(
  105. height: 325,
  106. child: Expanded(
  107. child: ListView.builder(
  108. scrollDirection: Axis.vertical,
  109. itemCount: value.upcomingEvents.length,
  110. itemBuilder: (context, index) {
  111. UpcomingEvent individualUpcomingEvent =
  112. value.upcomingEvents[index];
  113. return Padding(
  114. padding: const EdgeInsets.only(bottom: 10),
  115. child: UpcomingEventTile(
  116. upcomingEvents: individualUpcomingEvent,
  117. onTap: () {
  118. Navigator.of(context).push(
  119. MaterialPageRoute(
  120. builder: (context) =>
  121. IndividualUpcomingEventPage(
  122. upcomingEvents:
  123. individualUpcomingEvent,
  124. ),
  125. ),
  126. );
  127. },
  128. ),
  129. );
  130. },
  131. ),
  132. ),
  133. ),
  134. ],
  135. )
  136. ],
  137. ),
  138. ),
  139. ),
  140. ],
  141. ),
  142. ),
  143. );
  144. }
  145. }
qcuzuvrc

qcuzuvrc1#

如果编写代码时不使用外部包,则可以使用
InheritedWidget
test.dart(使用InheritedWidget)

  1. import 'package:flutter/material.dart';
  2. class TestWidget extends InheritedWidget {
  3. String data;
  4. TestWidget({
  5. Key key,
  6. @required Widget child,
  7. }) : super(key: key, child: child);
  8. @override
  9. bool updateShouldNotify(covariant TestWidget oldWidget) {
  10. return data != oldWidget.data;
  11. }
  12. static TestWidget of(BuildContext context) {
  13. return (context.dependOnInheritedWidgetOfExactType<TestWidget>());
  14. }
  15. }

并将MyApp Widget(Root Widget)TestWidget Package 在main.dart中
main.dart
runApp(TestWidget(child: MyApp()));
现在,您可以在新建事件页面中保存“图像路径”。
新建事件页

  1. @override
  2. Widget build(BuildContext context) {
  3. // you have to write code in build method
  4. final info = TestWidget.of(context);
  5. info.data = image; //String

然后,转到UpcomingEventTile并使用它。
活动预告

  1. @override
  2. Widget build(BuildContext context) {
  3. final info = TestWidget.of(context);
  4. ...
  5. ClipOval(
  6. child: Image.file(info.data,
  7. width: 125, height: 125, fit: BoxFit.cover),
  8. )
  9. ...
展开查看全部

相关问题