2

I added text field for rows and columns for the user to input , now what should I specify in the set state of the button so that on clicking, it changes the grid according to the row and column user input.

TextEditingController row = TextEditingController();
  TextEditingController column = TextEditingController();

  int rowC = 2;
  int colC = 2;
.
.
Container(
                height: 300,
                child: Container(
                  padding: EdgeInsets.all(10),
                  child: GridView.builder(
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: rowC,childAspectRatio: colC*rowC/2 ,crossAxisSpacing: 10,mainAxisSpacing: 10),
                    itemCount: 2,
                    itemBuilder: (ctx,index){
                      return Container(
                        margin: EdgeInsets.all(10),
                        child: RaisedButton(
                          onPressed: (){}
                            );
                          },
                          color: Colors.blue,
                          child: Text(index.toString()),
                        ),
             Card(
               elevation: 5,
               child: Container(
                 padding: EdgeInsets.all(10),
                 child: Column(
                   crossAxisAlignment: CrossAxisAlignment.end,
                 children: <Widget>[
                   TextField(
                     controller: row,
                     decoration: InputDecoration(labelText: 'Row'),
                   ),
                   TextField(
                     controller: column,
                     decoration: InputDecoration(labelText: 'Column'),
                   ),
                   FlatButton(onPressed: (){
                     rowC = int.parse(row.text);
                     colC = int.parse(column.text);
                     setState(() {  
                     });
                   },
                       child: Text('Add'))
                 ],
         
 

1 Answer 1

1

enter image description here

class Class extends StatefulWidget {
  @override
  _ClassState createState() => _ClassState();
}

class _ClassState extends State<Class> {

  TextEditingController row = TextEditingController();
  TextEditingController column = TextEditingController();

  int rowC = 2;
  int colC = 2;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Container(
            height: 500,
            child: GridView.builder(
              itemCount: colC * rowC,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: rowC,childAspectRatio: colC*rowC/2 ,crossAxisSpacing: 10,mainAxisSpacing: 10),
              shrinkWrap: true,
              itemBuilder: (context, index) => Container(
                color: Colors.greenAccent,
              ),
            ),
          ),
          Text("Row"),
          TextField(
            controller: row,
          ),
          SizedBox(height: 20,),
          Text("Column"),
          TextField(
            controller: column,
          ),
          SizedBox(height: 20,),
          FlatButton(onPressed: (){
            rowC = int.parse(row.text);
            colC = int.parse(column.text);
            setState(() {

            });
          }, child: Container(
            color: Colors.purple,
              padding: EdgeInsets.all(20),
              child: Text("Add")))
        ],
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It's great that you give an example of how to do it, but it's even better if you can explain why.
You just need to add itemCount: colC * rowC in your code, if you write 6 and 3 in textfield it will 6 * 3 and its take 18 as itemCount

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.