0

How to create a GridView Layout and buttons using dart in flutter ?

Is the below code correct ? To create a GridView Layout and buttons in Flutter using Dart, you can follow these steps:

  1. Import the necessary Flutter packages:
import 'package:flutter/material.dart';
  1. Create a StatefulWidget to manage the state of your UI:
class MyGridPage extends StatefulWidget {
  @override
  _MyGridPageState createState() => _MyGridPageState();
}

class _MyGridPageState extends State<MyGridPage> {
  @override
  Widget build(BuildContext context) {
    // Your UI code will go here
  }
}
  1. Implement the build method inside _MyGridPageState to create the UI:
@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Grid Layout Example'),
    ),
    body: GridView.count(
      crossAxisCount: 2, // Number of columns
      children: <Widget>[
        // Add buttons or any other widgets here
        FlatButton(
          onPressed: () {
            // Button pressed action
          },
          child: Text('Button 1'),
        ),
        FlatButton(
          onPressed: () {
            // Button pressed action
          },
          child: Text('Button 2'),
        ),
        // Add more buttons as needed
      ],
    ),
  );
}

In this example, GridView.count is used to create a grid layout. You can adjust the crossAxisCount parameter to change the number of columns. Each child of the GridView is a button wrapped in a FlatButton widget. Replace FlatButton with any other widget you need, such as ElevatedButton or IconButton, depending on your design requirements.

  1. Finally, you can use MyGridPage in your main.dart file or wherever you need it:
void main() {
  runApp(MaterialApp(
    home: MyGridPage(),
  ));
}
1
  • I think the code you posted will be working fine. You can just check the code by running it in a project. Commented Apr 24, 2024 at 1:11

1 Answer 1

0

If you want to make a gridview of buttons then You can follow this code

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Grid Layout Example'),
      ),
      body: GridView.count(
        crossAxisCount: 2, // Number of columns
        crossAxisSpacing: 20, // Spacing between columns
        mainAxisSpacing: 20, // Spacing between grid items
        childAspectRatio: 3, // Width to height ratio of each grid item to adjust item size
        children: List.generate // Generate grid items
          (10, // Number of grid items
                (index) {
          return ElevatedButton(
            onPressed: () {
              // Button pressed action
            },
            child: Text('Button $index'),
          );
        }),
      ),
    );
  }

In this code, I user List.generate property to generate as many items I want and child aspect ratio(width/height) to adjust button size.

Sign up to request clarification or add additional context in comments.

Comments

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.