Search code examples
listflutterconstructorflutter-layoutcard

How to add Dynamic Variables in a List in Flutter?


I have a reusableclass which has 39 different labels, and i have used

this.label0 -> this.label39 The issue is iam using a card and page viewer with itemcount so instead of copy and pasting the widget i want to add all the labels in a list:

for example Text(widget.label00)
like that Text(widget.label0) instead of making duplicates i want to add them in a list

i did add it to the list but am only seeing my class name when i enter translationText[i].toString()

if i don't put the toString i get an error saying: The argument type 'ReusableLarge' can't be assigned to the parameter type 'String

How do i go about doing this ?

class ReusableLarge extends StatefulWidget {
  final String quranImagepgs;
  final String portraitpg;
  // final String quranImagepg;
  final Function forward;
  final Function backward;
  final String labelpgNumber;
  final String label00;
  final String label0;
  final String label1;
  final String label2;
  final String label3;
  final String label4;
  final String label5;
  final String label6;
  final String label7;
  final String label8;
  final String label9;
  final String label10;
  final String label11;
  final String label12;
  final String label13;
  final String label14;
  final String label15;
  final String label16;
  final String label17;
  final String label18;
  final String label19;
  final String label20;
  final String label21;
  final String label22;
  final String label23;
  final String label24;
  final String label25;
  final String label26;
  final String label27;
  final String label28;
  final String label29;
  final String label30;
  final String label31;
  final String label32;
  final String label33;
  final String label34;
  final String label35;
  final String label36;
  final String label37;
  final String label38;
  final String label39;
  final String label40;
  final String label41;
  final String label42;
  final String label43;

