0

Hello guys i was trying to add a BottomNavigatorBar in my code i, and i try to add a HomePage to it from another file, so that if i press "Home" it will show the HomePage, both executed perfectly when seperated, but when i try to combine them i got this "BOTTOM OVERFLOYD BY 4.7 PIXEL" error.

Here is my code

My Navigator Bar Code

import 'package:flutter/material.dart';
import 'package:medreminder/NewsArticle/newsmain.dart';
import 'package:medreminder/Reminder/ui/home_reminder.dart';
import 'package:medreminder/Reminder/ui/widgets/add_remindbar.dart';
import 'package:medreminder/home_page.dart';
import 'package:medreminder/profile_page.dart';
import 'package:medreminder/settings_page.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:medreminder/Reminder/ui/theme.dart';

void main() => runApp(MaterialApp(home: MainPage()));

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  List <Widget> pages = [
    ReminderHomePage(),
    SettingPage(),
    ProfilePage()
  ];
  int currentIndex = 0;
  void onTap(int index){
    setState(() {
      currentIndex = index;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height * 0.4,
        child: pages[currentIndex]
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.shifting,
        onTap: onTap,
        currentIndex: currentIndex,
        selectedItemColor: bluishClr,
        unselectedItemColor: Colors.black,
        showUnselectedLabels: false,
        showSelectedLabels: false,
        items: [
          BottomNavigationBarItem(label: "Home", icon: Icon(Icons.home)),
          BottomNavigationBarItem(label: "Settings", icon: Icon(Icons.settings)),
          BottomNavigationBarItem(label: "Profile", icon: Icon(Icons.account_circle)),
          
        ],
      ),
    );
  }
}

and here's the other file i want to put the same page with the Bottom Navigator

import 'package:date_picker_timeline/date_picker_timeline.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:medreminder/Reminder/services/notification_services.dart';
import 'package:medreminder/Reminder/services/theme_services.dart';
import 'package:intl/intl.dart';
import 'package:medreminder/Reminder/ui/theme.dart';
import 'package:medreminder/Reminder/ui/widgets/add_remindbar.dart';
import 'package:medreminder/Reminder/ui/widgets/button.dart';
import 'package:medreminder/Reminder/ui/widgets/add_remindbar.dart';

class ReminderHomePage extends StatefulWidget {
  const ReminderHomePage({super.key});

  @override
  State<ReminderHomePage> createState() => _ReminderHomePageState();
}

class _ReminderHomePageState extends State<ReminderHomePage> {
  DateTime _selectedDate = DateTime.now();
  var notifyHelper;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    notifyHelper=NotifyHelper();
    notifyHelper.initializeNotification();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: _appBar(),
      backgroundColor: context.theme.backgroundColor,
      body: Column(
        children: [
          _addTaskBar(),
          _addDateBar(),
        ],
        ),
    );
  }
  _addDateBar(){
    return Container(
            margin: const EdgeInsets.only(top: 20, left: 20),
            child: DatePicker(
              DateTime.now(),
              height: 100,
              width: 80,
              initialSelectedDate: DateTime.now(),
              selectionColor: Color(0xFFAAB6FB),
              selectedTextColor: Colors.white,
              dateTextStyle: GoogleFonts.lato(
                textStyle: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.w600,
                    color:Colors.grey
                ),
              ),
              dayTextStyle: GoogleFonts.lato(
                textStyle: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                    color:Colors.grey
                ),
              ),
              monthTextStyle: GoogleFonts.lato(
                textStyle: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w600,
                    color:Colors.grey
                ),
              ),
              onDateChange: (date){
                    _selectedDate=date;
              },
            ),
          );
  }
  _addTaskBar(){
    return Container(
            margin: const EdgeInsets.only(left: 20, right: 20, top: 5),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  margin: const EdgeInsets.symmetric(horizontal: 20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(DateFormat.yMMMMd().format(DateTime.now()),
                      style: subHeadingStyle,
                      ),
                      Text("Today", 
                      style: headingStyle,
                      )
                    ],
                  ),
                ),
                MyButton(label: "Add Reminder", onTap: ()=>Get.to(AddReminderPage()))
              ],
            ),
          );
  }

  _appBar(){
    return AppBar(
      elevation: 0,
      backgroundColor: context.theme.backgroundColor,
      leading: GestureDetector(
        onTap: (){
            ThemeService().switchTheme();
            notifyHelper.displayNotification(
              title:"Theme Changed!",
              body: Get.isDarkMode?"Activated Light Theme!":"Activated Dark Theme!"
            );

            notifyHelper.scheduledNotification();
        },
        child: Icon(Get.isDarkMode ?Icons.wb_sunny_outlined:Icons.nightlight_round,
        size: 20,
          color:Get.isDarkMode ? Colors.white:Colors.black
        ),
      ),
      actions: [
        CircleAvatar(
          backgroundImage: AssetImage(
            "images/profile.png"
          ),
        ),
        // Icon(Icons.person,
        // size: 20,),
        SizedBox(width: 20,),
      ],
    );
  }
}

