Flutter中的按钮中的自定义图像 [英] Custom image in a button in Flutter

查看:435
本文介绍了Flutter中的按钮中的自定义图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个按钮,该按钮在按下时会执行某些操作,假设如果用户在其上点击(或按住),它也会调用_pressedButton()函数,则图像会发生变化.

I'm trying to create a button which will do some action when pressed, suppose it calls a function _pressedButton() also if the user taps on it (or taps and hold), the image changes.

将其视为类似于按下自定义图片的按钮.

Consider this like a button with a custom picture on pressed.

在后续工作中,我是否还可以基于某些外部因素来更改图像? 例如.如果某个函数A返回true,则显示图片1,否则显示图片2

In the follow-up, can I also change the image based on some external factor? eg. if some function A returns true, show image 1, else show image 2

推荐答案

您可以在 Flutter互动教程.

例如,这是一个按钮,每次单击时都会显示不同的猫.

For example, here is a button that shows a different cat every time each time it is clicked.

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new HomePage(),
  ));
}

class HomePage extends StatefulWidget {
  HomePageState createState() => new HomePageState();
}

class HomePageState extends State<HomePage> {
  String _url = getNewCatUrl();

  static String getNewCatUrl() {
    return 'http://thecatapi.com/api/images/get?format=src&type=jpg&size=small'
           '#${new DateTime.now().millisecondsSinceEpoch}';
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Cat Button'),
      ),
      body: new Center(
        child: new FloatingActionButton(
          onPressed: () {
            setState(() {
              _url = getNewCatUrl();
            });
          },
          child: new ConstrainedBox(
            constraints: new BoxConstraints.expand(),
            child: new Image.network(_url, fit: BoxFit.cover, gaplessPlayback: true),
          ),
        ),
      ),
    );
  }
}

这篇关于Flutter中的按钮中的自定义图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