如何在Flutter中剪切透明度小部件?

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

How to cut through an Opacity Widget in flutter?

问题

我想要从容器中心去掉不透明度,以使其下面的图片可见。

我想要的效果:

如何在Flutter中剪切透明度小部件?

我尝试过的方法:

如何在Flutter中剪切透明度小部件?

我尝试的代码:

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

class AppFour extends StatefulWidget {
  @override
  _AppFourState createState() => _AppFourState();
}

class _AppFourState extends State<AppFour> {
  String img =
      'https://images.unsplash.com/photo-1513279922550-250c2129b13a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Container(
            height: MediaQuery.of(context).size.height,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: NetworkImage('$img'),
                fit: BoxFit.cover,
              ),
            ),
          ),
          Opacity(
            opacity: 0.5,
            child: Container(
              color: Colors.white,
            ),
          ),
          Center(
            child: Container(
              height: MediaQuery.of(context).size.height / 1.5,
              width: MediaQuery.of(context).size.width / 1.5,
              decoration: BoxDecoration(
                color: Colors.transparent,
                image: DecorationImage(
                    image: NetworkImage('$img'),
                    fit: BoxFit.fitHeight),
                borderRadius: BorderRadius.circular(5.0),
                border: Border.all(
                  color: Colors.white,
                  width: 2.0,
                ),
                boxShadow: [
                  BoxShadow(
                      color: Colors.black,
                      offset: Offset(0.0, 0.0),
                      blurRadius: 10.0)
                ],
              ),
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.grey[800],
        child: Transform.rotate(
          angle: 45,
          child: Icon(FontAwesomeIcons.syncAlt),
        ),
        onPressed: () {},
      ),
    );
  }
}
英文:

I want to remove opacity from the center of a container to make the picture below it to be visible.

What I want:

如何在Flutter中剪切透明度小部件?

What I tried:

如何在Flutter中剪切透明度小部件?

Code I Tried:

import &#39;package:flutter/material.dart&#39;;
import &#39;package:font_awesome_flutter/font_awesome_flutter.dart&#39;;

class AppFour extends StatefulWidget {
  @override
  _AppFourState createState() =&gt; _AppFourState();
}

class _AppFourState extends State&lt;AppFour&gt; {
  String img = &#39;https://images.unsplash.com/photo-1513279922550-250c2129b13a?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1050&amp;q=80&#39;;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: &lt;Widget&gt;[
          Container(
            height: MediaQuery.of(context).size.height,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: NetworkImage(&#39;$img&#39;),
                fit: BoxFit.cover,
              ),
            ),
          ),
          Opacity(
            opacity: 0.5,
            child: Container(
              color: Colors.white,
            ),
          ),
          Center(
            child: Container(
              height: MediaQuery.of(context).size.height / 1.5,
              width: MediaQuery.of(context).size.width / 1.5,
              decoration: BoxDecoration(
                color: Colors.transparent,
                image: DecorationImage(
                    image: NetworkImage(&#39;$img&#39;),
                    fit: BoxFit.fitHeight),
                borderRadius: BorderRadius.circular(5.0),
                border: Border.all(
                  color: Colors.white,
                  width: 2.0,
                ),
                boxShadow: [
                  BoxShadow(
                      color: Colors.black,
                      offset: Offset(0.0, 0.0),
                      blurRadius: 10.0)
                ],
              ),
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.grey[800],
        child: Transform.rotate(
          angle: 45,
          child: Icon(FontAwesomeIcons.syncAlt),
        ),
        onPressed: () {},
      ),
    );
  }
}

答案1

得分: 2

我已经修改了您的代码,以实现您提供的示例图像效果:

截图

如何在Flutter中剪切透明度小部件?

代码

String img = 'https://images.unsplash.com/photo-1513279922550-250c2129b13a?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1050&amp;q=80';
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Stack(
      children: <Widget>[
        Container(
          height: MediaQuery.of(context).size.height,
          decoration: BoxDecoration(
            image: DecorationImage(
              image: NetworkImage('$img'),
              fit: BoxFit.cover,
            ),
          ),
        ),
        Opacity(
          opacity: 0.5,
          child: Container(
            color: Colors.white,
          ),
        ),
        Center(
          child: Container(
            decoration: BoxDecoration(
              color: Colors.transparent,
              borderRadius: BorderRadius.circular(5.0),
              border: Border.all(
                color: Colors.white,
                width: 5.0,
              ),
              boxShadow: [
                BoxShadow(
                  color: Colors.black,
                  offset: Offset(0.0, 0.0),
                  blurRadius: 10.0,
                )
              ],
            ),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(5.0),
              child: Align(
                alignment: Alignment.center,
                widthFactor: 0.75,
                heightFactor: 0.75,
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.transparent,
                    image: DecorationImage(
                      image: NetworkImage('$img'),
                      fit: BoxFit.fitHeight,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ],
    ),
    floatingActionButton: FloatingActionButton(
      backgroundColor: Colors.grey[800],
      child: Transform.rotate(
        angle: 45,
        child: Icon(FontAwesomeIcons.syncAlt),
      ),
      onPressed: () {},
    ),
  );
}

<details>
<summary>英文:</summary>

I&#39;ve modified your code to have the effect of the image that you gave as an example:

**Screenshot**

[![enter image description here][1]][1]

**Code**

    String img = &#39;https://images.unsplash.com/photo-1513279922550-250c2129b13a?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1050&amp;q=80&#39;;
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Stack(
          children: &lt;Widget&gt;[
            Container(
              height: MediaQuery.of(context).size.height,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: NetworkImage(&#39;$img&#39;),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Opacity(
              opacity: 0.5,
              child: Container(
                color: Colors.white,
              ),
            ),
            Center(
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.transparent,
                  borderRadius: BorderRadius.circular(5.0),
                  border: Border.all(
                    color: Colors.white,
                    width: 5.0,
                  ),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black,
                      offset: Offset(0.0, 0.0),
                      blurRadius: 10.0
                    )
                  ],
                ),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(5.0),
                  child: Align(
                    alignment: Alignment.center,
                    widthFactor: 0.75,
                    heightFactor: 0.75,
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.transparent,
                        image: DecorationImage(
                          image: NetworkImage(&#39;$img&#39;),
                          fit: BoxFit.fitHeight
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.grey[800],
          child: Transform.rotate(
            angle: 45,
            child: Icon(FontAwesomeIcons.syncAlt),
          ),
          onPressed: () {},
        ),
      );
    }


  [1]: https://i.stack.imgur.com/wFehU.png

</details>



huangapple
  • 本文由 发表于 2020年1月3日 17:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576354.html
匿名

发表评论

匿名网友

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

确定