Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 将小部件中心与AppBar底部边缘对齐_Flutter_Flutter Layout - Fatal编程技术网

Flutter 将小部件中心与AppBar底部边缘对齐

Flutter 将小部件中心与AppBar底部边缘对齐,flutter,flutter-layout,Flutter,Flutter Layout,我需要将小部件的中心与appbar底部边缘对齐。 因此,它将垂直放置,一半在appbar上,另一半在页面主体上 现在我已经将小部件添加到AppBar底部:但它不会与它的水平中心线对齐 现在看起来是这样的: class CustomAppBar extends AppBar { final Widget appBarActionButton; CustomAppBar({Widget title = AppUtils.EMPTY_TEXT_VIEW, this.appB

我需要将小部件的中心与appbar底部边缘对齐。 因此,它将垂直放置,一半在appbar上,另一半在页面主体上

现在我已经将小部件添加到AppBar底部:但它不会与它的水平中心线对齐

现在看起来是这样的:

  class CustomAppBar extends AppBar {

     final Widget appBarActionButton;

     CustomAppBar({Widget title = AppUtils.EMPTY_TEXT_VIEW, this.appBarActionButton}): super(
        title: title,
        backgroundColor: Colors.blueGrey,
        elevation: 0,
        bottom: PreferredSize(
            child: Stack( //The stack holds the horizontal line and the button aligned cente
              alignment: Alignment.center,
              children: <Widget>[
                Container( //This is the horizontal line
                  color: Colors.GeneralDividerGray,
                  height: 1.0,
                ),
                Align(
                  child: Container(
                      child: appBarActionButton, //This is the button widget
                      ),
               )
          ],
        ),
        preferredSize: Size.fromHeight(4.0)),
      );
 }

虽然我希望SelectEnvironment按钮的中间和水平白线将“坐”在appBar的底部边缘

appBar的代码如下所示:

  class CustomAppBar extends AppBar {

     final Widget appBarActionButton;

     CustomAppBar({Widget title = AppUtils.EMPTY_TEXT_VIEW, this.appBarActionButton}): super(
        title: title,
        backgroundColor: Colors.blueGrey,
        elevation: 0,
        bottom: PreferredSize(
            child: Stack( //The stack holds the horizontal line and the button aligned cente
              alignment: Alignment.center,
              children: <Widget>[
                Container( //This is the horizontal line
                  color: Colors.GeneralDividerGray,
                  height: 1.0,
                ),
                Align(
                  child: Container(
                      child: appBarActionButton, //This is the button widget
                      ),
               )
          ],
        ),
        preferredSize: Size.fromHeight(4.0)),
      );
 }
类CustomAppBar扩展了AppBar{
最终小部件appBarActionButton;
CustomAppBar({Widget title=AppUtils.EMPTY_TEXT_VIEW,this.appBarActionButton}):超级(
标题:标题,,
背景颜色:颜色。蓝灰色,
海拔:0,
底部:首选尺寸(
child:Stack(//堆栈保存水平线和对齐的按钮
对齐:对齐.center,
儿童:[
容器(//这是水平线
颜色:Colors.GeneralDividerGray,
高度:1.0,
),
对齐(
子:容器(
child:appBarActionButton,//这是按钮小部件
),
)
],
),
preferredSize:Size.fromHeight(4.0)),
);
}

如果有一个更好的方法来实现这一点,把它以外的appbar它的行,只要它会给同样的效果

最好的方法是通过如下小部件使用切片:

  ScrollController scrollController = new ScrollController();
  return Stack(
    children: [
      NestedScrollView(
        controller: scrollController,
        headerSliverBuilder: (context, value){
          return [
//            list of widgets in here
          ];
        },
        body: Container(
          // here, your normal body goes
        ),
      ),
      Positioned(
        top: 50.0,
        left: 100.0,
        child: Container(
          // your centered widget here
        ),
      )
    ]
  );
}

您必须使用SliverAppBar,而不是使用普通的appBar。最好的方法是通过如下小部件使用Sliver:

  ScrollController scrollController = new ScrollController();
  return Stack(
    children: [
      NestedScrollView(
        controller: scrollController,
        headerSliverBuilder: (context, value){
          return [
//            list of widgets in here
          ];
        },
        body: Container(
          // here, your normal body goes
        ),
      ),
      Positioned(
        top: 50.0,
        left: 100.0,
        child: Container(
          // your centered widget here
        ),
      )
    ]
  );
}

不要使用普通的appBar,你必须使用SliveAppBar,我认为你应该像这样使用
Stack
Column

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

typedef void OnWidgetSizeChange(Size size);

class MeasureSize extends StatefulWidget {
  final Widget child;
  final OnWidgetSizeChange onChange;

  const MeasureSize({
    Key key,
    @required this.onChange,
    @required this.child,
  }) : super(key: key);

  @override
  _MeasureSizeState createState() => _MeasureSizeState();
}

class _MeasureSizeState extends State<MeasureSize> {
  @override
  Widget build(BuildContext context) {
    SchedulerBinding.instance.addPostFrameCallback(postFrameCallback);
    return Container(
      key: widgetKey,
      child: widget.child,
    );
  }

  var widgetKey = GlobalKey();
  var oldSize;

  void postFrameCallback(_) {
    var context = widgetKey.currentContext;
    if (context == null) return;

    var newSize = context.size;
    if (oldSize == newSize) return;

    oldSize = newSize;
    widget.onChange(newSize);
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Size s;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: [
        Column(
          children: [
            MeasureSize(
              onChange: (size) {
                setState(() {
                  s = size;
                });
              },
              child: AppBar(
                title: Text('title'),
              ),
            ),
            SizedBox(
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height - (s?.height ?? 0.0),
                child: Center(child: Text('body')))
          ],
        ),
        Positioned(
          top: (s?.height ?? 0.0) - 16.0,
          child: Container(
            width: MediaQuery.of(context).size.width,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                    height: 32,
                    color: Colors.red[400],
                    padding: EdgeInsets.all(6),
                    child: Center(child: Text('Select Environment'))),
              ],
            ),
          ),
        )
      ],
    ));
  }
}
导入“包装:颤振/材料.省道”;
导入“package:flatter/scheduler.dart”;
typedef void OnWidgetSizeChange(大小);
类MeasureSize扩展StatefulWidget{
最后一个孩子;
最终OnWidgetSizeChange onChange;
常数测量({
关键点,
@需要此.onChange,
@需要这个孩子,
}):super(key:key);
@凌驾
_MeasureSizeState createState()=>_MeasureSizeState();
}
类_MeasureSizeState扩展状态{
@凌驾
小部件构建(构建上下文){
SchedulerBinding.instance.addPostFrameCallback(postFrameCallback);
返回容器(
key:widgetKey,
child:widget.child,
);
}
var widgetKey=GlobalKey();
大小;
无效postFrameCallback(\ux){
var context=widgetKey.currentContext;
if(context==null)返回;
var newSize=context.size;
if(oldSize==newSize)返回;
oldSize=新闻大小;
widget.onChange(newSize);
}
}
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
大小s;
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:堆栈(
儿童:[
纵队(
儿童:[
测量(
onChange:(大小){
设置状态(){
s=尺寸;
});
},
孩子:AppBar(
标题:文本(“标题”),
),
),
大小盒子(
宽度:MediaQuery.of(context).size.width,
高度:MediaQuery.of(上下文).size.height-(s?.height±0.0),
子对象:中心(子对象:文本('正文'))
],
),
定位(
顶部:(s?高度±0.0)-16.0,
子:容器(
宽度:MediaQuery.of(context).size.width,
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
容器(
身高:32,
颜色:颜色。红色[400],
填充:边缘设置。全部(6),
子对象:中心(子对象:文本(“选择环境”)),
],
),
),
)
],
));
}
}

我认为你应该像这样使用
堆栈

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

typedef void OnWidgetSizeChange(Size size);

class MeasureSize extends StatefulWidget {
  final Widget child;
  final OnWidgetSizeChange onChange;

  const MeasureSize({
    Key key,
    @required this.onChange,
    @required this.child,
  }) : super(key: key);

  @override
  _MeasureSizeState createState() => _MeasureSizeState();
}

class _MeasureSizeState extends State<MeasureSize> {
  @override
  Widget build(BuildContext context) {
    SchedulerBinding.instance.addPostFrameCallback(postFrameCallback);
    return Container(
      key: widgetKey,
      child: widget.child,
    );
  }

  var widgetKey = GlobalKey();
  var oldSize;

  void postFrameCallback(_) {
    var context = widgetKey.currentContext;
    if (context == null) return;

    var newSize = context.size;
    if (oldSize == newSize) return;

    oldSize = newSize;
    widget.onChange(newSize);
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Size s;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: [
        Column(
          children: [
            MeasureSize(
              onChange: (size) {
                setState(() {
                  s = size;
                });
              },
              child: AppBar(
                title: Text('title'),
              ),
            ),
            SizedBox(
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height - (s?.height ?? 0.0),
                child: Center(child: Text('body')))
          ],
        ),
        Positioned(
          top: (s?.height ?? 0.0) - 16.0,
          child: Container(
            width: MediaQuery.of(context).size.width,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                    height: 32,
                    color: Colors.red[400],
                    padding: EdgeInsets.all(6),
                    child: Center(child: Text('Select Environment'))),
              ],
            ),
          ),
        )
      ],
    ));
  }
}
导入“包装:颤振/材料.省道”;
导入“package:flatter/scheduler.dart”;
typedef void OnWidgetSizeChange(大小);
类MeasureSize扩展StatefulWidget{
最后一个孩子;
最终OnWidgetSizeChange onChange;
常数测量({
关键点,
@需要此.onChange,
@需要这个孩子,
}):super(key:key);
@凌驾
_MeasureSizeState createState()=>_MeasureSizeState();
}
类_MeasureSizeState扩展状态{
@凌驾
小部件构建(构建上下文){
SchedulerBinding.instance.addPostFrameCallback(postFrameCallback);
返回容器(
key:widgetKey,
child:widget.child,
);
}
var widgetKey=GlobalKey();
大小;
无效postFrameCallback(\ux){
var context=widgetKey.currentContext;
if(context==null)返回;
var newSize=context.size;
if(oldSize==newSize)返回;
oldSize=新闻大小;
widget.onChange(newSize);
}
}
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视密度:视密度