Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

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. 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. 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. 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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a ListTile and use a IconButton as leading.

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

Another possibility I see:

As the child from the 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,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...