Custom image in a button in Flutter

前端 未结 1 1332
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 09:57

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

1条回答
  •  孤独总比滥情好
    2021-01-18 10:11

    You can learn all about buttons and state in the Flutter interactivity tutorial.

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

    0 讨论(0)
提交回复
热议问题