Flutter'da "buzlu cam" etkisini nasıl yaparım?


101

Bir Flutter uygulaması yazıyorum ve iOS'ta yaygın olan "buzlu cam" efektini kullanmak / uygulamak istiyorum. Bunu nasıl yaparım?


1
Okumak - Ben BackdropFilter & ImageFilter kullanarak Flutter üzerinde bulanıklık efekti nasıl göstermek için bir makale yazmak Aracı'sı
— anticafe

Bulanık konteynır paketini kullanabilirsiniz .
— Jon

Yanıtlar:


174

Bu efekti elde etmek için BackdropFilter widget'ını kullanabilirsiniz.

ekran görüntüsü

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

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

class FrostedDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Stack(
        children: <Widget>[
          new ConstrainedBox(
            constraints: const BoxConstraints.expand(),
            child: new FlutterLogo()
          ),
          new Center(
            child: new ClipRect(
              child: new BackdropFilter(
                filter: new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
                child: new Container(
                  width: 200.0,
                  height: 200.0,
                  decoration: new BoxDecoration(
                    color: Colors.grey.shade200.withOpacity(0.5)
                  ),
                  child: new Center(
                    child: new Text(
                      'Frosted',
                      style: Theme.of(context).textTheme.display3
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Buzlu efektin uygulamanın tüm genişliğini / yüksekliğini kaplamasını nasıl sağlayabilirim?
— Pieter


2
veya buzlu cam efektini diyaloglar için kalıcı bir bariyer olarak kullanmaya çalışıyorsanız, ModalBarrier kopyanızı bir BackdropFilter içerecek şekilde değiştirebilirsiniz. github.com/flutter/flutter/blob/master/packages/flutter/lib/src/…
— Collin Jackson

Maalesef bulanıklık efekti iOS cihazlarda çalışmıyor: github.com/flutter/flutter/issues/10284
— Mark

3
iirc, yukarıdaki sorun çözüldü ve bulanıklaştırma efekti artık iOS cihazlarda çalışıyor :)
— loganrussell48

13

Sanırım 'Buzlu' kelimesinin tam anlamını bilmiyorum (Örneğim burada işe yaramadıysa),

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

void main() => runApp(
    MaterialApp(
        title: "Frosted glass",
        home: new HomePage()
    )
);

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Stack(
        fit: StackFit.expand,
        children: <Widget>[
          generateBluredImage(),
          new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              rectShapeContainer(),
            ],
          ),
        ],
      ),
    );
  }

  Widget generateBluredImage() {
    return new Container(
      decoration: new BoxDecoration(
        image: new DecorationImage(
          image: new AssetImage('assets/images/huxley-lsd.png'),
          fit: BoxFit.cover,
        ),
      ),
      //I blured the parent container to blur background image, you can get rid of this part
      child: new BackdropFilter(
        filter: new ui.ImageFilter.blur(sigmaX: 3.0, sigmaY: 3.0),
        child: new Container(
          //you can change opacity with color here(I used black) for background.
          decoration: new BoxDecoration(color: Colors.black.withOpacity(0.2)),
        ),
      ),
    );
  }

  Widget rectShapeContainer() {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 40.0, vertical: 10.0),
      padding: const EdgeInsets.all(15.0),
      decoration: new BoxDecoration(
        //you can get rid of below line also
        borderRadius: new BorderRadius.circular(10.0),
        //below line is for rectangular shape
        shape: BoxShape.rectangle,
        //you can change opacity with color here(I used black) for rect
        color: Colors.black.withOpacity(0.5),
        //I added some shadow, but you can remove boxShadow also.
        boxShadow: <BoxShadow>[
          new BoxShadow(
            color: Colors.black26,
            blurRadius: 5.0,
            offset: new Offset(5.0, 5.0),
          ),
        ],
      ),
      child: new Column(
        children: <Widget>[
          new Text(
            'There\'s only one corner of the universe you can be certain of improving and that\'s your own self.',
            style: new TextStyle(
              color: Colors.white,
              fontSize: 20.0,
            ),
          ),
        ],
      ),
    );
  }
}

Sonuç:

görüntü açıklamasını buraya girin

Umarım bu birine yardımcı olur.


1
tamamen yardımcı oldu. 'Yığın' seçeneğini tamamen unuttum ... çok teşekkürler.
— AhabLives

Buzlanmanın bir ana kabın şeklini takip etmesini nasıl sağlayabilirsiniz? dairesel şekilli bir kabın içine eklendiğinde hala dikdörtgen görünüyor
— Kaki Master Of Time

@KakiMasterOfTime Sorunuzu doğru anlamadım sanırım. Ancak, borderRadius'u kaldırarak rectShape kap şeklini daire yaparsanız işe yarayacaktır.
— Blasanka


0
BackdropFilter(
  filter: ImageFilter.blur(sigmaX: _sigmaX, sigmaY: _sigmaY),
  child: Container(
    color: Colors.black.withOpacity(_opacity),
  ),
),
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.