我在一个项目中,当我按“书签”图标时,图像应传递到Saved.dart页面。下面的代码包含一个书签图标。每当我按该图标时,它都应将图像传递到saved.dart页面,但不应将其(应在按下时不打开Saved.dart页面)推到Saved.dart页面。
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:image_downloader/image_downloader.dart';
import 'downloadingPage.dart';
class FullScreen extends StatefulWidget {
String imgUrl;
FullScreen({this.imgUrl});
@override
FullScreenState createState() => FullScreenState(imgUrl);
}
class FullScreenState extends State<FullScreen> {
Color colour = Colors.black;
Color colour2 = Colors.red;
Color colour3 = Colors.black;
bool isSaved = false;
String imgUrl;
FullScreenState(this.imgUrl);
String message = "";
String path = "";
int _progress = 0;
@override
void initState() {
super.initState();
events = new StreamController<int>.broadcast();
events.add(0);
ImageDownloader.callback(onProgressUpdate: (String imageId, int progress) {
setState(() {
print("progress $progress");
_progress = progress;
events.add(progress);
if (progress == 100) {
Navigator.pop(context);
}
});
});
}
@override
void dispose() {
events.close();
super.dispose();
}
Future<void> _downloadImage(String url,
{AndroidDestinationType destination, bool whenError = false}) async {
String fileName;
String path;
try {
String imageId;
if (whenError) {
imageId = await ImageDownloader.downloadImage(url).catchError((error) {
if (error is PlatformException) {
var path = "";
if (error.code == "404") {
print("Not Found Error.");
} else if (error.code == "unsupported_file") {
print("UnSupported FIle Error.");
path = error.details["unsupported_file_path"];
}
setState(() {
message = error.toString();
path = path;
});
}
print(error);
}).timeout(Duration(seconds: 10), onTimeout: () {
print("timeout");
});
} else {
if (destination == null) {
imageId = await ImageDownloader.downloadImage(url);
} else {
imageId = await ImageDownloader.downloadImage(
url,
destination: destination,
);
}
}
if (imageId == null) {
print("imageId is null");
return;
}
fileName = await ImageDownloader.findName(imageId);
path = await ImageDownloader.findPath(imageId);
} on PlatformException catch (error) {
setState(() {
message = error.message;
});
return;
}
if (!mounted) return;
setState(() {
message = 'Image Downloaded';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(
onPressed: () {
events.add(0);
_downloadImage(imgUrl);
showDialog(
context: context,
builder: (_) => FunkyOverlay(
progress: _progress,
message: message,
),
);
},
child: Text("download"),
),
IconButton(
icon: Icon(
Icons.bookmark,
color: colour,
),
onPressed: () {
<-----when user press this icon the current image shold be passed to saved page----->
})
],
),
),
);
}
}
以下代码已保存。dart
import 'package:flutter/material.dart';
class Saved extends StatefulWidget {
@override
_SavedState createState() => _SavedState();
}
class _SavedState extends State<Saved> {
@override
Widget build(BuildContext context) {
return Scaffold(
body:Container(
// here the images should be displayed
)
);
}
}
答案 0 :(得分:0)
您不能将任何东西传递给未初始化的小部件。从技术上讲,该小部件在您调用它之前甚至都不存在。
您将必须将Image保存在变量中。然后当您最终导航到该屏幕时将其传递。
答案 1 :(得分:0)
通过命名路线打开页面时可以传递参数:
应用中的命名路线:
MaterialApp(
routes: {
ExtractArgumentsScreen.routeName: (context) => ExtractArgumentsScreen(),
},
);
传递参数对象,例如您的图片:
onPressed: () {
// When the user taps the button, navigate to a named route
// and provide the arguments as an optional parameter.
Navigator.pushNamed(
context,
ExtractArgumentsScreen.routeName,
arguments: ScreenArguments(
'Extract Arguments Screen',
'This message is extracted in the build method.',
),
);
},
在您保存的.dart页面中接收参数:
class ExtractArgumentsScreen extends StatelessWidget {
static const routeName = '/extractArguments';
@override
Widget build(BuildContext context) {
// Extract the arguments from the current ModalRoute settings and cast
// them as ScreenArguments.
final ScreenArguments args = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text(args.title),
),
body: Center(
child: Text(args.message),
),
);
}
}
来源:https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments