Hello everyone I hope yall good. I want to know how to create a button and when it get pressed display an image and a column or row.
Please help me.
**NOW THE IMAGE APPEARS AND DISSAPEARS WHEN THE BUTTON ITS PRESSED. But it dont refresh to get a new one (its a dynamic url)
***Now the image change when it pressed, but i dont know where i can put the button.
UPDATE CODE 4.0
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: 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,
)),
);
super.initState();
}
_updateImgWidget() async {
setState(() {
_pic = Center(child: CircularProgressIndicator());
});
Uint8List bytes = (await NetworkAssetBundle(Uri.parse(url)).load(url))
.buffer
.asUint8List();
setState(() {
_pic = Center(
child: 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,
)));
SizedBox(height: 200);
ElevatedButton(onPressed: () {}, child: Text('Hola'));
});
SizedBox(height: 200);
ElevatedButton(onPressed: () {}, child: Text('Hola'));
}
@override
Widget build(BuildContext context) {
return InkWell(
child: _pic,
onTap: () {
_updateImgWidget();
},
);
}
}
I tried to add a Container, for the purpose of reduce or increase the image so it can fit perfect, but i didnt work.
Also, the url its a dynamic one, so i want to "refresh" the image display with each button hope you can help me.
Going to learn this method: Flutter: refresh network image
CodePudding user response:
I have modified your code:
removed image function, instead create a variable, bool showImage which should be false initially.
When the button is pressed, showImage should be changed to true.
ElevatedButton( onPressed: () { setState(() { showImage = true; }); }, child: Text('GATIT@S') ),
The image and text should only be displayed when showImage variable is true, otherwise display empty SizedBox().
showImage ? Column( children: [ Image.network( 'https://docs.flutter.dev/assets/images/dash/dash-fainting.gif'), Text('Hola') ], ) : SizedBox( height: 0, ),