【问题标题】:How do I make a button with an Image in flutter that highlights while someone is pressing it?如何制作一个带有颤动图像的按钮,当有人按下它时会突出显示?
【发布时间】:2018-02-13 05:28:11
【问题描述】:

我有一个小时候有图像的按钮。我想要它,以便在按下按钮时,图像更改为不同的图像,并且当用户停止按下按钮时,它会返回到其原始图像。

基本上我希望它像一个凸起的按钮,但带有用于凸起和按下状态的自定义图像。

以下是相关代码:

class LoginButton extends StatefulWidget {
  @override
  _LoginButtonState createState() => new _LoginButtonState();
}

class _LoginButtonState extends State<LoginButton> {
  void _onClicked() {
    setState(() {
      //I don't know what I should put here to cause the image to redraw 
      //only on button press

    });
  }

  @override
  Widget build(BuildContext context) {
    var assetImage = new AssetImage("assets/loginscreen/btn.png");
    var image = new Image(image: assetImage, height: 50.0, width: 330.0);
    return new Container(
      height: image.height,
      width: image.width,
      child: new FlatButton(
        onPressed: _onClicked,
        child: new ConstrainedBox(
          constraints: new BoxConstraints.expand(),
          child: image,
        ),
      ),
    );
  }
}

【问题讨论】:

  • 我已经用更相关的代码更新了我的答案,以实现您想要实现的目标。

标签: dart flutter


【解决方案1】:

对我来说,我想要一个没有额外填充的按钮,当我按下它时会变暗! 这是对我有用的代码:

  Container(
            height: image.height,
            width: image.width,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('/images/image.jpeg'),
              ),
            ),
            child: new FlatButton(
                padding: EdgeInsets.all(0.0),
                onPressed: () {},
                child: null),
          ),

感谢https://www.youtube.com/watch?v=Ze-BZBv85Ck!这个视频帮了我这个忙! :)

【讨论】:

    【解决方案2】:

    您可以将默认图像定义为_LoginButtonState类的属性,就像这样

    class _LoginButtonState extends State<LoginButton> {
    String _myImage = "assets/loginscreen/btn.png"; ... }
    

    现在你的 _onClick 方法应该包含状态的变化,你可以简单地使用 if 条件将 _myImage 的字符串更改为新图像

        void _onClicked() {
            setState(() {
              if (_myImage == "assets/loginscreen/btn.png"){
                _myImage = "<my new asset>";   //change myImage to the other one
                 }   
              else {
                     _myImage = "assets/loginscreen/btn.png"; //change myImage back to the original one
                    }
            });
          }
    

    在您的小部件构建中:

    var assetImage = new AssetImage(_myImage);
    

    ============================================== ================================

    更新

    我已经设法使用 GesutureDetector 实现了与您尝试实现的类似的想法,在这里我用颜色对其进行了测试,但它与更改图像的链接应该没有太大区别,尽管我假设重新绘制图像会比改变颜色慢。

    这是我使用的完整代码:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(new MaterialApp(
        home: new MyApp (),
      ));
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => new _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      var _myColor = Colors.blue;
    
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text ("Tap me!"),
            centerTitle: true,
          ),
          body: new GestureDetector(
            onTapDown:(TapDownDetails details) { setState(() {
          _myColor = Colors.orange;
        });
            },
            onTapUp: (TapUpDetails details) {
              setState(() {
                _myColor = Colors.blue;
              });
            },
    
            child: new Container (
              color: _myColor,
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 我在发布此答案后注意到帖子中的更新,我不确定您所说的未按下时的更改是什么意思?
    • 感谢您的回答,但我希望仅在按下时重绘图像,并在不再按下按钮时更改回原始图像
    • 我试图模拟一个普通按钮,当somone按下它时看起来被按下,当somone不再按下它时返回到未按下状态
    • 我不确定您要查找的是否是 onPressed 操作,我想我们在这里谈论的是聆听用户对按钮顶部的触摸。 docs.flutter.io/flutter/widgets/Listener-class.html
    • 啊,我想这可能是它。你知道我在哪里可以找到正在使用的听众的例子吗?我有点难以理解这一点
    猜你喜欢
    • 2017-01-25
    • 2011-07-04
    • 1970-01-01
    • 2012-12-10
    • 2014-11-26
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多