Flutter : create an overlay progress bar

前端 未结 1 550
予麋鹿
予麋鹿 2021-01-22 13:41

How do I create an overlay page in Flutter.

Basically, today I have a spinner in the center of my page. Now, I want the current page to fade (90% opacity) and show the

相关标签:
1条回答
  • 2021-01-22 13:54

    Try with this.

            class SamplePage extends StatefulWidget {
            @override
            _SamplePageState createState() => _SamplePageState();
            }
    
            class _SamplePageState extends State<SamplePage> {
            ProgressBar _sendingMsgProgressBar;
    
            @override
            void initState() {
                super.initState();
                _sendingMsgProgressBar = ProgressBar();
            }
    
            @override
            void dispose() {
                _sendingMsgProgressBar.hide();
                super.dispose();
            }
    
            void showSendingProgressBar() {
                _sendingMsgProgressBar.show(context);
            }
    
            void hideSendingProgressBar() {
                _sendingMsgProgressBar.hide();
            }
    
            @override
            Widget build(BuildContext context) {
                return Scaffold(
                appBar: AppBar(
                    title: Text('Progress Demo'),
                ),
                body: Center(
                    child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                        RaisedButton(
                        child: Text("Start Progress"),
                        onPressed:(){
                            showSendingProgressBar();
                            Future.delayed(const Duration(milliseconds: 3000), () {
                            setState(() {
                                hideSendingProgressBar();
                            });
                            });
                        },
                        )
                    ],
                    ),
                ),
                );
            }
            }
    

    ProgressBar class

            import 'package:flutter/material.dart';
    
            class ProgressBar {
            
            OverlayEntry _progressOverlayEntry;
            
            void show(BuildContext context){
                _progressOverlayEntry = _createdProgressEntry(context);
                Overlay.of(context).insert(_progressOverlayEntry);
            }
            
            void hide(){
                if(_progressOverlayEntry != null){
                _progressOverlayEntry.remove();
                _progressOverlayEntry = null;
                }
            }
            
            OverlayEntry _createdProgressEntry(BuildContext context) =>
                OverlayEntry(
                    builder: (BuildContext context) =>
                        Stack(
                            children: <Widget>[
                            Container(
                                color: Colors.lightBlue.withOpacity(0.5),
                            ),
                            Positioned(
                                top: screenHeight(context) / 2,
                                left: screenWidth(context) / 2,
                                child: CircularProgressIndicator(),
                            )
    
                            ],
    
                        )
                );
    
            double screenHeight(BuildContext context) =>
                MediaQuery.of(context).size.height;
    
            double screenWidth(BuildContext context) =>
                MediaQuery.of(context).size.width;
            
            }
    

    0 讨论(0)
提交回复
热议问题