0

I'm new in dart and I want to know what's the best implementation or the best practice between using a function or an object to do "simple" action

  1. Using a function
file: progress.dart

import 'package:flutter/material.dart';

Center buildProgressIndicator(BuildContext context) {
  return Center(
    child: new CircularProgressIndicator(
      valueColor: new AlwaysStoppedAnimation<Color>(Colors.green),
    ),
  );
}```

file: home.dart
...
if (!snapshot.hasData) return buildProgressIndicator(context);
...
  1. Using an objet
file: progress.dart

import 'package:flutter/material.dart';

class CustomProgressIndicator extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: new CircularProgressIndicator(
        valueColor: new AlwaysStoppedAnimation<Color>(Colors.green),
      ),
    );
  }
}

file: home.dart
...
if (!snapshot.hasData) return CustomProgressIndicator();
...

In the example above, the 2 versions seem to work in the same way. Therefore, I'm a little bit confused to do the best implementation choice !

RapazP
  • 101
  • 3
  • 1
    I marked it as duplicate of https://stackoverflow.com/questions/53234825/what-is-the-difference-between-functions-and-classes-to-create-widgets/53234826#53234826. If you disagree, feel free to ping me – Rémi Rousselet Dec 14 '19 at 12:37
  • thank you very much, that's exactly what I was looking for! – RapazP Dec 14 '19 at 14:11

0 Answers0