skip to Main Content

i was working on onboarding screen of my flutter project ( i am still beginner and still learning ) , there is a scroll indicator on onboarding screens which changes according to page change so i checked internet and found that i can use some packages butthey were confusing so i tried implemeneting it by myself, not sure that its right implementation

 PageView.builder(
              itemCount: OnboardingItmes().items.length,
              controller: pageController,
              onPageChanged: (value) {
                setState(() {
                  scrollIndex = value;
                });
              },
              itemBuilder: (context, index) => ...

and using a row to display that indiactor,

Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Padding(
                        padding: const EdgeInsets.only(right: 5.0),
                        child: Container(
                          height: 15,
                          width: scrollIndex == 0 ? 40 : 20,
                          decoration: BoxDecoration(
                              color: scrollIndex == 0
                                  ? Colors.blue
                                  : Colors.grey.withOpacity(0.5),
                              borderRadius: BorderRadius.circular(15)),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(right: 5.0),
                        child: Container(
                          height: 15,
                          width: scrollIndex == 1 ? 40 : 20,
                          decoration: BoxDecoration(
                              color: scrollIndex == 1
                                  ? Colors.blue
                                  : Colors.grey.withOpacity(0.5),
                              borderRadius: BorderRadius.circular(15)),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(right: 5.0),
                        child: Container(
                          height: 15,
                          width: scrollIndex == 2 ? 40 : 20,
                          decoration: BoxDecoration(
                              color: scrollIndex == 2
                                  ? Colors.blue
                                  : Colors.grey.withOpacity(0.5),
                              borderRadius: BorderRadius.circular(15)),
                        ),
                      ),
                    ],
                  )

is it okay ? or there are some better ways ?

2

Answers


  1. You can use the list.generate function to reduce code repetition.

    Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: List.generate(3, (index) => 
        Padding(
          padding: EdgeInsets.only(right: index < 2 ? 5.0 : 0),
          child: AnimatedContainer(
            duration: Duration(milliseconds: 300),
            height: 15,
            width: scrollIndex == index ? 40 : 20,
            decoration: BoxDecoration(
              color: scrollIndex == index 
                ? Colors.blue 
                : Colors.grey.withOpacity(0.5),
              borderRadius: BorderRadius.circular(15),
            ),
          ),
        )
      ),
    )
    
    Login or Signup to reply.
  2. There are some suggestions I can make for your code block. In your code block biggest improvement can be done on Widget that you created for displaying indicators. Creating components for the widgets that we use multiple times in view is a nice approach in readibility and time wise.

    In your code block you can make a component for each of the widget that is under that row with some parameters to reduce code duplicates.

    For Example you can create this component;

    class ScrollIndicator extends StatelessWidget {
      final int currentIndex;
      final int index;
    
      const ScrollIndicator({
        Key? key,
        required this.currentIndex,
        required this.index,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Padding(
          padding: const EdgeInsets.only(right: 5.0),
          child: AnimatedContainer(
            duration: const Duration(milliseconds: 300),
            height: 15,
            width: currentIndex == index ? 40 : 20,
            decoration: BoxDecoration(
              color: currentIndex == index
                  ? Colors.blue
                  : Colors.grey.withOpacity(0.5),
              borderRadius: BorderRadius.circular(15),
            ),
          ),
        );
      }
    }
    

    And you can use it as following;

    Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: List.generate(3, (index) => ScrollIndicator(currentIndex: scrollIndex, index: index)),
        );
    

    Also there are some more recommendations I can make;

    Try to use colors from the theme instead of hard coding like Colors.blue.
    Instead assign the colors you use in your app to colorScheme of the theme and use it from there. By doing it you will be able to implement multi themes way more easier. For example let’s say you assign Colors.blue to primary color of your color scheme in theme . In this case you should use the color blue like following;

    Theme.of(context).colorScheme.primary; 
    

    If you do it like this , when you create a new scheme with a different primary color, it will be also changing in here automatically without coding anything.

    This are the best practices that I am sure that you need to implement. Other then that, there are one small thing which I apply in my projects that I think makes the code more readable. For the padding and radius constants, I prefer to create enums and use every padding and radius from there to make it more readable and easier to change.
    For Example create an enum like following:

    enum ModulePadding {
      ///5
      xxxs(5);
    
      final double value;
      const ModulePadding(this.value);
    }
    

    and use it as:

    EdgeInsets.only(right: ModulePadding.xxxs.value),
    

    I recommend same for also the radiuses.

    I hope it will be enough for you. I also have another suggestion that I used to have an idea on which approach is the best. You can search for popular onboarding libraries in pub.dev and check their GitHub repository to understand how is there approach.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search