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
You can use the
list.generate
function to reduce code repetition.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;
And you can use it as following;
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;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:
and use it as:
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.