gpt4 book ai didi

flutter - 如何在 flutter 中将自定义偏移量设置为 "FloatingActionButtonLocation"?

转载 作者:IT王子 更新时间:2023-10-29 07:05:36 25 4
gpt4 key购买 nike

最近引入了 FloatingActionButtonLocation,它有四个值,全部用于底部对齐。我希望它位于应用栏下方的顶部。但我不知道如何设置自定义偏移量。官方文档对此也很少。

最佳答案

这违反了 Material 设计准则。但是您可以通过将原始源代码中的 scaffoldGeometry.contentBottom 更改为 scaffoldGeometry.contentTop 来实现。下面的代码应该可以工作

import 'package:flutter/material.dart';
import 'dart:math' as math;


class HomeHeader extends StatefulWidget {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
HomeHeaderState createState() {
return new HomeHeaderState();
}
}

class HomeHeaderState extends State<HomeHeader> {

static const FloatingActionButtonLocation centerDocked = _CenterDockedFloatingActionButtonLocation();

@override
Widget build(BuildContext context) {
return new Scaffold(
key: widget._scaffoldKey,
appBar: AppBar(
title: Text('duh'),
),
floatingActionButtonLocation:centerDocked,
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add), onPressed: () {

},),
body: new Container()
);
}
}
class _CenterDockedFloatingActionButtonLocation extends _DockedFloatingActionButtonLocation {
const _CenterDockedFloatingActionButtonLocation();

@override
Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
final double fabX = (scaffoldGeometry.scaffoldSize.width - scaffoldGeometry.floatingActionButtonSize.width) / 2.0;
return Offset(fabX, getDockedY(scaffoldGeometry));
}
}

abstract class _DockedFloatingActionButtonLocation extends FloatingActionButtonLocation {
const _DockedFloatingActionButtonLocation();
@protected
double getDockedY(ScaffoldPrelayoutGeometry scaffoldGeometry) {
final double contentBottom = scaffoldGeometry.contentTop;
final double appBarHeight = scaffoldGeometry.bottomSheetSize.height;
final double fabHeight = scaffoldGeometry.floatingActionButtonSize.height;
final double snackBarHeight = scaffoldGeometry.snackBarSize.height;

double fabY = contentBottom - fabHeight / 2.0;
if (snackBarHeight > 0.0)
fabY = math.min(fabY, contentBottom - snackBarHeight - fabHeight - kFloatingActionButtonMargin);
if (appBarHeight > 0.0)
fabY = math.min(fabY, contentBottom - appBarHeight - fabHeight / 2.0);

final double maxFabY = scaffoldGeometry.scaffoldSize.height - fabHeight;
return math.min(maxFabY, fabY);
}
}

关于flutter - 如何在 flutter 中将自定义偏移量设置为 "FloatingActionButtonLocation"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53463461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com