Home > OS >  How to add button to display this images?
How to add button to display this images?

Time:04-02

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();
      },
    );
  }
}

CodePudding user response:

In elevated button on pressed function, you were doing nothing

child: ElevatedButton(onPressed: () {}, child: Text('GATIT@S')),

change it with this:

ElevatedButton(
                onPressed: () {
                  _updateImgWidget();
                },
                child: Text('GATIT@S'))
  • Related