  final double label00SidetoSidePositionBottom;
  final double label00SidetoSidePositionLeft;
  final double label00SidetoSidePositionRight;
  final double label0SidetoSidePositionBottom;
  final double label0SidetoSidePositionLeft;
  final double label0SidetoSidePositionRight;
  final double label1SidetoSidePositionBottom;
  final double label1SidetoSidePositionLeft;
  final double label1SidetoSidePositionRight;
  final double label2SidetoSidePositionBottom;
  final double label2SidetoSidePositionLeft;
  final double label2SidetoSidePositionRight;
  final double label3SidetoSidePositionBottom;
  final double label3SidetoSidePositionLeft;
  final double label3SidetoSidePositionRight;
  final double label4SidetoSidePositionBottom;
  final double label4SidetoSidePositionLeft;
  final double label4SidetoSidePositionRight;
  final double label5SidetoSidePositionBottom;
  final double label5SidetoSidePositionLeft;
  final double label5SidetoSidePositionRight;
  final double label6SidetoSidePositionBottom;
  final double label6SidetoSidePositionLeft;
  final double label6SidetoSidePositionRight;
  final double label7SidetoSidePositionBottom;
  final double label7SidetoSidePositionLeft;
  final double label7SidetoSidePositionRight;
  final double label8SidetoSidePositionBottom;
  final double label8SidetoSidePositionLeft;
  final double label8SidetoSidePositionRight;
  final double label9SidetoSidePositionBottom;
  final double label9SidetoSidePositionLeft;
  final double label9SidetoSidePositionRight;
  final double label10SidetoSidePositionBottom;
  final double label10SidetoSidePositionLeft;
  final double label10SidetoSidePositionRight;
  final double label11SidetoSidePositionBottom;
  final double label11SidetoSidePositionLeft;
  final double label11SidetoSidePositionRight;
  final double label00UpdownBottom;
  final double label00UpdownLeft;
  final double label00UpdownRight;
  final double label0UpdownBottom;
  final double label0UpdownLeft;
  final double label0UpdownRight;
  final double label1UpdownBottom;
  final double label1UpdownLeft;
  final double label1UpdownRight;
  final double label2UpdownBottom;
  final double label2UpdownLeft;
  final double label2UpdownRight;
  final double label3UpdownBottom;
  final double label3UpdownLeft;
  final double label3UpdownRight;
  final double label4UpdownBottom;
  final double label4UpdownLeft;
  final double label4UpdownRight;
  final double label5UpdownBottom;
  final double label5UpdownLeft;
  final double label5UpdownRight;
  final double label6UpdownBottom;
  final double label6UpdownLeft;
  final double label6UpdownRight;
  final double label7UpdownBottom;
  final double label7UpdownLeft;
  final double label7UpdownRight;
  final double label8UpdownBottom;
  final double label8UpdownLeft;
  final double label8UpdownRight;
  final double label9UpdownBottom;
  final double label9UpdownLeft;
  final double label9UpdownRight;
  final double label10UpdownBottom;
  final double label10UpdownLeft;
  final double label10UpdownRight;
  final double label11UpdownBottom;
  final double label11UpdownLeft;
  final double label11UpdownRight;
// ReusableLarge({Key key, this.label00}) : super(key: key);
  const ReusableLarge({
    Key key,
    this.forward,
    this.portraitpg,
    this.backward,
    this.quranImagepgs,
    this.labelpgNumber,
    this.label00,
    this.label0,
    this.label1,
    this.label2,
    this.label3,
    this.label4,
    this.label5,
    this.label6,
    this.label7,
    this.label8,
    this.label9,
    this.label10,
    this.label11,
    this.label12,
    this.label13,
    this.label14,
    this.label15,
    this.label16,
    this.label17,
    this.label18,
    this.label19,
    this.label20,
    this.label21,
    this.label22,
    this.label23,
    this.label24,
    this.label25,
    this.label26,
    this.label27,
    this.label28,
    this.label29,
    this.label30,
    this.label31,
    this.label32,
    this.label33,
    this.label34,
    this.label35,
    this.label36,
    this.label37,
    this.label38,
    this.label39,
    this.label40,
    this.label41,
    this.label42,
    this.label43,
    this.label00SidetoSidePositionBottom,
    this.label00SidetoSidePositionLeft,
    this.label00SidetoSidePositionRight,
    this.label0SidetoSidePositionBottom,
    this.label0SidetoSidePositionLeft,
    this.label0SidetoSidePositionRight,
    this.label1SidetoSidePositionBottom,
    this.label1SidetoSidePositionLeft,
    this.label1SidetoSidePositionRight,
    this.label2SidetoSidePositionBottom,
    this.label2SidetoSidePositionLeft,
    this.label2SidetoSidePositionRight,
    this.label3SidetoSidePositionBottom,
    this.label3SidetoSidePositionLeft,
    this.label3SidetoSidePositionRight,
    this.label4SidetoSidePositionBottom,
    this.label4SidetoSidePositionLeft,
    this.label4SidetoSidePositionRight,
    this.label5SidetoSidePositionBottom,
    this.label5SidetoSidePositionLeft,
    this.label5SidetoSidePositionRight,
    this.label6SidetoSidePositionBottom,
    this.label6SidetoSidePositionLeft,
    this.label6SidetoSidePositionRight,
    this.label7SidetoSidePositionBottom,
    this.label7SidetoSidePositionLeft,
    this.label7SidetoSidePositionRight,
    this.label8SidetoSidePositionBottom,
    this.label8SidetoSidePositionLeft,
    this.label8SidetoSidePositionRight,
    this.label9SidetoSidePositionBottom,
    this.label9SidetoSidePositionLeft,
    this.label9SidetoSidePositionRight,
    this.label10SidetoSidePositionBottom,
    this.label10SidetoSidePositionLeft,
    this.label10SidetoSidePositionRight,
    this.label11SidetoSidePositionBottom,
    this.label11SidetoSidePositionLeft,
    this.label11SidetoSidePositionRight,
    this.label00UpdownBottom,
    this.label00UpdownLeft,
    this.label00UpdownRight,
    this.label0UpdownBottom,
    this.label0UpdownLeft,
    this.label0UpdownRight,
    this.label1UpdownBottom,
    this.label1UpdownLeft,
    this.label1UpdownRight,
    this.label2UpdownBottom,
    this.label2UpdownLeft,
    this.label2UpdownRight,
    this.label3UpdownBottom,
    this.label3UpdownLeft,
    this.label3UpdownRight,
    this.label4UpdownBottom,
    this.label4UpdownLeft,
    this.label4UpdownRight,
    this.label5UpdownBottom,
    this.label5UpdownLeft,
    this.label5UpdownRight,
    this.label6UpdownBottom,
    this.label6UpdownLeft,
    this.label6UpdownRight,
    this.label7UpdownBottom,
    this.label7UpdownLeft,
    this.label7UpdownRight,
    this.label8UpdownBottom,
    this.label8UpdownLeft,
    this.label8UpdownRight,
    this.label9UpdownBottom,
    this.label9UpdownLeft,
    this.label9UpdownRight,
    this.label10UpdownBottom,
    this.label10UpdownLeft,
    this.label10UpdownRight,
    this.label11UpdownBottom,
    this.label11UpdownLeft,
    this.label11UpdownRight,
  }) : super(key: key);

@override
  void initState() {
    super.initState();
    List<String> tests = [widget.label00, widget.label0];







       bottomSheet: SolidBottomSheet(
                controller: _controller,
                draggableBody: true,
                showOnAppear: true,
                maxHeight: 220,
                headerBar: Container(
                  color: Colors.white,
                  height: 12,
                ),
                body: Center(
                    child: SizedBox(
                        height: height * 0.5, // card height
                        // width: 1000,
                        child: PageView.builder(
                            itemCount: 3,
                            // if(i==0) && ,

                            //  translationText.length,
                            // itemCount: translationText.length,
                            controller: _pageController,
                            // (PageController(viewportFraction: 0.7)),
                            // viewportFraction:0.7),
                            // PageController(viewportFraction: 0.7),
                            // allowImplicitScrolling: true,

                            onPageChanged: (int index) =>
                                setState(() => _index = index),
                            itemBuilder: (_, i) {
                              return Transform.scale(
                                  scale: i == _index ? 1 : 0.9,
                                  child: Container(
                                      height: 200,
                                      // width: 1000,
                                      decoration: BoxDecoration(
                                          gradient: LinearGradient(
                                              colors: [
                                            HexColor(styling.Hexcolor1),
                                            HexColor(styling.Hexcolor2),
                                            HexColor(styling.Hexcolor3),
                                          ],
                                              begin: Alignment.centerLeft,
                                              end: Alignment.centerRight)),
                                      child: Visibility(
                                          visible: visibilityController,
                                          child: Card(
                                            // Card(
                                            color: Colors.transparent,
                                            shape: RoundedRectangleBorder(
                                              // side: BorderSide(
                                              //     color: Colors.redAccent, width: 1),
                                              borderRadius:
                                                  BorderRadius.circular(20),
                                            ),
                                            shadowColor: Colors.cyan,
                                            elevation: 8,
                                            child: Stack(
                                              children: [
                                                Center(child: FutureBuilder<
                                                        Widget>(
                                                    // future: LoadingShimmer(),
                                                    builder:
                                                        (context, snapshot) {
                                                  return LoadingShimmer();
                                                })),
                                                Center(
// String x="";if(i==1){
//     x= translationText[i].label0
// Widget _translationtext(){
                                                  //  String x=="";  if (i==1) {
                                                  //       return(Text(widget.label00))
                                                  //     } else {
                                                  //       return(Text(widget.label0)),
                                                  //     }

                                                  child:
                                                      // _translationsdifferent(
                                                      // i)),
// _translationsdifferent),
                                                      // getString(i, translationText),
                                                      Text(
                                                    //  translationText[i],
                                                    widget.label00,
                                                    // widget.tests[i],
                                                    // widget.,
                                                    //   if (i==1){
                                                    //  child = translationText[i].label00,
                                                    //   }else if(i==2){
                                                    //   translationText[i].label0,
                                                    //   }
                                                    // super.initState(widget.label00,widget.label0),
                                                    // superinit(widget.label00),
                                                    // widget.label5,
                                                    // translationText,
                                                    // "translationText${i + 1}",
                                                    // "AYAH 24: If you cannot do this- and you never will- then beware of the Fire prepared for the disbelievers, whose fuel is men and stones.",
                                                    // "Card ${i + 1}",
                                                    textAlign: TextAlign.center,
                                                    style: TextStyle(
                                                        color: styling
                                                            .allButtonsTextColor,
                                                        fontSize: 20,
                                                        // styling
                                                        // .allButtonsSurahFontSizeSidetoSide,
                                                        fontWeight: styling
                                                            .allButtonsTextWeight,
                                                        fontStyle: styling
                                                            .allButtonsTextStyle),
                                                  ),

Solution

  • you should have a list with 39 strings instead 39 diferents strings to can access them in this way.

    translationText[i].labelList[i]