progress_indicator_demo.dart 3.18 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/material.dart';
6

7
class ProgressIndicatorDemo extends StatefulWidget {
8
  @override
9
  _ProgressIndicatorDemoState createState() => new _ProgressIndicatorDemoState();
10
}
11

12 13
class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> {

14
  @override
15 16
  void initState() {
    super.initState();
17 18
    controller = new AnimationController(
      duration: const Duration(milliseconds: 1500)
Adam Barth's avatar
Adam Barth committed
19
    )..forward();
20

21
    animation = new CurvedAnimation(
22 23 24
      parent: controller,
      curve: new Interval(0.0, 0.9, curve: Curves.ease),
      reverseCurve: Curves.ease
25
    )..addStatusListener((AnimationStatus status) {
Adam Barth's avatar
Adam Barth committed
26 27 28 29
      if (status == AnimationStatus.dismissed)
        controller.forward();
      else if (status == AnimationStatus.completed)
        controller.reverse();
30
    });
31 32
  }

33
  Animation<double> animation;
34
  AnimationController controller;
35

Adam Barth's avatar
Adam Barth committed
36
  void _handleTap() {
37 38
    setState(() {
      // valueAnimation.isAnimating is part of our build state
Adam Barth's avatar
Adam Barth committed
39
      if (controller.isAnimating) {
40
        controller.stop();
Adam Barth's avatar
Adam Barth committed
41 42 43 44 45 46 47 48 49 50 51 52
      } else {
        switch (controller.status) {
          case AnimationStatus.dismissed:
          case AnimationStatus.forward:
            controller.forward();
            break;
          case AnimationStatus.reverse:
          case AnimationStatus.completed:
            controller.reverse();
            break;
        }
      }
53
    });
54 55
  }

Adam Barth's avatar
Adam Barth committed
56
  Widget _buildIndicators(BuildContext context, Widget child) {
57 58 59 60 61 62 63
    List<Widget> indicators = <Widget>[
        new SizedBox(
          width: 200.0,
          child: new LinearProgressIndicator()
        ),
        new LinearProgressIndicator(),
        new LinearProgressIndicator(),
64
        new LinearProgressIndicator(value: animation.value),
65 66 67 68
        new CircularProgressIndicator(),
        new SizedBox(
            width: 20.0,
            height: 20.0,
69
            child: new CircularProgressIndicator(value: animation.value)
70 71 72 73
        ),
        new SizedBox(
          width: 50.0,
          height: 30.0,
74
          child: new CircularProgressIndicator(value: animation.value)
75
        ),
76
        new Text("${(animation.value * 100.0).toStringAsFixed(1)}%" + (controller.isAnimating ? '' : ' (paused)'))
77
    ];
78
    return new Column(
79
      children: indicators
80
        .map((Widget c) => new Container(child: c, margin: const EdgeInsets.symmetric(vertical: 15.0, horizontal: 20.0)))
81
        .toList(),
82
      mainAxisAlignment: MainAxisAlignment.center
83 84 85
    );
  }

86
  @override
87
  Widget build(BuildContext context) {
88
    return new Scaffold(
89
      appBar: new AppBar(title: new Text('Progress Indicators')),
90
      body: new DefaultTextStyle(
91
        style: Theme.of(context).textTheme.title,
92
        child: new GestureDetector(
Adam Barth's avatar
Adam Barth committed
93
          onTap: _handleTap,
94 95
          behavior: HitTestBehavior.opaque,
          child: new Container(
96
            padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 8.0),
97 98
            child: new AnimatedBuilder(
              animation: animation,
Adam Barth's avatar
Adam Barth committed
99
              builder: _buildIndicators
100
            )
101 102 103 104 105 106
          )
        )
      )
    );
  }
}