and this is the full error i got on Terminal

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
The following assertion was thrown during layout:
A RenderFlex overflowed by 2.7 pixels on the bottom.

The relevant error-causing widget was:
  Column
  Column:file:///D:/Kuliah/Mata%20Kuliah/Pemrograman%20Mobile/medicine_reminder/code/lib/Reminder/ui/home_reminder.dart:38:13

The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and
black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the
RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be
seen. If the content is legitimately bigger than the available space, consider clipping it with a
ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex,
like a ListView.
The specific RenderFlex in question is: RenderFlex#7023a relayoutBoundary=up1 OVERFLOWING:
  needs compositing
  creator: Column ← KeyedSubtree-[GlobalKey#e59a5] ← _BodyBuilder ← MediaQuery ←
    LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← _ActionsMarker ← Actions ←
    AnimatedBuilder ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#01a94 ink
    renderer] ← ⋯
  parentData: offset=Offset(0.0, 56.0); id=_ScaffoldSlot.body (can use size)
  constraints: BoxConstraints(0.0<=w<=502.0, 0.0<=h<=173.3)
  size: Size(502.0, 173.3)
  direction: vertical
  mainAxisAlignment: start
  mainAxisSize: max
  crossAxisAlignment: center
  verticalDirection: down
◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
═══════════════════════════════════════════════════════════════════════════════════════════════════

Here is the error when i try

image

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
NyK
  • 43
  • 7
  • Can you comment `height: MediaQuery.of(context).size.height * 0.4,` – Md. Yeasin Sheikh Nov 18 '22 at 08:55
  • it workst thankyou. but when i try to press the "MyButton(label: "Add Reminder", onTap: ()=>Get.to(AddReminderPage()))" it shows another error "Another exception was thrown: You are trying to use contextless navigation without" – NyK Nov 18 '22 at 08:58
  • Need to check `AddReminderPage` – Md. Yeasin Sheikh Nov 18 '22 at 09:01
  • nevermind sir i fixed it by adding GetMaterialApp in my main.dart file. thankyou so much sir for helping, you have been a big help – NyK Nov 18 '22 at 09:03
  • I prefer MaterialApp :), but it need 1 per app. Also you may need scaffold there, no need to have multiple MaterialApp – Md. Yeasin Sheikh Nov 18 '22 at 09:04
  • ohh okay so i should change my GetMaterialApp to Material App? And where do i add the Scaffold to? Is it in my main.dart? – NyK Nov 18 '22 at 09:20
  • Current snippet is ok with MaterialApp, as you included, you may using getx I think, and I would suggest not to use it(personal opinion). I would suggest checking riverpod/bloc for state-management and go router if needed for navigation. – Md. Yeasin Sheikh Nov 18 '22 at 09:24
  • Thank you for the advice sir, but im so sorry for bothering you again because i got another question, you see in my app it has the moon icon on it, it supposed to change the theme to dark mode. When i run my "ReminderPage" code seperately, it works. But it wont work when u run ut with the Navigation bar. So are there any ways to fix it?. Thank you sir once again – NyK Nov 18 '22 at 09:42
  • You need to share the data on different routes, [state management](https://docs.flutter.dev/development/data-and-backend/state-mgmt/options) will help on this. – Md. Yeasin Sheikh Nov 18 '22 at 09:44

1 Answers1

0

You can remove fixed height from body Container, it is forcing to have fixed height and causing overflow then the child is bigger than MediaQuery.of(context).size.height * 0.4

 body: Container(
        width: MediaQuery.of(context).size.width,
         // height: MediaQuery.of(context).size.height * 0.4, //removed fixed height
        child: pages[currentIndex]
      ),
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56