2017-08-07 39 views
1

我試圖控制FAB的小項目的可見性的知名度,根據下面的GIF:撲 - 浮動的操作按鈕 - 隱藏物品

enter image description here

但是我不能在項目中插入opacity。任何地方我都會發生某種錯誤。我不知道opacity是否是最好的方法。

要隱藏項目,我相信動畫可以控制它們出現的時間。

下面是我迄今取得:

enter image description here

你能幫我解決這個問題?

下面是上述GIF代碼:

import 'package:flutter/animation.dart'; 
import 'package:flutter/material.dart'; 

void main() { 
    runApp(new MaterialApp(home: new HomePage())); 
} 

class HomePage extends StatefulWidget { 
    @override 
    HomePageState createState() => new HomePageState(); 
} 

class HomePageState extends State<HomePage> with TickerProviderStateMixin { 
    int _angle = 90; 
    bool _isRotated = true; 

    void _rotate(){ 
    setState((){ 
     if(_isRotated) { 
     _angle = 45; 
     _isRotated = false; 
     } else { 
     _angle = 90; 
     _isRotated = true; 
     } 
    }); 
    } 

    @override 
    Widget build(BuildContext context) { 
    return new Scaffold(
     body: new Stack(
     children: <Widget>[ 
      new Positioned(
       bottom: 200.0, 
       right: 24.0, 
       child: new Container(
       child: new Row(
        children: <Widget>[ 
        new Container(
         margin: new EdgeInsets.only(right: 16.0), 
         child: new Text(
         'foo1', 
         style: new TextStyle(
          fontSize: 13.0, 
          fontFamily: 'Roboto', 
          color: new Color(0xFF9E9E9E), 
          fontWeight: FontWeight.bold, 
         ), 
        ), 
        ), 
        new Material(
         color: new Color(0xFF9E9E9E), 
         type: MaterialType.circle, 
         elevation: 6.0, 
         child: new GestureDetector(
         child: new Container(
          width: 40.0, 
          height: 40.0, 
          child: new InkWell(
          onTap:(){}, 
          child: new Center(
           child: new Icon(
           Icons.add, 
           color: new Color(0xFFFFFFFF), 
          ),      
          ), 
         ) 
         ), 
        ) 
        ), 
        ], 
       ), 
      ) 
      ), 

      new Positioned(
      bottom: 144.0, 
      right: 24.0, 
      child: new Container(
       child: new Row(
       children: <Widget>[ 
        new Container(
        margin: new EdgeInsets.only(right: 16.0), 
        child: new Text(
         'foo2', 
         style: new TextStyle(
         fontSize: 13.0, 
         fontFamily: 'Roboto', 
         color: new Color(0xFF9E9E9E), 
         fontWeight: FontWeight.bold, 
        ), 
        ), 
       ), 
        new Material(
        color: new Color(0xFF00BFA5), 
        type: MaterialType.circle, 
        elevation: 6.0, 
        child: new GestureDetector(
         child: new Container(
         width: 40.0, 
         height: 40.0, 
         child: new InkWell(
          onTap:(){}, 
          child: new Center(
          child: new Icon(
           Icons.add, 
           color: new Color(0xFFFFFFFF), 
          ),      
         ), 
         ) 
        ), 
        ) 
       ), 
       ], 
      ), 
      ) 
     ), 
      new Positioned(
      bottom: 88.0, 
      right: 24.0, 
      child: new Container(
       child: new Row(
       children: <Widget>[ 
        new Container(
        margin: new EdgeInsets.only(right: 16.0), 
        child: new Text(
         'foo3', 
         style: new TextStyle(
         fontSize: 13.0, 
         fontFamily: 'Roboto', 
         color: new Color(0xFF9E9E9E), 
         fontWeight: FontWeight.bold, 
        ), 
        ), 
       ), 
        new Material(
        color: new Color(0xFFE57373), 
        type: MaterialType.circle, 
        elevation: 6.0, 
        child: new GestureDetector(
         child: new Container(
         width: 40.0, 
         height: 40.0, 
         child: new InkWell(
          onTap:(){}, 
          child: new Center(
          child: new Icon(
           Icons.add, 
           color: new Color(0xFFFFFFFF), 
          ),      
         ), 
         ) 
        ), 
        ) 
       ), 
       ], 
      ), 
      ) 
     ), 
      new Positioned(
      bottom: 16.0, 
      right: 16.0, 
      child: new Material(
       color: new Color(0xFFE57373), 
       type: MaterialType.circle, 
       elevation: 6.0, 
       child: new GestureDetector(
       child: new Container(
        width: 56.0, 
        height: 56.00, 
        child: new InkWell(
        onTap: _rotate, 
        child: new Center(
         child: new RotationTransition(
         turns: new AlwaysStoppedAnimation(_angle/360), 
         child: new Icon(
          Icons.add, 
          color: new Color(0xFFFFFFFF), 
         ), 
        ) 
        ), 
       ) 
       ), 
      ) 
      ), 
     ), 
     ] 
    ) 
    ); 
    } 
} 
+0

