我正在尝试创建一个按钮,该按钮在按下时会执行一些操作,假设它调用函数 _pressedButton() 如果用户点击它(或点击并按住),图像也会发生变化。
将此视为按下自定义图片的按钮。
在后续,我是否也可以根据一些外部因素更改图像? 例如。如果某个函数 A 返回 true,则显示图像 1,否则显示图像 2
最佳答案
您可以在 Flutter interactivity tutorial 中了解有关按钮和状态的所有信息。 .
例如,这里有一个按钮,每次点击时都会显示不同的猫。
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),
),
),
),
);
}
}
https://stackoverflow.com/questions/44497289/
相关文章:
flutter - 使用谷歌地图的两个屏幕返回应用程序的问题
firebase - 我可以使用 StreamBuilder 在 Firestore 中收听单个文档
android - Gradle 失败可能是因为这个 Flutter 应用程序中的 AndroidX
flutter - Cocoapods 找不到 pod Firebase 的兼容版本
dart - 如何在 dart 中使用 request.send() 获取响应正文
flutter - 在 Dart 中添加/减去迄今为止的月/年?