简体   繁体   English

Flutter 中的圆形 AppBar,带有后退按钮

[英]Rounded AppBar in Flutter with Back button

I created a custom rounded AppBar using a code found here, but with just a title in the center.我使用此处找到的代码创建了一个自定义的圆形 AppBar,但中间只有一个标题。 I wanted to add a backbutton in the top left corner inside AppBar and I tried nesting a button and the text in a Row, but the result is that neither the button or the text are shown.我想在 AppBar 的左上角添加一个后退按钮,我尝试将一个按钮和文本嵌套在一个行中,但结果是按钮和文本都没有显示。 Any help?有什么帮助吗?

Here the code:这里的代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class RoundedAppBar extends StatelessWidget implements PreferredSizeWidget {
  String title;

  RoundedAppBar(this.title);
  @override
  Widget build(BuildContext context) {
    return PreferredSize(
        child: LayoutBuilder(builder: (context, constraints) {
          final width =
              constraints.maxWidth * 16; //per modificare "rotondità" app Bar
          return OverflowBox(
            maxHeight: double.infinity,
            maxWidth: double.infinity,
            child: SizedBox(
              height: width,
              width: width,
              child: Padding(
                padding: EdgeInsets.only(
                    bottom: width / 2 - preferredSize.height / 2),
                child: Container(
                    alignment: Alignment.bottomCenter,
                    padding: EdgeInsets.only(bottom: 10),
                    decoration: BoxDecoration(
                      color: const Color(0xff000350),
                      shape: BoxShape.circle,
                    ),
                    child: Row(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: IconButton(
                            color: Colors.black,
                            icon: Icon(Icons.chevron_left),
                            onPressed: () => Navigator.pop(context),
                          ),
                        ),
                        Text(
                          title,
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              fontFamily: 'Conformity',
                              color: Colors.white,
                              fontSize: 30,
                              fontWeight: FontWeight.normal),
                        ),
                      ],
                    )),
              ),
            ),
          );
        }),
        preferredSize: preferredSize);
  }

  @override
  Size get preferredSize => Size.fromHeight(80);

EDIT: Tried using ListTile as suggested, something happened but didn't work properly.编辑:按照建议尝试使用 ListTile,发生了一些事情但无法正常工作。 Here the result.结果在这里

child: ListTile(
                    title: Text(
                      title,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontFamily: 'Conformity',
                          color: Colors.white,
                          fontSize: 30,
                          fontWeight: FontWeight.normal),
                    ),
                    leading: IconButton(
                      color: Colors.white,
                      icon: Icon(Icons.chevron_left),
                      onPressed: () => Navigator.pop(context),
                    ),
                  ),

EDIT: I inserted your code as shown.编辑:我插入了你的代码,如图所示。 With trial and error, using 35 as height I was able to see the title, but still no button.经过反复试验,使用 35 作为高度,我能够看到标题,但仍然没有按钮。

child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      _buildBack(true, context),
                      Container(
                        height: 35,
                        child: Text(
                          title,
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              fontFamily: 'Conformity',
                              color: Colors.white,
                              fontSize: 30,
                              fontWeight: FontWeight.normal),
                        ),
                      ),
                      _buildBack(false, context),
                    ],

and

  Widget _buildBack(bool isPlaceHolder, BuildContext context) {
    return Visibility(
      child: InkWell(
        child: Icon(
          Icons.close,
          size: 35,
        ),
        onTap: () => Navigator.of(context, rootNavigator: true).pop('dialog'),
      ),
      maintainSize: true,
      maintainAnimation: true,
      maintainState: true,
      visible: !isPlaceHolder,
    );
  }

and here the result结果在这里

You can use a ListTile and use a IconButton as leading.您可以使用ListTile并使用IconButton作为前导。

ListTile(
  
  leading: IconButton(
    icon: Icon(Icons.back),
    title: '',
    onPressed => Navigator.pop(context),
   ),
),

Another possibility I see:我看到的另一种可能性:

As the child from the AppBar作为来自 AppBar 的孩子

Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              _buildBack(true, context),
              Container(
                height: height,
                child: Text(
                  '$_title',
                  style: Theme.of(context).textTheme.headline2,
                ),
              ),
              _buildBack(false, context),
            ],
          ),

In another place outside the builder .建造者之外的另一个地方。

  Widget _buildBack(bool isPlaceHolder, Buildcontext context) {
      return Visibility(
        child: InkWell(
          child: Icon(
            Icons.close,
            size: widget.height,
          ),
          onTap: () => Navigator.of(context, rootNavigator: true).pop('dialog'),
        ),
        maintainSize: true,
        maintainAnimation: true,
        maintainState: true,
        visible: !isPlaceHolder,
      );
  }}

Here there is again a row as you have tried it yourself, but this one is set up a little differently and an iconButton is built before and after the text, but so that the text remains in the center, the second one is made invisible,这里又有一行你自己试过了,但是这个设置有点不同,在文本之前和之后构建了一个 iconButton,但是为了让文本保持在中心,第二个是不可见的,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM