|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +/// Flutter code sample for [CircularProgressIndicator]. |
| 6 | +
|
| 7 | +import 'package:flutter/material.dart'; |
| 8 | + |
| 9 | +void main() => runApp(const ProgressIndicatorApp()); |
| 10 | + |
| 11 | +class ProgressIndicatorApp extends StatelessWidget { |
| 12 | + const ProgressIndicatorApp({super.key}); |
| 13 | + |
| 14 | + @override |
| 15 | + Widget build(BuildContext context) { |
| 16 | + return MaterialApp( |
| 17 | + theme: ThemeData(useMaterial3: true, colorSchemeSeed: const Color(0xff6750a4)), |
| 18 | + home: const ProgressIndicatorExample(), |
| 19 | + ); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +class ProgressIndicatorExample extends StatefulWidget { |
| 24 | + const ProgressIndicatorExample({super.key}); |
| 25 | + |
| 26 | + @override |
| 27 | + State<ProgressIndicatorExample> createState() => _ProgressIndicatorExampleState(); |
| 28 | +} |
| 29 | + |
| 30 | +class _ProgressIndicatorExampleState extends State<ProgressIndicatorExample> |
| 31 | + with TickerProviderStateMixin { |
| 32 | + late AnimationController controller; |
| 33 | + bool determinate = false; |
| 34 | + |
| 35 | + @override |
| 36 | + void initState() { |
| 37 | + controller = AnimationController( |
| 38 | + /// [AnimationController]s can be created with `vsync: this` because of |
| 39 | + /// [TickerProviderStateMixin]. |
| 40 | + vsync: this, |
| 41 | + duration: const Duration(seconds: 2), |
| 42 | + )..addListener(() { |
| 43 | + setState(() {}); |
| 44 | + }); |
| 45 | + controller.repeat(reverse: true); |
| 46 | + super.initState(); |
| 47 | + } |
| 48 | + |
| 49 | + @override |
| 50 | + void dispose() { |
| 51 | + controller.dispose(); |
| 52 | + super.dispose(); |
| 53 | + } |
| 54 | + |
| 55 | + @override |
| 56 | + Widget build(BuildContext context) { |
| 57 | + return Scaffold( |
| 58 | + body: Padding( |
| 59 | + padding: const EdgeInsets.all(20.0), |
| 60 | + child: Column( |
| 61 | + mainAxisAlignment: MainAxisAlignment.center, |
| 62 | + children: <Widget>[ |
| 63 | + Text( |
| 64 | + 'Circular progress indicator', |
| 65 | + style: Theme.of(context).textTheme.titleLarge, |
| 66 | + ), |
| 67 | + const SizedBox(height: 30), |
| 68 | + CircularProgressIndicator( |
| 69 | + value: controller.value, |
| 70 | + semanticsLabel: 'Circular progress indicator', |
| 71 | + ), |
| 72 | + const SizedBox(height: 10), |
| 73 | + Row( |
| 74 | + children: <Widget>[ |
| 75 | + Expanded( |
| 76 | + child: Text( |
| 77 | + 'determinate Mode', |
| 78 | + style: Theme.of(context).textTheme.titleSmall, |
| 79 | + ), |
| 80 | + ), |
| 81 | + Switch( |
| 82 | + value: determinate, |
| 83 | + onChanged: (bool value) { |
| 84 | + setState(() { |
| 85 | + determinate = value; |
| 86 | + if (determinate) { |
| 87 | + controller.stop(); |
| 88 | + } else { |
| 89 | + controller..forward(from: controller.value)..repeat(); |
| 90 | + } |
| 91 | + }); |
| 92 | + }, |
| 93 | + ), |
| 94 | + ], |
| 95 | + ), |
| 96 | + ], |
| 97 | + ), |
| 98 | + ), |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
0 commit comments