是GIF動畫,你粘貼的預期/期望行爲?你認爲你的代碼中的哪些內容應該從foo1/foo2/foo3的視圖引導到另一個視圖? (動畫Gif和你的代碼是不同的語言,所以很難跟蹤和猜測你想要做什麼)。請澄清;更準確的例子會有所幫助。 – mzimmermann

+0

最終目標是gif,但我首先需要的是將'foo1'隱藏起來,以便在出現輕擊之後。到目前爲止,我還沒有找到任何東西來幫助我用Flutter來控制widget的可視性 – rafaelcb21

+0

不確定這有助於您的最終目標,但是:您可以將tap添加到setState,然後再次調用build(),這次跳過(不添加)foo1(基本上foo1的存在將基於條件)。如果你需要foo1保留它的空間,把它換成條件相同大小的空容器(但獲得相同的大小通常會讓我覺得很痛苦)。 – mzimmermann

回答

1

我用Opacity但它使用ScaleTransition後變得多餘。使用ScaleTransition可以隱藏和顯示小部件。

我使用了3個動畫來生成級聯效果。

下面是代碼,並將其結果:

import 'package:flutter/animation.dart'; 
import 'package:flutter/material.dart'; 

void main() { 
    runApp(new MaterialApp(home: new HomePage())); 
} 

class HomePage extends StatefulWidget { 
    @override 
    HomePageState createState() => new HomePageState(); 
} 

class HomePageState extends State<HomePage> with TickerProviderStateMixin { 
    int _angle = 90; 
    bool _isRotated = true; 

    AnimationController _controller; 
    Animation<double> _animation; 
    Animation<double> _animation2; 
    Animation<double> _animation3; 

    @override 
    void initState() { 
    _controller = new AnimationController(
     vsync: this, 
     duration: const Duration(milliseconds: 180), 
    ); 

    _animation = new CurvedAnimation(
     parent: _controller, 
     curve: new Interval(0.0, 1.0, curve: Curves.linear), 
    ); 

    _animation2 = new CurvedAnimation(
     parent: _controller, 
     curve: new Interval(0.5, 1.0, curve: Curves.linear), 
    ); 

    _animation3 = new CurvedAnimation(
     parent: _controller, 
     curve: new Interval(0.8, 1.0, curve: Curves.linear), 
    ); 
    _controller.reverse(); 
    super.initState(); 
    } 

    void _rotate(){ 
    setState((){ 
     if(_isRotated) { 
     _angle = 45; 
     _isRotated = false; 
     _controller.forward(); 
     } else { 
     _angle = 90; 
     _isRotated = true; 
     _controller.reverse(); 
     } 
    }); 
    } 

