How to use Flow widget in Flutter

In this Google flutter code example we are going to learn how to use Flow widget in Flutter.

You can copy and adopt this source code example to your android project without reinventing the wheel.

The comments on the code provide more detailed explanation

The code is based on flutter version 1.

main.dart (App entry point)

import 'package:flutter/material.dart';
import 'flow.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Baseline Widget',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: BasicFlow(),
    );
  }
}

flow.dart

import 'package:flutter/material.dart';

class BasicFlow extends StatelessWidget {
  //A widget that sizes and positions children efficiently, according to the logic in a FlowDelegate.
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Flow Widget")),
      body: Center(
        child: Flow(
          delegate: _MyFlowDelegate(),
          children: <Widget>[
            _buildContainer(),
            _buildContainer(),
            _buildContainer(),
            _buildContainer(),
            _buildContainer(),
          ],
        ),
      ),
    );
  }

  Widget _buildContainer() {
    return Container(
      margin: EdgeInsets.only(bottom: 8.0, top: 8.0),
      height: 50.0,
      width: 50.0,
      color: Colors.red,
    );
  }
}

class _MyFlowDelegate extends FlowDelegate {
  @override
  void paintChildren(FlowPaintingContext context) {
    double dy = 0.0;
    double opacity = 0.2;
    for (int i = 0; i < context.childCount; ++i) {
      dy = context.getChildSize(i).height * i;

      context.paintChild(
        i,
        transform: Matrix4.translationValues(
          0,
          dy * 0.7,
          0,
        ),
      );
    }
  }

  @override
  bool shouldRepaint(_MyFlowDelegate oldDelegate) {
    // false just for now
    return false;
  }
}

class FlowDeg extends FlowDelegate {
  //we define a FlowDeletegate that will paint each child with different opacity values
  @override
  void paintChildren(FlowPaintingContext context) {
    //get the total number of children
    int count = context.childCount;
    context.paintChild(0, opacity: .2);
    context.paintChild(1, opacity: .4);
    context.paintChild(2, opacity: .6);
    context.paintChild(3, opacity: .8);
    context.paintChild(4);
  }

  @override
  bool shouldRepaint(FlowDelegate oldDelegate) {
    return true;
  }
}

 

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment