progress_indicator.dart 3.72 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 6
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
7

8
class ProgressIndicatorApp extends StatefulComponent {
9
  ProgressIndicatorAppState createState() => new ProgressIndicatorAppState();
10
}
11

12
class ProgressIndicatorAppState extends State<ProgressIndicatorApp> {
13 14
  void initState() {
    super.initState();
15
    valueAnimation = new ValuePerformance<double>()
16 17 18 19
      ..duration = const Duration(milliseconds: 1500)
      ..variable = new AnimatedValue<double>(
        0.0,
        end: 1.0,
20 21
        curve: new Interval(0.0, 0.9, curve: Curves.ease),
        reverseCurve: Curves.ease
22
      );
23 24
    valueAnimation.addStatusListener((PerformanceStatus status) {
      if (status == PerformanceStatus.dismissed || status == PerformanceStatus.completed)
25 26 27
        reverseValueAnimationDirection();
    });
    valueAnimation.play(valueAnimationDirection);
28 29
  }

30 31
  ValuePerformance<double> valueAnimation;
  AnimationDirection valueAnimationDirection = AnimationDirection.forward;
32

33
  void handleTap() {
34 35 36 37 38 39 40
    setState(() {
      // valueAnimation.isAnimating is part of our build state
      if (valueAnimation.isAnimating)
        valueAnimation.stop();
      else
        valueAnimation.resume();
    });
41 42 43
  }

  void reverseValueAnimationDirection() {
44 45 46
    valueAnimationDirection = (valueAnimationDirection == AnimationDirection.forward)
      ? AnimationDirection.reverse
      : AnimationDirection.forward;
47
    valueAnimation.play(valueAnimationDirection);
48 49
  }

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

80
  Widget build(BuildContext context) {
81 82
    Widget body = new GestureDetector(
      onTap: handleTap,
83 84 85
      child: new Container(
        padding: const EdgeDims.symmetric(vertical: 12.0, horizontal: 8.0),
        child: new BuilderTransition(
Hixie's avatar
Hixie committed
86
          variables: <AnimatedValue<double>>[valueAnimation.variable],
87
          performance: valueAnimation.view,
88 89 90 91 92 93 94 95 96 97
          builder: buildIndicators
        )
      )
    );

    return new IconTheme(
      data: const IconThemeData(color: IconThemeColor.white),
      child: new Theme(
        data: new ThemeData(
          brightness: ThemeBrightness.light,
98 99
          primarySwatch: Colors.blue,
          accentColor: Colors.redAccent[200]
100
        ),
101
        child: new Title(
102
          title: 'Progress Indicators',
103
          child: new Scaffold(
Adam Barth's avatar
Adam Barth committed
104
            toolBar: new ToolBar(center: new Text('Progress Indicators')),
105
            body: new DefaultTextStyle(
106
              style: Theme.of(context).text.title,
107 108
              child: body
            )
109 110 111 112 113 114 115 116 117 118
          )
        )
      )
    );
  }
}

void main() {
  runApp(new ProgressIndicatorApp());
}