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;
      }
    }
  • 相关阅读:
    JAVA005-基本数据类型变量的存储
    JAVA003-变量、数据类型
    Python_pandas数据处理_学习
    python_性能FPS
    DB_004_创建表
    DB_003_关系数据库标准语言(SQL)
    DB_002_数据库的创建和管理
    DB_001_概念模型设计
    虚幻蓝图学习笔记 简单VR功能实现
    虚幻蓝图学习笔记 制作第一人称(实现功能:捡枪,换枪,扔枪,仍炸弹等)(一)
  • 原文地址:https://www.cnblogs.com/liuzhi20101016/p/14751333.html
Copyright © 2011-2022 走看看