flutter onboarding screen scroll indicator

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 but they were confusing so I tried implementing it by myself, not sure that its the 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 indicator,

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?

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),
        ),
      ),
    )
  ),
)

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật