栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Flutter-隐藏FloatingActionButton

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flutter-隐藏FloatingActionButton

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) {  print("**** ${_isVisible} up"); //Move IO away from setState setState((){   _isVisible = false; });        }      } else {        if(_hideButtonController.position.userScrollDirection == ScrollDirection.forward){          if(_isVisible == false) {       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),        ),),    );  }}

如果我不使用列表视图,我深表歉意,因为我不知道如何使用列表视图滚动。我将回答您问题的其他部分。

首先,你需要创建一个scrollcontroller将侦听scrollPostion事件

如果scrollcontroller设法找到向前或向后的滚动方向。您添加一个状态,将状态设置为可见。

绘制按钮时,将按钮包装在可见性类中。您设置了可见标志,并且小部件应忽略输入命令。

编辑:我似乎无法添加到ScrollController,ScrollerPosition,ScrollDirection和不透明度的链接。我想您可以自己搜索,也可以由其他人在链接中进行编辑

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

Edit3:鉴于新来者按原样使用代码,因此我将更新代码以鼓励更好的实践。使用可见性而不是不透明度。从setState中删除io。在Flutter
1.5.4-hotfix.2上测试



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号