Ink/IconButton color inside Container 在容器内的Ink/IconButton颜色

huangapple go评论81阅读模式
英文:

Ink/IconButton color inside Container

问题

我有一个位于Container内部的Ink中的IconButton(为其提供边框),但由于容器的颜色叠加在其上,边框不可见。

在Container内部显示具有有色背景的IconButton的最佳方法是什么?

工作示例:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.pink,
      child: Center(
        child: Ink(
          decoration: const ShapeDecoration(
            color: Colors.black,
            shape: CircleBorder(),
          ),
          child: IconButton(
            icon: Icon(Icons.android),
            color: Colors.white,
            onPressed: () {},
          ),
        ),
      ),
    );
  }
}
英文:

I have an IconButton inside an Ink (to give it a Border) which is inside a Container. However the Border is not visible since it is overlaid by the Containers color.

What is the best way to display an IconButton with a colored background inside a Container?

Working example:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
  return Container(
    color: Colors.pink,
    child: Center(
      child: Ink(
        decoration: const ShapeDecoration(
          color: Colors.black,
          shape: CircleBorder(),
        ),
        child: IconButton(
          icon: Icon(Icons.android),
          color: Colors.white,
          onPressed: () {},
        ),
      ),
    ),
  );
}

}

答案1

得分: 9

使用Material而不是Container (查看官方示例)

之所以Container不起作用是因为Ink是绘制在底层的Material小部件上,如Ink文档中所解释的:

> Ink涟漪和高亮,由InkWell和InkResponse渲染,
> 在实际的底层Material上绘制,在其上绘制任何小部件
> 覆盖材料(如文本和图标)。如果在Material上绘制不透明的图像
> (也许使用Container或DecoratedBox),
> 这些墨水效果将不可见,因为它们将完全被不透明的图形遮挡。

查看工作示例:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
  return Material(
    color: Colors.red,
    child: Center(
      child: Ink(
        decoration: const ShapeDecoration(
          color: Colors.black,
          shape: CircleBorder(),
        ),
        child: IconButton(
          icon: Icon(Icons.android),
          color: Colors.white,
          onPressed: () {},
        ),
      ),
    ),
  );
}

}

额外内容:

如果您真的需要一个不是Material的Container或其他小部件,只需在Ink小部件和父级小部件之间添加一个Material小部件(具有透明背景)。

像这样:

Container(
    color: Colors.red,
    child: Material(
    color: Colors.transparent,
    child: Center(
      child: Ink(
        decoration: const ShapeDecoration(
          color: Colors.black,
          shape: CircleBorder(),
        ),
        child: IconButton(
          icon: Icon(Icons.android),
          color: Colors.white,
          onPressed: () {},
        ),
      ),
    ),
  ))
英文:

Use Material instead of Container (see official example)

The reason Container doesn't work is because the Ink is drawn on the underlying Material widget, as explained in the Ink doc:

> Ink splashes and highlights, as rendered by InkWell and InkResponse,
> draw on the actual underlying Material, under whatever widgets are
> drawn over the material (such as Text and Icons). If an opaque image
> is drawn over the Material (maybe using a Container or DecoratedBox),
> these ink effects will not be visible, as they will be entirely
> obscured by the opaque graphics drawn above the Material.

See working demo:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
  return Material(
    color: Colors.red,
    child: Center(
      child: Ink(
        decoration: const ShapeDecoration(
          color: Colors.black,
          shape: CircleBorder(),
        ),
        child: IconButton(
          icon: Icon(Icons.android),
          color: Colors.white,
          onPressed: () {},
        ),
      ),
    ),
  );
}

}

EXTRA:

If you really need a Container or other widget that is not Material you just need to add a Material widget between the Ink Widget and the parent one. (with transparent background)

Like this:


Container(
    color: Colors.red,
    child: Material(
    color: Colors.transparent,
    child: Center(
      child: Ink(
        decoration: const ShapeDecoration(
          color: Colors.black,
          shape: CircleBorder(),
        ),
        child: IconButton(
          icon: Icon(Icons.android),
          color: Colors.white,
          onPressed: () {},
        ),
      ),
    ),
  ))

huangapple
  • 本文由 发表于 2020年1月7日 02:18:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定