zoukankan      html  css  js  c++  java
  • Flutter——自定义悬浮按钮的位置

    实例代码:https://github.com/hfhuaizhi/flutter_testview/blob/master/lib/pages/page_floating.dart

    import 'package:flutter/material.dart';
    import 'dart:math' as math;
    class PageFloating extends StatefulWidget {
      @override
      _PageFloatingState createState() {
        return _PageFloatingState();
      }
    }
    
    class _PageFloatingState extends State<PageFloating> {
      int progress = 10;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          floatingActionButtonLocation: GuideUserActionLocation.getInstance(),
          floatingActionButton: FloatingActionButton(onPressed: () {
            setState(() {
              progress += 10;
            });
          }),
        );
      }
    }
    class GuideUserActionLocation extends FloatingActionButtonLocation {
      double marginRight = 60;
      double marginBottom = 60;
      GuideUserActionLocation._();
    
      static GuideUserActionLocation _instance;
    
      static GuideUserActionLocation getInstance() {
        if (_instance == null) {
          _instance = GuideUserActionLocation._();
        }
        return _instance;
      }
    
      @override
      Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
        // Compute the x-axis offset.
        final double fabX = _endOffset(scaffoldGeometry);
    
        // Compute the y-axis offset.
        final double contentBottom = scaffoldGeometry.contentBottom;
        final double bottomSheetHeight = scaffoldGeometry.bottomSheetSize.height;
        final double fabHeight = scaffoldGeometry.floatingActionButtonSize.height;
        final double snackBarHeight = scaffoldGeometry.snackBarSize.height;
    
        double fabY = contentBottom - fabHeight - marginBottom;
        if (snackBarHeight > 0.0)
          fabY = math.min(
              fabY,
              contentBottom -
                  snackBarHeight -
                  fabHeight -
                  kFloatingActionButtonMargin);
        if (bottomSheetHeight > 0.0)
          fabY =
              math.min(fabY, contentBottom - bottomSheetHeight - fabHeight / 2.0);
        return Offset(fabX, fabY);
      }
    
      @override
      String toString() => 'TestActionLocation';
    
      double _endOffset(ScaffoldPrelayoutGeometry scaffoldGeometry,
          {double offset = 0.0}) {
        assert(scaffoldGeometry.textDirection != null);
        switch (scaffoldGeometry.textDirection) {
          case TextDirection.rtl:
            return _leftOffset(scaffoldGeometry, offset: offset);
          case TextDirection.ltr:
            return _rightOffset(scaffoldGeometry, offset: offset);
        }
        return null;
      }
    
      double _leftOffset(ScaffoldPrelayoutGeometry scaffoldGeometry,
          {double offset = 0.0}) {
        return kFloatingActionButtonMargin +
            scaffoldGeometry.minInsets.left -
            offset;
      }
    
      double _rightOffset(ScaffoldPrelayoutGeometry scaffoldGeometry,
          {double offset = 0.0}) {
        return scaffoldGeometry.scaffoldSize.width -
            scaffoldGeometry.floatingActionButtonSize.width -
            marginRight;
      }
    }
  • 相关阅读:
    BZOJ1146:[CTSC2008]网络管理
    AtCoder Grand Contest 004 C:AND Grid
    BZOJ3295:[CQOI2011]动态逆序对
    AtCoder Regular Contest 070F:Honest Or Unkind
    BZOJ3110:[ZJOI2013]K大数查询
    BZOJ3196:二逼平衡树
    浅谈splay
    BZOJ3938:Robot
    浅谈标记永久化
    AtCoder Regular Contest 068E:Snuke Line
  • 原文地址:https://www.cnblogs.com/liuzhi20101016/p/14751333.html
Copyright © 2011-2022 走看看