Home > Blockchain >  How do I put context without Stateful widget
How do I put context without Stateful widget

Time:04-06

I want to navigate to next page which is FinalCart but there is no context in this page. When user click Book Now it will navigate to another page and will get the data from the details like price and calculate it.

Widget buildDetailsBottomBar(
     Map petspace,
      Color defaultColor,
      Color secondColor,
      Size size,
      //context 
    ) 
    {
            InkWell(
            onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context) => FinalCart),), //('book now'), //TODO: add book now action
              child: Container(
                width: size.width * 0.35,
                height: size.height * 0.07,
                decoration: BoxDecoration(
                  color: defaultColor,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Align(
                  child: Text(
                    'Book Now',
                    style: GoogleFonts.lato(
                      color: secondColor,
                      fontSize: size.height * 0.02,
                    ),
                  ),
                ),
              ),
            )
          ],
        ),
      );
}

CodePudding user response:

Just use

Widget buildDetailsBottomBar(
     Map petspace,
      Color defaultColor,
      Color secondColor,
      Size size,
      BuildContext context,
    )

as your widget parameters, then you can pass in the BuildContext of a widget that has one down to yours.

CodePudding user response:

I get the idea of defining a widget like that but inside another widget, but, if you need to pass the current context just create it into a new StatelessWidget. Consider this also as a good practice because it can be built as a constant widget, preventing that it rebuilds over an over again.

  • Related