본문 바로가기

study/flutter

플러터(flutter) ProgressDialog 사용

pubspec.yaml 파일에 패키지 추가

패키지 import

버튼 클릭시 ProgressDialog가 5초동안 동작 후 멈춤

멈출때에는 Navigator.of(context, rootNavigator: true).pop(); 를 사용함

import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sn_progress_dialog/progress_dialog.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const CupertinoApp(
    home: MyApp(),
    debugShowCheckedModeBanner: false,
    showSemanticsDebugger: false,
  ));
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      debugShowMaterialGrid: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  ProgressDialog? pd;

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Scaffold(
          appBar: AppBar(
            // Here we take the value from the MyHomePage object that was created by
            // the App.build method, and use it to set our appbar title.
            title: Text(widget.title),
          ),
          body: Column(
            children: [
              Container(
                width: MediaQuery.of(context).size.width,
                padding: const EdgeInsets.only(left: 10, right: 10),
                child: CupertinoButton(
                  color: Color(0xff117a4f),
                  child: Text('테스트 버튼'),
                  onPressed: () => {
                    PdTest(),
                  },
                ),
              ),
            ],
          )),
    );
  }

  // 버튼 클릭시 동작 함수
  void PdTest() async {
    try {
      print("!1");
      customProgress();

      await Future.delayed(const Duration(seconds: 5));
      Navigator.of(context, rootNavigator: true).pop();
    } catch (e) {
      print('eeeeeeee');
    }
  }

  // ProgressDialog 함수
  customProgress() async {
    pd = ProgressDialog(context: context);
    pd!.show(
      max: 1,
      msg: '로그인중...',
      progressType: ProgressType.normal,
      backgroundColor: Colors.red,
      progressValueColor: Colors.black,
      progressBgColor: Colors.white70,
      elevation: 0,
      msgColor: Colors.white,
      valueColor: Colors.white,
    );
    pd!.update(value: 1, msg: '로그인중...');
  }
}