Wrap Widget - Place widgets as per the available size

This Article is posted by seven.srikanth at 12/2/2022 5:07:35 AM



Using Wrap widget you can place widgets as per the available size as shown below. 
It's super useful. The available properties hep you to move the widgets to next row or column. 
Here is the complete code for the above example.

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  final title = 'Wrap Widget example';
  @ override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: title,
      home: MyHomePage(
        title: title,
      ),
    );
  }
}
class MyHomePage extends StatefulWidget {
  final String title;
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  @ override
  State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  bool _first = true;
  @ override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Wrap(
          spacing: 8.0,
          runSpacing: 8.0,
          children: const [
            MyContainer(),
            MyContainer(),
            MyContainer(),
            MyContainer(),
            MyContainer(),
            MyContainer(),
            MyContainer(),
            MyContainer(),
          ],
        ),
      ),
    );
  }
}
class MyContainer extends StatelessWidget {
  const MyContainer({
    super.key,
  });
  @ override
  Widget build(BuildContext context) {
    return Container(
      height: 80,
      width: 80,
      color: Colors.blue,
    );
  }
}
Thanks,
Srikanth

Tags: Align widgets; Move to next row; move to next column;








0 Comments
Login to comment.
Recent Comments












© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com