Amfani Overlay .of a cikin Flutter: Jagora da Misali

Overlay.of hanya ce a tsaye Flutter wacce ake amfani da ita don dawo OverlayState da widget din kakanni mafi kusa Overlay.

Ana amfani da widget din Overlay don ƙirƙirar tarin widget din da za a iya nunawa a saman wasu widget din a Flutter aikace. Hanyar Overlay.of tana ba ku damar samun dama ga OverlayState abin da ke hade da takamaiman BuildContext.

Ga misalin yadda zaku yi amfani da Overlay.of su don samun damar shiga OverlayState:

import 'package:flutter/material.dart';  
  
void main() {  
  runApp(MyApp());  
}  
  
class MyApp extends StatelessWidget {  
  @override  
  Widget build(BuildContext context) {  
    return MaterialApp(  
      home: MyHomePage(),  
   );  
  }  
}  
  
class MyHomePage extends StatelessWidget {  
  @override  
  Widget build(BuildContext context) {  
    return Scaffold(  
      appBar: AppBar(  
        title: Text('Overlay.of Example'),  
     ),  
      body: Center(  
        child: ElevatedButton(  
          onPressed:() {  
            OverlayState overlayState = Overlay.of(context);  
            OverlayEntry overlayEntry = OverlayEntry(  
              builder:(BuildContext context) {  
                return Positioned(  
                  top: 100,  
                  left: 50,  
                  child: Container(  
                    width: 100,  
                    height: 100,  
                    color: Colors.blue,  
                 ),  
               );  
              },  
           );  
            overlayState.insert(overlayEntry);  
          },  
          child: Text('Show Overlay'),  
       ),  
     ),  
   );  
  }  
}  

A cikin wannan misali, lokacin da aka danna maɓallin, Overlay.of ana amfani da hanyar don dawo da OverlayState abin da ke hade da na yanzu BuildContext. OverlayEntry An ƙirƙiri an ƙara shi zuwa hanyar overlay amfani insert da hanyar OverlayState. Wannan yana nuna akwati mai shuɗi a wani takamaiman matsayi a saman wasu widget din.

Lura cewa amfani overlay yana buƙatar kulawa da hankali, kuma ya kamata ka yawanci cire shigarwar daga overlay lokacin da ba a buƙatar su don guje wa ɓoyayyen ƙwaƙwalwar ajiya.

Idan an sami wasu sabuntawa ko canje-canje masu alaƙa Overlay.of bayan sabuntawa na ƙarshe, Ina ba da shawarar duba Flutter takaddun don sabbin bayanai.