如何获得自定义的圆形进度指示器(如图像所示)?

时间:2020-05-10 19:38:46

标签: flutter dart flutter-layout loader flutter-animation

我试图以警报对话框类型获取此循环进度指示器。这是我的代码和下面的输出。

代码:

Future<void> loaderDialogNormal(BuildContext context) { 
    return showDialog<void>(
            context: context,
            builder: (BuildContext context) {
                 return  Dialog(
            shape: RoundedRectangleBorder(
                borderRadius:
                    BorderRadius.circular(20.0)),
            child: 
                 Container(
                   width: 50, height: 50,
                   child: CircularProgressIndicator()),
            );
            });
  }

我的输出:

my output for code

预期输出:

expected output

如何实现预期的输出?

2 个答案:

答案 0 :(得分:1)

您可以在下面复制粘贴运行完整代码
您可以用Center

包装
 Container(
                width: 50,
                height: 50,
                child: Center(child: CircularProgressIndicator())),
          );

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  Future<void> loaderDialogNormal(BuildContext context) {
    return showDialog<void>(
        context: context,
        builder: (BuildContext context) {
          return Dialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20.0)),
            child: Container(
                width: 50,
                height: 50,
                child: Center(child: CircularProgressIndicator())),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                loaderDialogNormal(context);
              },
              child: Text('click'),
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

答案 1 :(得分:1)

尝试此代码,即可根据需要运行

Future<void> loaderDialogNormal(BuildContext context) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (_) {
          return Center(
              child: Container(
                width: 100.0,
                height: 100.0,
                decoration: ShapeDecoration(
                  color: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(10.0),
                    ),
                  ),
                ),
                child: Center(
                  child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation<Color>(
                        Colors.grey),
                  ),
                ),
              ));
        });
  }