Flutter 如何从上一页获取数据
我正在尝试使用以下代码构建一个关于照片描述的页面:Flutter 如何从上一页获取数据,flutter,Flutter,我正在尝试使用以下代码构建一个关于照片描述的页面: import 'package:flutter/material.dart'; import 'description_widget.dart'; class ImageDescription extends StatefulWidget { @override _ImageDescriptionState createState() => _ImageDescriptionState(); } class _ImageDes
import 'package:flutter/material.dart';
import 'description_widget.dart';
class ImageDescription extends StatefulWidget {
@override
_ImageDescriptionState createState() => _ImageDescriptionState();
}
class _ImageDescriptionState extends State<ImageDescription> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.white10,
elevation: 0,
leading: Container(
padding: EdgeInsets.fromLTRB(20, 20, 0, 0),
child: InkWell(
child: Hero(
tag: 'back',
child: Image.asset(
'assets/images/wp_back_button_icon.png',
height: 250,
),
),
onTap: () {
Navigator.pop(context);
},
),
),
actions: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(0, 20, 20, 0),
child: Hero(
tag: 'logo',
child: Image.asset(
'assets/images/wp_logo.png',
height: 250,
),
),
),
],
),
body: SingleChildScrollView(
child: imageDescription(
"assets/images/gallery/Image1.jpg",
"Tittle 1",
"Description 1.",
"Image1"),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“description_widget.dart”;
类ImageDescription扩展了StatefulWidget{
@凌驾
_ImageDescriptionState createState()=>\u ImageDescriptionState();
}
类_ImageDescriptionState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
自动嵌入:false,
背景颜色:Colors.white10,
海拔:0,
领先:集装箱(
填充:从LTRB(20,20,0,0)开始的边缘设置,
孩子:InkWell(
孩子:英雄(
标签:“后退”,
子:Image.asset(
“assets/images/wp_back_button_icon.png”,
身高:250,
),
),
onTap:(){
Navigator.pop(上下文);
},
),
),
行动:[
容器(
填充:从LTRB(0,20,20,0)开始的边缘设置,
孩子:英雄(
标签:“logo”,
子:Image.asset(
“assets/images/wp_logo.png”,
身高:250,
),
),
),
],
),
正文:SingleChildScrollView(
子:图像描述(
“assets/images/gallery/Image1.jpg”,
“标题1”,
“说明1。”,
“图像1”),
),
);
}
}
使用我创建的这个小部件:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
Widget imageDescription(String url, title, description, tag) {
return Container(
child: Column(
children: <Widget>[
Container(
child: Column(
children: <Widget>[
Center(
child: Text(title, style: TextStyle(fontSize: 30)),
),
Center(
child: Text("Let's educate in the fun way!"),
)
],
),
),
Container(
child: Column(
children: <Widget>[
Container(
padding: const EdgeInsets.all(10),
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Hero(tag: tag, child: Image.asset(url)),
),
),
Container(
padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
child: Card(
child: Container(
margin: const EdgeInsets.all(10),
child: Text(
description,
textAlign: TextAlign.justify,
style: TextStyle(fontSize: 20),
),
),
),
)
],
),
)
],
),
);
}
导入“包装:颤振/材料.省道”;
进口“包装:颤振/cupertino.dart”;
小部件图像描述(字符串url、标题、描述、标记){
返回容器(
子:列(
儿童:[
容器(
子:列(
儿童:[
居中(
子项:文本(标题,样式:TextStyle(fontSize:30)),
),
居中(
孩子:文本(“让我们用有趣的方式教育吧!”),
)
],
),
),
容器(
子:列(
儿童:[
容器(
填充:常量边集。全部(10),
孩子:ClipRRect(
边界半径:边界半径。圆形(20),
子:英雄(标记:标记,子:Image.asset(url)),
),
),
容器(
填充:从LTRB(10,0,10,0)开始的边缘设置,
孩子:卡片(
子:容器(
边距:常数边集全部(10),
子:文本(
描述
textAlign:textAlign.justify,
样式:TextStyle(字体大小:20),
),
),
),
)
],
),
)
],
),
);
}
但不是手动添加图像描述(
“assets/images/gallery/Image1.jpg”,
“标题1”,
“说明1。”,
“图像1”),
我想从上一页单击的图像中获取变量:
import 'package:flutter/material.dart';
import 'image_description.dart';
import 'show_image.dart';
class Gallery extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.white10,
elevation: 0,
leading: Container(
padding: EdgeInsets.fromLTRB(20, 20, 0, 0),
child: InkWell(
child: Hero(
tag: 'back',
child: Image.asset(
'assets/images/wp_back_button_icon.png',
height: 250,
),
),
onTap: () {
Navigator.pop(context);
},
),
),
actions: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(0, 20, 20, 0),
child: Hero(
tag: 'logo',
child: Image.asset(
'assets/images/wp_logo.png',
height: 250,
),
),
),
],
),
body: SafeArea(
child: Column(
children: <Widget>[
Container(
child: Text(
'AR Gallery',
style: TextStyle(fontSize: 30),
),
),
Container(
child: Text("Let's educate in the fun way"),
),
Expanded(
child: SizedBox(
height: double.infinity,
child: GridView.count(
primary: false,
padding: const EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 3,
children: <Widget>[
InkWell(
child: Stack(
children: <Widget>[
ShowImage(
url: "assets/images/gallery/Image1.jpg",
tag: "Image1",
title: "Title 1",
)
],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImageDescription()));
},
),
InkWell(
child: Stack(
children: <Widget>[
ShowImage(
url: "assets/images/gallery/Image2.jpg",
tag: "Image2",
title: "Title 2",
)
],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImageDescription()));
},
),
InkWell(
child: Stack(
children: <Widget>[
ShowImage(
url: "assets/images/gallery/Image3.jpg",
tag: "Image3",
title: "Title 3",
)
],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImageDescription()),
);
}),
],
),
),
),
],
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“image_description.dart”;
导入“show_image.dart”;
类库扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
resizeToAvoidBottomPadding:false,
appBar:appBar(
自动嵌入:false,
背景颜色:Colors.white10,
海拔:0,
领先:集装箱(
填充:从LTRB(20,20,0,0)开始的边缘设置,
孩子:InkWell(
孩子:英雄(
标签:“后退”,
子:Image.asset(
“assets/images/wp_back_button_icon.png”,
身高:250,
),
),
onTap:(){
Navigator.pop(上下文);
},
),
),
行动:[
容器(
填充:从LTRB(0,20,20,0)开始的边缘设置,
孩子:英雄(
标签:“logo”,
子:Image.asset(
“assets/images/wp_logo.png”,
身高:250,
),
),
),
],
),
正文:安全区(
子:列(
儿童:[
容器(
子:文本(
‘AR画廊’,
样式:TextStyle(字体大小:30),
),
),
容器(
孩子:文本(“让我们以有趣的方式进行教育”),
),
扩大(
孩子:大小盒子(
高度:双无限,
子项:GridView.count(
主要:错误,
填充:常数边集。全部(20),
横轴间距:10,
平均间距:10,
交叉轴计数:3,
儿童:[
墨水池(
子:堆栈(
儿童:[
ShowImage(
url:“assets/images/gallery/Image1.jpg”,
标签:“Image1”,
标题:“标题1”,
)
],
),
onTap:(){
导航器。推(
上下文
材料路线(
生成器:(上下文)=>ImageDescription());
},
),
墨水池(
子:堆栈(
儿童:[
ShowImage(
url:“assets/images/gallery/Image2.jpg”,
标签:“Image2”,
标题:“标题2”,
)
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class ShowImage extends StatelessWidget {
final String tag;
final String url;
final String title;
const ShowImage({Key key, this.tag, this.url, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child:
Hero(tag: tag, child: Image.asset(url, fit: BoxFit.fitHeight)),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text(title,
style: TextStyle(
backgroundColor: Colors.deepOrange, color: Colors.white)),
),
)
],
);
}
}
class ImageData {
final String title;
final String url;
final String description;
final String tag;
Image(this.title, this.url, this.description, this.tag);
}
import 'package:flutter/material.dart';
import 'description_widget.dart';
import 'image_data.dart';
class ImageDescription extends StatefulWidget {
final ImageData imageData;
// In the constructor, require an Image Object.
ImageDescriptionScreen({Key key, @required this.imageData}) :
super(key: key);
@override
_ImageDescriptionState createState() => _ImageDescriptionState();
}
class _ImageDescriptionState extends State<ImageDescription> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.white10,
elevation: 0,
leading: Container(
padding: EdgeInsets.fromLTRB(20, 20, 0, 0),
child: InkWell(
child: Hero(
tag: 'back',
child: Image.asset(
'assets/images/wp_back_button_icon.png',
height: 250,
),
),
onTap: () {
Navigator.pop(context);
},
),
),
actions: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(0, 20, 20, 0),
child: Hero(
tag: 'logo',
child: Image.asset(
'assets/images/wp_logo.png',
height: 250,
),
),
),
],
),
body: SingleChildScrollView(
child: imageDescription(
widget.imageData.url,
widget.imageData.title,
widget.imageData.description,
widget.imageData.tag
),
),
);
}
}
Navigator.push(context, MaterialPageRoute(builder: (context) =>
ImageDescription(imageData: ImageData('title', 'url', 'description', 'tag'),)));
final List<ImageData> imageList = [
ImageData(title: 'MacBook',url: 'https://picsum.photos/250?image=9',
description: 'this is a macbook', tag: 'macbook'),
ImageData(title: 'Deer',url: 'https://picsum.photos/250?image=1003',
description: 'this is a deer', tag: 'deer'),
];