Home > Software design >  pathing argument from widget to another screen
pathing argument from widget to another screen

Time:02-03

is there a way to path this "model" from commentItem Widget to a Navigator that transmit me to another screen ? like Navigator.push or Navigator.pushNamed ? it will help me to display posts or comments in different screens like home screen it will display posts as well as my profile screen it will display only my posts so i need to know how to path arguments to different screens by Navigator how to perform this with code ?

Widget commentItem(CommentModel model, context,) => Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CircleAvatar(
                radius: 22,
                backgroundImage: NetworkImage('${model.image}'),
              ),
              SizedBox(
                width: 15,
              ),
              Expanded(
                child: Column(
                  children: [
                    Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10),
                        color: SocialCubit.get(context).iconChangeTheme? Colors.white70 : Colors.grey[350],
                      ),
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Text(
                                  '${model.name}',
                                  style: TextStyle(
                                      fontSize: 15,
                                      fontWeight: FontWeight.bold,
                                    color: SocialCubit.get(context).iconChangeTheme? Colors.black : Colors.black
                                  ),
                                ),
                                SizedBox(
                                  width: 7,
                                ),
                                Text(
                                  '${model.dateTime}',
                                  style: TextStyle(
                                      fontSize: 11,
                                      color: SocialCubit.get(context).iconChangeTheme? Colors.black : Colors.black
                                  ),
                                ),
                              ],
                            ),
                            if(model.text != '')
                              SizedBox(
                                height: 5,
                              ),
                            Text('${model.text}'
                            ,style: TextStyle(
                                  color: SocialCubit.get(context).iconChangeTheme? Colors.black : Colors.black
                              ),),
                          ],
                        ),
                      ),
                    ),
                    if (model.commentImage != '')
                      Padding(
                        padding: const EdgeInsetsDirectional.only(top: 5),

                        child: Container(
                          height: 170,
                          //width: double.infinity,
                          alignment: Alignment.topLeft,
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(10),
                            child: Image(image: NetworkImage('${model.commentImage}'),
                                //fit: BoxFit.fill,
                                alignment: Alignment.topLeft),
                          ),
                        ),
                      ),
                  ],
                ),
              ),
            ],
          ),
        ],
      );
ListView.separated(
                                                  shrinkWrap: true,
                                                  physics: NeverScrollableScrollPhysics(),
                                                  itemBuilder: (context, index) => commentItem(SocialCubit.get(context).comments[index], context,),
                                                  separatorBuilder: (context, index) => SizedBox(height: 20),
                                                  itemCount: SocialCubit.get(context).comments.length),

CodePudding user response:

You can use the Navigator push as following:

Navigator.of(context).push(MaterialPageRoute(builder: (context) => NewScreenForPosts(
postCode: postCode,
week: week,
postName: postName
)));

Your class will look like following:

class NewScreenForPosts extends StatelessWidget {
  final postCode;
  final week;
  final postName;

  const NewScreenForPosts({Key? key, required this.postCode, required this.week, required this.postName}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}

if using Stateless widget, you can access the passed data through Navigator directly like postCode, week or postName etc. If using Stateful, you need to use widget.postCode, widget.week or widget.postName notation.

CodePudding user response:

using Getx Package Get.to(ScreenName()); https://pub.dev/packages/get

  • Related