10

I'm new to Flutter programming and I want to ask is it possible to make image as button background flutter? Here's my image asset:

  final _backgroundButton = new AssetImage("assets/background_button.png");

and here's my button:

RaisedButton(
          child: const Text('LANJUTKAN'),
          color: Theme.of(context).accentColor,

          elevation: 0.0,
          splashColor: Colors.blueGrey,
          onPressed: () {
               // Perform some action
          },
),

Anyone know how to do it? it's okay for me to change the raised button to flat button or anything else as long as I can set image as background. Thank you!

0

4 Answers 4

24

the "RaisedButton" is a material component , its take it shape depends on "material design" roles , you can create your own custom button widget

    GestureDetector(
      child: Container(
       width:120,
       height: 40,
       decoration: BoxDecoration(
         color: Colors.black,
         image: DecorationImage(
           image:AssetImage("assets/background_button.png"), 
           fit:BoxFit.cover
         ),
       child: Text("clickMe") // button text
       )
      ),onTap:(){
       print("you clicked me");
      }
    )
Sign up to request clarification or add additional context in comments.

2 Comments

child: Text("clickMe") is for Container
Anyone using this solution should also wrap with Semantics() and add support for keyboard focus to maintain accessibility.
4

If anyone else come here looking for this, MaterialButton works perfectly.

      MaterialButton(
        padding: EdgeInsets.all(8.0),
        textColor: Colors.white,
        splashColor: Colors.greenAccent,
        elevation: 8.0,
        child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage('assets/button_color.png'),
                fit: BoxFit.cover),
          ),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text("SIGN OUT"),
          ),
        ),
        // ),
        onPressed: () {
          print('Tapped');
        },
      ),

Comments

0

You can create custom widget

Expanded( child: Container( child: ConstrainedBox( constraints: BoxConstraints.expand(), child: Ink.image( image: AssetImage( 'path/the_image.png'), fit: BoxFit.fill, child: InkWell( onTap: null, ), ), ), ), ),

Or

In Raised button use Image() as a child instead of Text(). If both text and image are required just use Row() or Column() widget as a child.

If just an icon is required in a button use IconButton instead of RaisedButton

Comments

-1

As @pskink mentioned change your RaisedButton child from Text to Image, like this

RaisedButton(
          child: const AssetImage("assets/background_button.png"),
          color: Theme.of(context).accentColor,

          elevation: 0.0,
          splashColor: Colors.blueGrey,
          onPressed: () {
               // Perform some action
          },
),

But if you want both Text and Image then use Column or Row as child of RaisedButton and put both as children,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.