在 Flutter 中重新定位/移动高程位置

Relocate / Move elevation position in Flutter

问题


下图显示了 Flutter 中每个 Widget 的高度通常如何对齐(如果高度可用):

但我想将高度移动到此处 Card 小部件的直接中心 (Offset(0.0, 0.0))。

我从来没有找到一个干净的解决方案。有人在这里得到答案吗?

在 flutter 中无法更改 offset 高度。 Flutter 遵循 Material Design 原则,修改偏移等参数的能力将达到这一目的。

正如您在 source code 中看到的那样,对于 flutter 中的阴影,高程不过是阴影列表。如果您想修改海拔参数,我建议创建一个自定义包装器并使用具有可覆盖参数的实际值。

import 'package:flutter/material.dart';

class CustomElevation extends StatelessWidget {
  final Widget child;
  final Offset offset;
  final int elevation;
  final Map<int, List<BoxShadow>> _elevationToShadow;

  CustomElevation({@required this.child, @required this.elevation, this.offset = const Offset(0.0, 0.0)})
      : assert(child != null),
        // Here kElevationToShadow is a map exposed by flutter material design shadows
        _elevationToShadow = kElevationToShadow.map<int, List<BoxShadow>>(
          (int key, List<BoxShadow> value) => MapEntry<int, List<BoxShadow>>(
            key,
            value.map(
              (BoxShadow bs) => BoxShadow(
                offset: offset,
                blurRadius: bs.blurRadius,
                spreadRadius: bs.spreadRadius,
                color: bs.color,
              ),
            ),
          ),
        );

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        boxShadow: _elevationToShadow[elevation] ?? <BoxShadow>[],
      ),
      child: child,
    );
  }
}

或者,如果您需要完全不同的东西,请按照您的心意进行提升:)