    @override 
    Widget build(BuildContext context) { 
    return new Scaffold(
     body: new Stack(
     children: <Widget>[ 
      new Positioned(
       bottom: 200.0, 
       right: 24.0, 
       child: new Container(
       child: new Row(
        children: <Widget>[ 
        new ScaleTransition(
         scale: _animation3, 
         alignment: FractionalOffset.center, 
         child: new Container(
         margin: new EdgeInsets.only(right: 16.0), 
         child: new Text(
          'foo1', 
          style: new TextStyle(
          fontSize: 13.0, 
          fontFamily: 'Roboto', 
          color: new Color(0xFF9E9E9E), 
          fontWeight: FontWeight.bold, 
         ), 
         ), 
        ), 
        ), 

        new ScaleTransition(
         scale: _animation3, 
         alignment: FractionalOffset.center, 
         child: new Material(
         color: new Color(0xFF9E9E9E), 
         type: MaterialType.circle, 
         elevation: 6.0, 
         child: new GestureDetector(
          child: new Container(
          width: 40.0, 
          height: 40.0, 
          child: new InkWell(
           onTap:(){ 
           if(_angle == 45.0){ 
            print("foo1"); 
           } 
           }, 
           child: new Center(
           child: new Icon(
            Icons.add, 
            color: new Color(0xFFFFFFFF), 
           ),      
          ), 
          ) 
         ), 
         ) 
        ), 
        ),          
        ], 
       ), 
      ) 
      ), 

      new Positioned(
      bottom: 144.0, 
      right: 24.0, 
      child: new Container(
       child: new Row(
       children: <Widget>[ 
        new ScaleTransition(
        scale: _animation2, 
        alignment: FractionalOffset.center, 
        child: new Container(
         margin: new EdgeInsets.only(right: 16.0), 
         child: new Text(
         'foo2', 
         style: new TextStyle(
          fontSize: 13.0, 
          fontFamily: 'Roboto', 
          color: new Color(0xFF9E9E9E), 
          fontWeight: FontWeight.bold, 
         ), 
        ), 
        ), 
       ), 

        new ScaleTransition(
        scale: _animation2, 
        alignment: FractionalOffset.center, 
        child: new Material(
         color: new Color(0xFF00BFA5), 
         type: MaterialType.circle, 
         elevation: 6.0, 
         child: new GestureDetector(
         child: new Container(
          width: 40.0, 
          height: 40.0, 
          child: new InkWell(
          onTap:(){ 
           if(_angle == 45.0){ 
           print("foo2"); 
           } 
          }, 
          child: new Center(
           child: new Icon(
           Icons.add, 
           color: new Color(0xFFFFFFFF), 
          ),      
          ), 
         ) 
         ), 
        ) 
        ), 
       ), 
       ], 
      ), 
      ) 
     ), 
      new Positioned(
      bottom: 88.0, 
      right: 24.0, 
      child: new Container(
       child: new Row(
       children: <Widget>[ 
        new ScaleTransition(
        scale: _animation, 
        alignment: FractionalOffset.center, 
        child: new Container(
         margin: new EdgeInsets.only(right: 16.0), 
         child: new Text(
         'foo3', 
         style: new TextStyle(
          fontSize: 13.0, 
          fontFamily: 'Roboto', 
          color: new Color(0xFF9E9E9E), 
          fontWeight: FontWeight.bold, 
         ), 
        ), 
        ), 
       ), 

        new ScaleTransition(
        scale: _animation, 
        alignment: FractionalOffset.center, 
        child: new Material(
         color: new Color(0xFFE57373), 
         type: MaterialType.circle, 
         elevation: 6.0, 
         child: new GestureDetector(
         child: new Container(
          width: 40.0, 
          height: 40.0, 
          child: new InkWell(
          onTap:(){ 
           if(_angle == 45.0){ 
           print("foo3"); 
           } 
          }, 
          child: new Center(
           child: new Icon(
           Icons.add, 
           color: new Color(0xFFFFFFFF), 
          ),      
          ), 
         ) 
        ), 
        ) 
        ), 
       ), 
       ], 
      ), 
      ) 
     ), 
      new Positioned(
      bottom: 16.0, 
      right: 16.0, 
      child: new Material(
       color: new Color(0xFFE57373), 
       type: MaterialType.circle, 
       elevation: 6.0, 
       child: new GestureDetector(
       child: new Container(
        width: 56.0, 
        height: 56.00, 
        child: new InkWell(
        onTap: _rotate, 
        child: new Center(
         child: new RotationTransition(
         turns: new AlwaysStoppedAnimation(_angle/360), 
         child: new Icon(
          Icons.add, 
          color: new Color(0xFFFFFFFF), 
         ), 
        ) 
        ), 
       ) 
       ), 
      ) 
      ), 
     ), 
     ] 
    ) 
    ); 
    } 
} 

Float Action Button with cascade animation