anber
anber

Reputation: 3665

How to compose Flutter code not being stretched in height

I have some experience in Java code, and I am familiar with linear code style where command follows by command, but I can't understand how to write Flutter code with inherited widgets style - my code tends to have hundreds of lines for every widget. I faced with few constraints:

So my average code looks like this one:

                      child: Column(
                        children: <Widget>[
                          if (widget.headerWidget == null)
                            const SizedBox(
                              height: 14,
                            ),
                          AnimatedContainer(
                            foregroundDecoration: BoxDecoration(
                              color: getForegroundColor(),
                            ),
                            child: Row(
                              children: <Widget>[
                                (widget.isFeed)
                                    ? (widget.isNew
                                        ? Flexible(
                                            flex: 0,
                                            child: Padding(
                                              padding:
                                                  const EdgeInsets.only(
                                                left: middlePadding,
                                                top: middlePadding,
                                              ),
                                              child: Container(
                                                height: xSmallPadding,
                                                width: xSmallPadding,
                                                decoration: BoxDecoration(
                                                  color: AppColors.magenta,
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                    xxSmallPadding,
                                                  ),
                                                ),
                                              ),
                                            ),
                                          )
                                        : SizedBox(
                                            width:
                                                widget.isViewed ? 0 : 24.0,
                                          ))
                                    : const SizedBox(),

And simple widget become 500+ lines in height. How to avoid it?

Upvotes: 0

Views: 48

Answers (1)

Ahmad Ellamey
Ahmad Ellamey

Reputation: 329

you can split your code to small widgets (in flutter every thing is a widget) , there's an easy way for that in android studio you can right click on a widget name like a row and then choose refactor after this choose extract flutter widget , by this you will get all the work done and it will be converted to a flutter widget class , also you can extract flutter method but i preferer the flutter widget class so you move it to another file.

Upvotes: 2

Related Questions