gpt4 book ai didi

dart - 在 Flutter 中显示全屏图像 onTap

转载 作者:IT老高 更新时间:2023-10-28 12:36:46 25 4
gpt4 key购买 nike

我使用 Flutter 小部件之一显示图像

Image.network(....);

我想添加功能,在点击图像后,我可以以全屏模式显示此图像。怎么做?

最佳答案

你说你想要这样的flutter cookbook ?

import 'package:flutter/material.dart';

void main() => runApp(HeroApp());

class HeroApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image/Detail Demo',
home: MainScreen(),
);
}
}

class MainScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Main Screen'),
),
body: GestureDetector(
child: Hero(
tag: 'imageHero',
child: Image.network(
'https://raw.githubusercontent.com/flutter/website/master/src/_includes/code/layout/lakes/images/lake.jpg',
),
),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return DetailScreen();
}));
},
),
);
}
}

class DetailScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
child: Center(
child: Hero(
tag: 'imageHero',
child: Image.network(
'https://raw.githubusercontent.com/flutter/website/master/src/_includes/code/layout/lakes/images/lake.jpg',
),
),
),
onTap: () {
Navigator.pop(context);
},
),
);
}
}

您可以使用 cache_network_image包以显示缓存的图像而无需再次下载。

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

void main() => runApp(HeroApp());

class HeroApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image/Detail Demo',
home: MainScreen(),
);
}
}

class MainScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Main Screen'),
),
body: GestureDetector(
child: Hero(
tag: 'imageHero',
child: CachedNetworkImage(
imageUrl: 'https://raw.githubusercontent.com/flutter/website/master/src/_includes/code/layout/lakes/images/lake.jpg',
placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error),
)
),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return DetailScreen();
}));
},
),
);
}
}

class DetailScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
child: Center(
child: Hero(
tag: 'imageHero',
child: CachedNetworkImage(
imageUrl: 'https://raw.githubusercontent.com/flutter/website/master/src/_includes/code/layout/lakes/images/lake.jpg',
placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error),
),
),
),
onTap: () {
Navigator.pop(context);
},
),
);
}
}

我的最终版本(真正的全屏):

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/services.dart';

void main() => runApp(HeroApp());

class HeroApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image/Detail Demo',
home: MainScreen(),
);
}
}

class MainScreen extends StatefulWidget {
@override
_MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Main Screen'),
),
body: GestureDetector(
child: Hero(
tag: 'imageHero',
child: CachedNetworkImage(
imageUrl:
'https://raw.githubusercontent.com/flutter/website/master/src/_includes/code/layout/lakes/images/lake.jpg',
placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error),
)),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return DetailScreen();
}));
},
),
);
}
}

class DetailScreen extends StatefulWidget {
@override
_DetailScreenState createState() => _DetailScreenState();
}

class _DetailScreenState extends State<DetailScreen> {
@override
initState() {
SystemChrome.setEnabledSystemUIOverlays([]);
super.initState();
}

@override
void dispose() {
//SystemChrome.restoreSystemUIOverlays();
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
child: Center(
child: Hero(
tag: 'imageHero',
child: CachedNetworkImage(
imageUrl:
'https://raw.githubusercontent.com/flutter/website/master/src/_includes/code/layout/lakes/images/lake.jpg',
placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error),
),
),
),
onTap: () {
Navigator.pop(context);
},
),
);
}
}

将数据从主页面传递到详细页面

为了完成我的回答,我添加了一些代码来展示如何将图像 url 从主页面传递到详细页面。

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/services.dart';

void main() => runApp(HeroApp());

class HeroApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image/Detail Demo',
home: MainScreen(),
);
}
}

class MainScreen extends StatefulWidget {
@override
_MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
var _url = [
'https://raw.githubusercontent.com/flutter/website/master/src/_includes/code/layout/lakes/images/lake.jpg',
'https://github.com/flutter/plugins/raw/master/packages/video_player/doc/demo_ipod.gif?raw=true'
];
var _tag = ['imageHero', 'imageHero2'];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Main Screen'),
),
body: ListView(
children: <Widget>[
GestureDetector(
child: Hero(
tag: _tag[0],
child: CachedNetworkImage(
imageUrl: _url[0],
placeholder: Center(child: Container(width: 32, height: 32,child: new CircularProgressIndicator())),
errorWidget: new Icon(Icons.error),
)),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return DetailScreen(tag: _tag[0], url: _url[0]);
}));
},
),
GestureDetector(
child: Hero(
tag: _tag[1],
child: CachedNetworkImage(
imageUrl: _url[1],
placeholder: Center(child: Container(width: 32, height: 32,child: new CircularProgressIndicator())),
errorWidget: new Icon(Icons.error),
)),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return DetailScreen(tag: _tag[1], url: _url[1]);
}));
},
),
],
),
);
}
}

class DetailScreen extends StatefulWidget {
final String tag;
final String url;

DetailScreen({Key key, @required this.tag, @required this.url})
: assert(tag != null),
assert(url != null),
super(key: key);

@override
_DetailScreenState createState() => _DetailScreenState();
}

class _DetailScreenState extends State<DetailScreen> {
@override
initState() {
SystemChrome.setEnabledSystemUIOverlays([]);
super.initState();
}

@override
void dispose() {
//SystemChrome.restoreSystemUIOverlays();
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
child: Center(
child: Hero(
tag: widget.tag,
child: CachedNetworkImage(
imageUrl: widget.url,
placeholder: Center(child: Container(width: 32, height: 32,child: new CircularProgressIndicator())),
errorWidget: new Icon(Icons.error),
),
),
),
onTap: () {
Navigator.pop(context);
},
),
);
}
}

更新

为了在图像外部弹出点击,请将 Detail 小部件中的 GestureDetector 移到外部。

class _DetailScreenState extends State<DetailScreen> {
@override
initState() {
SystemChrome.setEnabledSystemUIOverlays([]);
super.initState();
}

@override
void dispose() {
//SystemChrome.restoreSystemUIOverlays();
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
super.dispose();
}

@override
Widget build(BuildContext context) {
return GestureDetector(
child: Scaffold (
body: Center(
child: Hero(
tag: widget.tag,
child: CachedNetworkImage(
imageUrl: widget.url,
placeholder: Center(child: Container(width: 32, height: 32,child: new CircularProgressIndicator())),
errorWidget: new Icon(Icons.error),
),
),
),
),
onTap: () {
Navigator.pop(context);
},
);
}
}

关于dart - 在 Flutter 中显示全屏图像 onTap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54055187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com