颤动按钮以在其上方显示图像

发布于 2025-01-18 03:55:31 字数 3134 浏览 0 评论 0原文

大家好,希望你们一切顺利。 我想知道如何创建一个按钮,并在按下按钮时显示图像和列或行。

请帮我。

**现在,按下按钮时图像会出现并消失。 但它不会刷新以获得新的(它是一个动态 URL)

***现在按下按钮时图像会发生变化,但我不知道可以将按钮放在哪里。

更新代码5.0

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

import 'colors.dart';

const String url = 'http://thecatapi.com/api/images/get?format=src&type=gif';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme:
            ThemeData(primarySwatch: primaryBlack, accentColor: Colors.white),
        debugShowCheckedModeBanner: false,
        home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late Uint8List bytes;
  bool loading = true;

  @override
  void didChangeDependencies() async {
    getBytes();
    super.didChangeDependencies();
  }

  void getBytes() async {
    setState(() {
      loading = true;
    });

    Uint8List newbytes = (await NetworkAssetBundle(Uri.parse(url)).load(url))
        .buffer
        .asUint8List();

    setState(() {
      bytes = newbytes;
      loading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              ':(:',
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            Text('FelizTriste'),
            Text(':(:', style: TextStyle(fontWeight: FontWeight.bold)),
          ],
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            loading
                ? const SizedBox(
                    width: 355,
                    height: 355,
                    child: Center(child: CircularProgressIndicator()))
                : Container(
                    decoration: BoxDecoration(
                        border: Border.all(color: primaryBlack, width: 6)),
                    child: Image.memory(
                      bytes,
                      width: 350,
                      height: 350,
                      fit: BoxFit.cover,
                    ),
                  ),
            SizedBox(
              height: 60,
            ),
            ElevatedButton(onPressed: getBytes, child: const Text('GATIT@S')),
            const Text(
              '¿Quieres ver gatit@s?',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            )
          ],
        ),
      ),
    );
  }
}

我尝试添加一个容器,目的是减少或增加图像,使其完美适合,但没有成功。

另外,URL 是动态的,所以我想用每个按钮“刷新”图像显示希望你能帮助我。

打算学习一下这个方法: Flutter:刷新网络图像

Hello everyone I hope yall good.
I want to know how to create a button and when it gets pressed display an image and a column or row.

Please help me.

**NOW THE IMAGE APPEARS AND DISAPPEARS WHEN THE BUTTON IS PRESSED.
But it don't refreshes to get a new one (it's a dynamic URL)

***Now the image change when it is pressed, but I don't know where I can put the button.

UPDATE CODE 5.0

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

import 'colors.dart';

const String url = 'http://thecatapi.com/api/images/get?format=src&type=gif';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme:
            ThemeData(primarySwatch: primaryBlack, accentColor: Colors.white),
        debugShowCheckedModeBanner: false,
        home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late Uint8List bytes;
  bool loading = true;

  @override
  void didChangeDependencies() async {
    getBytes();
    super.didChangeDependencies();
  }

  void getBytes() async {
    setState(() {
      loading = true;
    });

    Uint8List newbytes = (await NetworkAssetBundle(Uri.parse(url)).load(url))
        .buffer
        .asUint8List();

    setState(() {
      bytes = newbytes;
      loading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              ':(:',
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            Text('FelizTriste'),
            Text(':(:', style: TextStyle(fontWeight: FontWeight.bold)),
          ],
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            loading
                ? const SizedBox(
                    width: 355,
                    height: 355,
                    child: Center(child: CircularProgressIndicator()))
                : Container(
                    decoration: BoxDecoration(
                        border: Border.all(color: primaryBlack, width: 6)),
                    child: Image.memory(
                      bytes,
                      width: 350,
                      height: 350,
                      fit: BoxFit.cover,
                    ),
                  ),
            SizedBox(
              height: 60,
            ),
            ElevatedButton(onPressed: getBytes, child: const Text('GATIT@S')),
            const Text(
              '¿Quieres ver gatit@s?',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            )
          ],
        ),
      ),
    );
  }
}

I tried to add a Container, for the purpose of reducing or increasing the image so it can fit perfectly, but it didn't work.

Also, the URL is 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

四叶草在未来唯美盛开 2025-01-25 03:55:31

我已经修改了您的代码:

  1. 删除了图像函数,而是创建一个最初应为false的变量,布尔。

  2. 按下按钮时,应更改为true。

     升高的丁顿(
       onpressed :(){
         setState((){
           showimage = true;
         });
       },,
       孩子:文字('gatit@s')
    ),
     
  3. 仅在showimage变量为真时才能显示图像和文本,否则显示空sizedbox()。

      showimage
       ?柱子(
           孩子们: [
             image.network('https://docs.flutter.dev/assets/images/dash/dash-fainting.gif'),),
             文字('Hola')
           ],,
         )
       :sizedbox(
           身高:0,
         ),
     

I have modified your code:

  1. removed image function, instead create a variable, bool showImage which should be false initially.

  2. When the button is pressed, showImage should be changed to true.

     ElevatedButton(
       onPressed: () {
         setState(() {
           showImage = true;
         });
       },
       child: Text('GATIT@S')
    ),
    
  3. 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,
         ),
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文