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

问题描述

问题


下图显示Flutter中每个小部件的高度对齐方式(如果可以使用高度):

elevation example

但是我想将高程移到此处"%URL%"小部件的直接中心(Offset(0.0,0.0))。

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

解决方法

在颤动中无法更改高度offset。 Flutter遵循Material Design原理,并且修改参数(例如offset)的功能将达到这个目的。

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

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,);
  }
}

或者如果您需要其他完全不同的东西,请按照自己的内心需求进行提升:)