progress_indicator_demo.dart 3.14 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 8
class ProgressIndicatorDemo extends StatefulComponent {
  _ProgressIndicatorDemoState createState() => new _ProgressIndicatorDemoState();
9
}
10

11 12
class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> {

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

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

31
  Animation<double> animation;
32
  AnimationController controller;
33

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

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

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