首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自定义浮动动作按钮

自定义浮动动作按钮
EN

Stack Overflow用户
提问于 2019-08-08 06:12:11
回答 1查看 1.6K关注 0票数 0

我使用的是sliver,并且我希望使用浮动操作按钮和来自pub的sliding_up_panel,其行为如下:当我向下滚动列表时,浮动动作按钮消失;当我向上滚动时,会出现fab。而且,当我滑动(打开)菜单的时候,这个工厂应该会消失。

正如您在上面看到的,浮动操作按钮都在滑动元素上,但是我希望它在滑动元素和滚动项列表之间。

同样在上面的图片中,问题是浮动按钮实际上是可见的,但是当我在滑动菜单上滑动时,我想用一个很好的动画来隐藏它。

希望我的问题是清楚的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-08 07:47:12

编辑,请用滚动控制器编辑

全码

代码语言:javascript
运行
复制
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SliverExample(
        bodyWidgets: Text('Hello Body'),
        backgroundWidget: Text('Hello Background'),
      ),
    );
  }
}

Widget listItem(Color color, String title) => Container(
  height: 100.0,
  color: color,
  child: Center(
    child: Text(
      "$title",
      textAlign: TextAlign.center,
      style: TextStyle(
          color: Colors.white,
          fontSize: 14.0,
          fontWeight: FontWeight.bold),
    ),
  ),
);

class SliverExample extends StatefulWidget {
  final Widget backgroundWidget;
  final Widget bodyWidgets;

  SliverExample({
    this.backgroundWidget,
    this.bodyWidgets,
  });
  @override
  _SliverExampleState createState() => _SliverExampleState();
}

class _SliverExampleState extends State<SliverExample> {

  // I need something like this
  // To determine if SliverAppBar is expanded or not.
  ScrollController _scrollController;
  bool isAppBarExpanded = false;

  @override
  void initState() {
    super.initState();

    _scrollController = ScrollController()
      ..addListener(() => setState(() {
        print('Scroll view Listener is called offset ${_scrollController.offset}');
      }));
  }
  bool get _changecolor {
    return _scrollController.hasClients
        && _scrollController.offset > (200-kToolbarHeight);
  }

  bool get _hideFAB {
    return _scrollController.hasClients
        && _scrollController.offset > (200-kToolbarHeight);
  }

  @override
  Widget build(BuildContext context) {

    // To change the item's color accordingly
    // To be used in multiple places in code
    //Color itemColor = isAppBarExpanded ? Colors.white : Colors.black;

    // In my case PrimaryColor is white,
    // and the background widget is dark

    return Scaffold(
      body: CustomScrollView(
        controller: _scrollController,
        slivers: <Widget>[
          SliverAppBar(

            pinned: true,
            leading: BackButton(
              color: _changecolor? Colors.white: Colors.black, // Here
            ),
            actions: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.shopping_cart,
                  color: _changecolor? Colors.white: Colors.black, // Here
                ),
                onPressed: () {},
              ),
            ],
            expandedHeight: 200.0,
            flexibleSpace: FlexibleSpaceBar(
              centerTitle: true,
              title: Text(
                'title',
                style: TextStyle(
                  fontSize: 18.0,
                  color: _changecolor? Colors.white: Colors.black, // Here
                ),
              ),
              // Not affecting the question.
              background: widget.backgroundWidget,

            ),
          ),
        SliverList(
          ///Use SliverChildListDelegate and provide a list
          ///of widgets if the count is limited
          ///
          ///Lazy building of list
          delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
              /// To convert this infinite list to a list with "n" no of items,
              /// uncomment the following line:
              /// if (index > n) return null;
              return listItem(Colors.grey, "Sliver List item: $index");
            },
            /// Set childCount to limit no.of items
            /// childCount: 100,
          ),
        ),
          // Not affecting the question.
          SliverToBoxAdapter(child: widget.bodyWidgets),
        ],
      ),
      floatingActionButton: _hideFAB? Container() : FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.add,),
      ),
    );
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57406172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档