代码之家  ›  专栏  ›  技术社区  ›  Marcin Szałek

颤振-隐藏浮动动作按钮

  •  61
  • Marcin Szałek  · 技术社区  · 8 年前

    有什么内置的方式在颤振隐藏一个 FloatingActionButton ListView

    9 回复  |  直到 4 年前
        1
  •  80
  •   user1462442    6 年前
    import 'package:flutter/material.dart';
    import 'package:flutter/rendering.dart';
    
    void main() {
      runApp(new MyApp());
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Flutter Demo',
          theme: new ThemeData(
    
            primarySwatch: Colors.blue,
          ),
          home: new MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
     }
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => new _MyHomePageState();
     }
    
     class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      ScrollController _hideButtonController;
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
      var _isVisible;
      @override
      initState(){
        super.initState();
        _isVisible = true;
        _hideButtonController = new ScrollController();
        _hideButtonController.addListener((){
          if(_hideButtonController.position.userScrollDirection == ScrollDirection.reverse){
            if(_isVisible == true) {
                /* only set when the previous state is false
                 * Less widget rebuilds 
                 */
                print("**** ${_isVisible} up"); //Move IO away from setState
                setState((){
                  _isVisible = false;
                });
            }
          } else {
            if(_hideButtonController.position.userScrollDirection == ScrollDirection.forward){
              if(_isVisible == false) {
                  /* only set when the previous state is false
                   * Less widget rebuilds 
                   */
                   print("**** ${_isVisible} down"); //Move IO away from setState
                   setState((){
                     _isVisible = true;
                   });
               }
            }
        }});
      }
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text(widget.title),
          ),
          body: new Center(
            child: new CustomScrollView(
              controller: _hideButtonController,
              shrinkWrap: true,
              slivers: <Widget>[
                new SliverPadding(
                  padding: const EdgeInsets.all(20.0),
                  sliver: new SliverList(
                    delegate: new SliverChildListDelegate(
                      <Widget>[
                        const Text('I\'m dedicating every day to you'),
                        const Text('Domestic life was never quite my style'),
                        const Text('When you smile, you knock me out, I fall apart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('And I thought I was so smart'),
                        const Text('I realize I am crazy'),   
                      ],
                    ),
                  ),
                ),
              ],
            )
          ),
          floatingActionButton: new Visibility( 
            visible: _isVisible,
            child: new FloatingActionButton(
              onPressed: _incrementCounter,
              tooltip: 'Increment',
              child: new Icon(Icons.add),
            ),     
          ),
        );
      }
    }
    

    如果我没有使用listview,我很抱歉,因为我不知道如何使用listview滚动。我将回答你问题的其他部分。

    首先,您需要创建一个 scrollcontroller 那就听我说 scrollPostion 事件

    如果scrollcontroller能够找到 scrolldirection 前进或后退。您添加了一个将状态设置为可见的状态。

    当你画按钮时,你把按钮包在一个 visibility 班您设置了可见标志,小部件应该忽略输入命令。

    编辑:我似乎无法添加指向ScrollController、ScrollerPosition、ScrollDirection和不透明性的链接。我想你可以自己搜索,或者其他人在链接中编辑

    Edit2:使用CopsonRoad或使用可见性小部件,除非您希望布局树中有未绘制的小部件

        2
  •  41
  •   CopsOnRoad    4 年前

    没有动画:

    • 使用 Visibility

      floatingActionButton: Visibility(
        visible: false, // Set it to false
        child: FloatingActionButton(...),
      )
      
    • 使用 Opacity

      floatingActionButton: Opacity(
        opacity: 0, // Set it to 0
        child: FloatingActionButton(...),
      )
      
    • 使用 三元 操作员:

      floatingActionButton: shouldShow ? FloatingActionButton() : null,
      
    • 使用 if

      floatingActionButton: Column(
        children: <Widget>[
          if (shouldShow) FloatingActionButton(...), // Visible if condition is true
        ],
      )
      

    带动画:

    enter image description here

    这只是使用动画的一个示例,您可以使用此方法创建不同类型的UI。

    bool _showFab = true;
      
    @override
    Widget build(BuildContext context) {
      const duration = Duration(milliseconds: 300);
      return Scaffold(
        floatingActionButton: AnimatedSlide(
          duration: duration,
          offset: _showFab ? Offset.zero : Offset(0, 2),
          child: AnimatedOpacity(
            duration: duration,
            opacity: _showFab ? 1 : 0,
            child: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () {},
            ),
          ),
        ),
        body: NotificationListener<UserScrollNotification>(
          onNotification: (notification) {
            final ScrollDirection direction = notification.direction;
            setState(() {
              if (direction == ScrollDirection.reverse) {
                _showFab = false;
              } else if (direction == ScrollDirection.forward) {
                _showFab = true;
              }
            });
            return true;
          },
          child: ListView.builder(
            itemCount: 100,
            itemBuilder: (_, i) => ListTile(title: Text('$i')),
          ),
        ),
      );
    }
    
        3
  •  31
  •   Ashwin S Ashok    6 年前

    这是一个很老的问题,但在我看来,有一个更好(更简短)的解决方案。

    每当用户滚动(上/下)时,NotificationListener会通知您。使用AnimationController,您可以控制晶圆厂的动画。

    class WidgetState extends State<Widget> with TickerProviderStateMixin<Widget> {
      AnimationController _hideFabAnimation;
    
      @override
      initState() {
        super.initState();
        _hideFabAnimation = AnimationController(vsync: this, duration: kThemeAnimationDuration);
      }
    
      @override
      void dispose() {
        _hideFabAnimation.dispose();
        super.dispose();
      }
    
      bool _handleScrollNotification(ScrollNotification notification) {
        if (notification.depth == 0) {
          if (notification is UserScrollNotification) {
            final UserScrollNotification userScroll = notification;
            switch (userScroll.direction) {
              case ScrollDirection.forward:
                if (userScroll.metrics.maxScrollExtent !=
                    userScroll.metrics.minScrollExtent) {
                  _hideFabAnimation.forward();
                }
                break;
              case ScrollDirection.reverse:
               if (userScroll.metrics.maxScrollExtent !=
                    userScroll.metrics.minScrollExtent) {
                  _hideFabAnimation.reverse();
                }
                break;
              case ScrollDirection.idle:
                break;
            }
          }
        }
        return false;
      }
    
      @override
      Widget build(BuildContext context) {
        return NotificationListener<ScrollNotification>(
          onNotification: _handleScrollNotification,
          child: Scaffold(
            appBar: AppBar(
              title: Text('Fabulous FAB Animation')
            ),
            body: Container(),
            floatingActionButton: ScaleTransition(
              scale: _hideFabAnimation,
              alignment: Alignment.bottomCenter,
              child: FloatingActionButton(
                elevation: 8,
                onPressed: () {},
                child: Icon(Icons.code),
              ),
            ),
          ),
        );
      }
    }
    
        4
  •  12
  •   Josteve Adekanbi    4 年前

    import 'package:flutter/material.dart';
    import 'package:flutter/rendering.dart';
    
    class Home extends StatefulWidget {
      @override
      _HomeState createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> {
      ScrollController controller;
      bool fabIsVisible = true;
    
      @override
      void initState() {
        super.initState();
        controller = ScrollController();
        controller.addListener(() {
          setState(() {
            fabIsVisible =
                controller.position.userScrollDirection == ScrollDirection.forward;
          });
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: ListView(
            controller: controller,
            children: List.generate(
                100,
                (index) => ListTile(
                      title: Text("Text $index"),
                    )),
          ),
          floatingActionButton: AnimatedOpacity(
            child: FloatingActionButton(
              child: Icon(Icons.add),
              tooltip: "Increment",
              onPressed: !fabIsVisible ? null: () {
                print("Pressed");
              },
            ),
            duration: Duration(milliseconds: 100),
            opacity: fabIsVisible ? 1 : 0,
          ),
        );
      }
    }
    
        5
  •  8
  •   vahid haj hosseini    6 年前

    您可以使用以下代码保留默认动画

    floatingActionButton: _isVisible
            ? FloatingActionButton(...)
            : null,
    
        6
  •  6
  •   Sibin    7 年前

    您可以使用 Visibility 用于处理子窗口小部件可见性的窗口小部件

      floatingActionButton:
                Visibility(visible: _visibilityFlag , child: _buildFAB(context)),
    
        7
  •  5
  •   Álvaro Agüero    6 年前

    另一个很好的方法是

    AnimatedOpacity(
              opacity: isEnabled ? 0.0 : 1.0,
              duration: Duration(milliseconds: 1000),
              child: FloatingActionButton(
                 onPressed: your_method,
                 tooltip: 'Increment',
                 child: new Icon(Icons.add),
              ),
            )
    
        8
  •  1
  •   khoaharp    4 年前

    首先,将滚动位置转换为流,以后也可以重用此方法。

    
    extension ScrollControllerX on ScrollController {
      Stream<double> positionAsStream() {
        late StreamController<double> controller;
    
        void addListener() => controller.add(position.pixels);
        void onListen() => this.addListener(addListener);
        void onCancel() {
          removeListener(addListener);
          controller.close();
        }
    
        controller = StreamController<double>(onListen: onListen, onCancel: onCancel);
    
        return controller.stream;
      }
    }
    
    

    像这样使用它。

    
          @override
          void initState() {
            super.initState();
            final subscription = scrollController
                .positionAsStream()
                .pairwise()
                .map((p) => p.last > p.first)
                .distinct() // If direction don't change, skip it 
                .listen((down) => down ? hideFabAnimationController.forward() : hideFabAnimationController.reverse());
        }
    
    
    
    
        FadeTransition(
                    opacity: hideFabAnimationController,
                    child: ScaleTransition(
                      scale: hideFabAnimationController,
                      child: FloatingActionButton(
                        onPressed: () => {},
                        child: const Icon(Icons.add),
                      ),
                    ),
                  )
    
    

    
          @override
          void dispose() {
            subscription.cancel();
          }
    
    

    当用户滚动过快时,你可以做其他事情,比如限制流。

        9
  •  0
  •   DarkMath    4 年前

    “Josteve”的答案是正确的,但打电话不是一个好主意 setState()

    import 'package:flutter/material.dart';
    
    class Home extends StatefulWidget {
      @override
      _HomeState createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> {
      ScrollController controller;
      bool _isFabVisible = true;
    
      @override
      void initState() {
        super.initState();
        controller = ScrollController();
        controller.addListener(() {
          // FAB should be visible if and only if user has not scrolled to bottom
          var userHasScrolledToBottom = controller.position.atEdge && controller.position.pixels > 0;
    
          if(_isFabVisible == userHasScrolledToBottom) {
            setState(() => _isFabVisible = !userHasScrolledToBottom);
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: ListView(
            controller: controller,
            children: List.generate(
                100,
                (index) => ListTile(
                      title: Text("Text $index"),
                    )),
          ),
          floatingActionButton: AnimatedOpacity(
            child: FloatingActionButton(
              child: Icon(Icons.add),
              tooltip: "Increment",
              onPressed: () {
                print("Pressed");
              },
            ),
            duration: Duration(milliseconds: 100),
            opacity: _isFabVisible? 1 : 0,
          ),
        );
      }
    }