Custom AppBar button not working

99 views
Skip to first unread message

Rick P.

unread,
Apr 20, 2020, 3:49:49 PM4/20/20
to Flutter Development (flutter-dev)
I am trying to implement a custom app bar for child pages of my main page. However, whenever I go to the child page the button on the custom app bar does not work. The app bar is called within a Scaffold on each child page page.

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:../resources/constants.dart';
import 'package:../resources/text_styles.dart';

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
  CustomAppBar(this.title);
  final String title;
 

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

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: getAppBarWidgets(context),
      ),
    );
  }

  List<Widget> getAppBarWidgets(BuildContext context) {
    List<Widget> _appBarWidgets = [];

    _appBarWidgets.add(
      IconButton(
        icon: Icon(
          Platform.isIOS ? Icons.arrow_back_ios : Icons.arrow_back,
          size: 20.0,
        ),
        onPressed: () {
          Navigator.pop(context);
        },
      ),
    );

    _appBarWidgets.add(
      Text(
        title,
        textAlign: TextAlign.center,
        style:
            kHeadlineBase.copyWith(fontWeight: FontWeight.bold, fontSize: 12.0),
      ),
    );
    _appBarWidgets.add(SizedBox(
      width: kAppBarMargin,
    ));
    return _appBarWidgets;
  }
}


Stefan Matthias Aust

unread,
Apr 20, 2020, 5:18:45 PM4/20/20
to Flutter Development (flutter-dev)
To show test code, it can be useful to use dartpad.  I copied (and slightly changed) your code and it seems to work.


Stefan


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/9c2476b0-7ff4-4eb3-808a-c75e644ab5e4%40googlegroups.com.

Rick P.

unread,
Apr 21, 2020, 3:35:30 PM4/21/20
to Flutter Development (flutter-dev)
This gives me the same results. I think the issue is using the CustomAppBar on the second child page.  The button works on the FirstChildPage but not on the SecondChildPage. I tested it as a bottomBar in the Scaffold widget and the button worked.

Would it work better if I created a Stateful widget to hold the Scaffold, assign the appropriate page and  setState based on the user interactions? Right now I have a Scaffold on each page.

HomePage => FirstChildPage => SecondChildPage
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

Stefan Matthias Aust

unread,
Apr 21, 2020, 4:59:09 PM4/21/20
to Rick P., Flutter Development (flutter-dev)
Did you run the Dartpad example? I can press the button four times and go back four times without problems. Therefore, I don't think the CustomAppBar is an issue.

I recommend you try to provide a minimal running example using Dartpad to demonstrate your problem.

Stefan

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/905f82c7-0e33-4b4d-acf7-bff7e77bf462%40googlegroups.com.

Rick P.

unread,
Apr 22, 2020, 2:16:46 PM4/22/20
to Flutter Development (flutter-dev)
I tried to replicate the issue in Dartpad, but I couldn't. I then rewrote the Pages and CustomAppBar widgets and that fixed the issue. I still don't know what I did wrong, but thank you for your help.
Reply all
Reply to author
Forward
0 new messages