Flutter中按钮中的自定义图像

Sam*_*jan 4 flutter

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

将此视为按下时自定义图片的按钮.

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

Col*_*son 7

您可以在Flutter交互式教程中了解有关按钮和状态的所有信息.

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

猫按钮

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),
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)