How Can I use ObjectKey in stateful widget?

Viewed 20

I am developing an app which people can add menus to home screen. From this menus people can reach new screen for example,when user created "Cakes" menu, the new page is occuring which called "Cakes",also, user can add cakes recipes to new screen, But people will add more menus and I dont know how will I put statefull new pages, and the recipes list of this new page, Only thing I know, I have to use key, but how?, where?

List<Widget> menuExtensionScreens=[];
List<Widget> menuExtensionCards=[EmptyMenu(fromWhere: "menuExtension")];


class MenuCard extends StatelessWidget {
  MenuCard({this.newMenuName, this.imagePath, });
  final newMenuName;
  final imagePath;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top:15.0),
      child: FlatButton(
        onPressed: (){
          MenuExtensionScreen menuExtensionScreen=MenuExtensionScreen(menuExtensionName: newMenuName);
          menuExtensionScreens.add(menuExtensionScreen);
          Navigator.push(context, MaterialPageRoute(builder: (context)=>MenuExtensionScreen(menuExtensionName: newMenuName,)));
        },
        child: Container(
          height: 180,
          width: 180,
          decoration: BoxDecoration(
            border: Border.all(style: BorderStyle.solid, width: 1),
            borderRadius: BorderRadius.circular(30),
            color: Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(0.5),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(height: 10,),
              Container(
                decoration: BoxDecoration(
                  color: Colors.white.withOpacity(0.5),
                  borderRadius: BorderRadius.circular(90),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    newMenuName,
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 20,
                        fontFamily: 'Graduate',
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ),
              Expanded(
                child: Padding(
                  padding:EdgeInsets.all(5),
                  child: Image(
                    image: AssetImage(
                        imagePath
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
class EmptyMenu extends StatelessWidget {
  EmptyMenu({this.fromWhere});
  final String fromWhere;
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top:15.0),
      child: FlatButton(
        onPressed: (){
          if(fromWhere=="homeScreen"){
            showModalBottomSheet(
              context: context,
              builder: (BuildContext context)=> AddMenuScreen(buttonText: "Menü Ekle",route:"homeScreen"),
            );
          }
          else if(fromWhere=="menuExtension"){
            showModalBottomSheet(
              context: context,
              builder: (BuildContext context)=> AddMenuScreen(buttonText: "Menü Ekle",route:"menuExtensionScreen"),
            );
          }
        },
        child: Container(
          height: 180,
          width: 180,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(30),
            color: Colors.black12.withOpacity(0.1),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Icon(Icons.add_circle_outline_outlined,size: 100,color: Colors.grey.shade400,),
            ],
          ),
        ),
      ),
    );
  }
}

This stateful widget that will created when the user created New menu and pressed. How can I use MenuCard widget in MenuExtensionScreen when New menü created Please help me.

class MenuExtensionScreen extends StatefulWidget {
  MenuExtensionScreen({this.menuExtensionName, this.imagePath});
  final String imagePath;
  final String menuExtensionName;
  @override
  _MenuExtensionScreenState createState() => _MenuExtensionScreenState();
}

class _MenuExtensionScreenState extends State<MenuExtensionScreen> {

  void initState(){
    super.initState();
    if (widget.menuExtensionName!=null && widget.imagePath!=null){
      Widget newMenu=MenuCard(newMenuName: widget.menuExtensionName,imagePath: widget.imagePath);
      menuExtensionCards.insert(0, newMenu);
    }
  }
  Widget buildBottomSheet(BuildContext context)=>AddMenuScreen(buttonText: "Tarif Ekle",route: "menuExtensionScreen",);
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          automaticallyImplyLeading: false,
          centerTitle: true,
          title: BorderedText(
            child:Text(
              widget.menuExtensionName,
              style: TextStyle(
                  color: Color(0XFFFFFB00),
                  fontSize: 30,
                  fontFamily: "Graduate"
              ),
            ),
            strokeWidth: 5,
            strokeColor: Colors.black,
          ),
          elevation: 5,
          backgroundColor: Color(0xFFF2C3D4).withOpacity(1),
          leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: (){
              Navigator.pop(context);
            },
            iconSize: 40,
            color: Color(0xFFA2000B),
          ),
          actions: [
            CircleAvatar(
              radius: 27,
              backgroundColor: Colors.transparent,
              backgroundImage: AssetImage("images/cuttedlogo.PNG"),
            )
          ],
        ),
        body: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("images/logoBGopacity.png"),
              fit: BoxFit.cover,
            ),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Expanded(
                child: GridView.count(
                  crossAxisCount: 2,
                  children:menuExtensionCards,
                ),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.end,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Padding(
                    padding: EdgeInsets.all(10),
                    child: Container(
                      decoration: BoxDecoration(
                        border: Border.all(style: BorderStyle.solid),
                        color: kColorTheme7,
                        borderRadius: BorderRadius.circular(40),
                      ),
                      child: FlatButton(
                        onPressed: (){
                          showModalBottomSheet(
                            context: context,
                            builder: (BuildContext context)=> AddMenuScreen(buttonText: "Tarif Ekle", route:"menuExtension"),
                          );
                        },
                        child: BorderedText(
                          strokeWidth: 5,
                          strokeColor: Colors.black,
                          child:Text("Tarif Ekle",style: TextStyle(
                            color: Colors.white,
                            fontFamily:'Graduate',
                            fontSize:30,
                          ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

0 Answers
Related