如何添加按钮来显示此图像?
大家好,希望你们一切都好!
在这篇帖子中,
我弄清楚了如何放置刷新的图像在网络中。 但现在我想在开始显示图像之前添加一些按钮,当我按下时显示新图像。 (现在“onTap()”会发生这种情况)。
我试着按一个按钮,但没用。 有人可以帮助我吗?
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'colors.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
var title = 'Web Images';
return MaterialApp(
theme: ThemeData(
primarySwatch: primaryBlack,
accentColor: Colors.white,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
':(:',
style: TextStyle(fontWeight: FontWeight.bold),
),
Text('FelizTriste'),
Text(':(:', style: TextStyle(fontWeight: FontWeight.bold)),
],
),
),
body: ForcePicRefresh(),
));
}
}
class ForcePicRefresh extends StatefulWidget {
@override
_ForcePicRefreshState createState() => _ForcePicRefreshState();
}
class _ForcePicRefreshState extends State<ForcePicRefresh> {
String url = 'http://thecatapi.com/api/images/get?format=src&type=gif';
Widget _pic;
@override
void initState() {
_pic = Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 350,
width: 350,
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 6)),
child: Image.network(
url,
width: double.infinity,
fit: BoxFit.fill,
)),
Container(
child: ElevatedButton(onPressed: () {}, child: Text('GATIT@S')),
),
Container(
child: Text('¿Quieres ver gatit@s?'),
)
],
),
);
super.initState();
}
_updateImgWidget() async {
setState(() {
_pic = Center(child: CircularProgressIndicator());
});
Uint8List bytes = (await NetworkAssetBundle(Uri.parse(url)).load(url))
.buffer
.asUint8List();
setState(() {
_pic = Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 350,
width: 350,
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 6)),
child: Image.memory(
bytes,
width: double.infinity,
fit: BoxFit.fill,
),
),
Column(
children: [
Container(
child: ElevatedButton(onPressed: () {}, child: Text('GATIT@S')),
),
],
),
Container(
child: Text('¿Quieres ver gatit@s?'),
)
],
));
});
}
@override
Widget build(BuildContext context) {
return InkWell(
child: _pic,
onTap: () {
_updateImgWidget();
},
);
}
}
Hello to everyone i hope yall good!
In this post.
I figure out how to put an image that refresh in the network.
But now i want to add some button, before start to display the image, and when i press show the new image. (this now happen with "onTap()") .
I tried to put a button, but it dosent work.
Someone can help me please!!
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'colors.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
var title = 'Web Images';
return MaterialApp(
theme: ThemeData(
primarySwatch: primaryBlack,
accentColor: Colors.white,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
':(:',
style: TextStyle(fontWeight: FontWeight.bold),
),
Text('FelizTriste'),
Text(':(:', style: TextStyle(fontWeight: FontWeight.bold)),
],
),
),
body: ForcePicRefresh(),
));
}
}
class ForcePicRefresh extends StatefulWidget {
@override
_ForcePicRefreshState createState() => _ForcePicRefreshState();
}
class _ForcePicRefreshState extends State<ForcePicRefresh> {
String url = 'http://thecatapi.com/api/images/get?format=src&type=gif';
Widget _pic;
@override
void initState() {
_pic = Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 350,
width: 350,
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 6)),
child: Image.network(
url,
width: double.infinity,
fit: BoxFit.fill,
)),
Container(
child: ElevatedButton(onPressed: () {}, child: Text('GATIT@S')),
),
Container(
child: Text('¿Quieres ver gatit@s?'),
)
],
),
);
super.initState();
}
_updateImgWidget() async {
setState(() {
_pic = Center(child: CircularProgressIndicator());
});
Uint8List bytes = (await NetworkAssetBundle(Uri.parse(url)).load(url))
.buffer
.asUint8List();
setState(() {
_pic = Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 350,
width: 350,
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 6)),
child: Image.memory(
bytes,
width: double.infinity,
fit: BoxFit.fill,
),
),
Column(
children: [
Container(
child: ElevatedButton(onPressed: () {}, child: Text('GATIT@S')),
),
],
),
Container(
child: Text('¿Quieres ver gatit@s?'),
)
],
));
});
}
@override
Widget build(BuildContext context) {
return InkWell(
child: _pic,
onTap: () {
_updateImgWidget();
},
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在按下功能的提升按钮中,您没有做任何
改变:
In elevated button on pressed function, you were doing nothing
change it with this: