Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the highlight color of an IconButton appear over parent widget?

Tags:

flutter

When I set the color of a Container that holds an IconButton, I find that highlight color of the IconButton is hidden by the color of the container. Here's what I mean:

enter image description here

How can ensure that the blue circle appears above the red square?

Here's my code:

import 'dart:ui';
import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(home: new MyDemo()));
}

class MyDemo extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new Container(
          width: 60.0,
          height: 60.0,
          color: Colors.red,
          child: new IconButton(
            highlightColor: Colors.blue,
            icon: new Icon(Icons.add_a_photo), onPressed: ()=>{},),
        ),
      ),
    );
  }
}
like image 315
Mark Avatar asked Oct 11 '17 06:10

Mark


People also ask

How do I change the color of my IconButton?

Steps to change icon button color in FlutterLocate the file where you have placed the IconButton widget. Inside the IconButton widget, add the color parameter and assign the color of your choice. Run the App.

How can I change the color of my icon when I pressed in Flutter?

Here's how you do it:Step 1: Locate the MaterialApp widget. Step 2: Inside the MaterialApp, add the theme parameter with ThemeData class assigned. Step 3: Inside the ThemeData add the iconTheme parameter and then assign the IconThemeData . Step 4:Inside the IconThemeData add the color parameter and set its color.

How do you make an icon clickable on a Flutter?

How to Make Icon Clickable in Flutter? You can wrap Icon() widget with InkWell or alternatively GestureDetector() widget to make icons clickable in your Flutter app.

How do you create a raised button with icon and text?

The simplest way to create a button with icon and text in Flutter is to use the new Material button called ElevatedButton with an icon constructor. ElevatedButton. icon() gives you the ability to add the icon and label parameter to the button. The ElevatedButton was introduced with the release of Flutter v1.


2 Answers

InkSplash occurs on the closest ancestor Material widget.

You can get that widget using Material.of(context) which provides a few helpers for InkSplashes.

In your case, it's InkResponse instantiated by IconButton which provoke the Splash effect. But the targeted Material widget is instantiated by Scaffold. Which is an ancestor of your background. Therefore the background paints above your InkSplash.

To solve this problem you'd have to introduce a new Material instance between your background and your IconButton.

Which leads to :

enter image description here

Guh, we solved the problem. But now it's cropped ! Let's continue.

The easiest option would be to split your rendering in two branches. One for the background, and one for the UI. Something similar should do the trick :

return new Scaffold(
  body: new Stack(
    fit: StackFit.expand,
    children: <Widget>[
      new Center(
        child: new Container(
          height: 60.0,
          width: 60.0,
          color: Colors.red,
        ),
      ),
      new Material(
        type: MaterialType.transparency,
        child: new IconButton(
          highlightColor: Colors.blue,
          icon: new Icon(Icons.add_a_photo),
          onPressed: () => {},
        ),
      ),
    ],
  ),
);

enter image description here

like image 100
Rémi Rousselet Avatar answered Oct 10 '22 02:10

Rémi Rousselet


Try this

 Material(
    color: Colors.transparent,
    shape: CircleBorder(),
    clipBehavior: Clip.hardEdge,
    child: IconButton(
      icon: Icon(Icons.arrow_back),
      iconSize: 30,
      color: Colors.white,
      onPressed: () {

      },
    ),
  );
like image 33
Navin Kumar Avatar answered Oct 10 '22 03:10

Navin Kumar