mimic_demo.dart 2.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
// 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.

import 'package:flutter/material.dart';

const double _kHeight = 150.0;
const Duration _kEffectDuration = const Duration(seconds: 1);

class MimicDemo extends StatefulComponent {
  _MimicDemoState createState() => new _MimicDemoState();
}

class _MimicDemoState extends State<MimicDemo> {
  GlobalKey<MimicableState> _orange = new GlobalKey<MimicableState>();
  GlobalKey _targetContainer = new GlobalKey();

  bool _slotForOrangeOnTop = false;
  bool _orangeOnTop = false;

  void _handleTap() {
    if (_slotForOrangeOnTop)
      return;
    setState(() {
      _slotForOrangeOnTop = true;
    });
    MimicOverlayEntry entry = _orange.currentState.liftToOverlay();
    entry.animateTo(targetKey: _targetContainer, duration: _kEffectDuration, curve: Curves.ease).then((_) {
      setState(() {
        _orangeOnTop = true;
      });
      entry.dispose();
    });
  }

  void _reset() {
    setState(() {
      _slotForOrangeOnTop = false;
      _orangeOnTop = false;
    });
  }

  Widget _buildOrange() {
    return new Mimicable(
      key: _orange,
      child: new Container(
        height: _kHeight,
        decoration: new BoxDecoration(
          backgroundColor: Colors.deepOrange[500]
        )
      )
    );
  }

  Widget build(BuildContext context) {
    List<Widget> children = <Widget>[
      new Container(
        height: _kHeight,
        decoration: new BoxDecoration(
          backgroundColor: Colors.amber[500]
        )
      ),
      new AnimatedContainer(
        key: _targetContainer,
        height: _slotForOrangeOnTop ? _kHeight : 0.0,
        duration: _kEffectDuration,
        curve: Curves.ease,
        child: _orangeOnTop ? _buildOrange() : null
      ),
      new Container(
        height: _kHeight,
        decoration: new BoxDecoration(
          backgroundColor: Colors.green[500]
        )
      ),
      new Container(
        height: _kHeight,
        decoration: new BoxDecoration(
          backgroundColor: Colors.blue[500]
        )
      ),
    ];

    if (!_orangeOnTop)
      children.add(_buildOrange());

    return new GestureDetector(
      onTap: _handleTap,
      onLongPress: _reset,
90
      child: new Block(children: children)
91 92 93 94 95 96 97 98 99 100 101 102
    );
  }
}

void main() {
  runApp(new MaterialApp(
    title: 'Mimic Demo',
    routes: {
      '/': (_) => new MimicDemo()
    }
  ));
}