How to check if Flutter Text widget was overflowed

I found a way to do it. Full code below, but in short: Use a LayoutBuilder to determine how much space we have. Use a TextPainter to simulate the render of the text within the space. Here’s the full demo app: import ‘package:flutter/material.dart’; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext … Read more

How to set state from another widget?

Avoid this whenever possible. It makes these widgets depends on each others and can make things harder to maintain in the long term. What you can do instead, is having both widgets share a common Listenable or something similar such as a Stream. Then widgets interact with each other by submitting events. For easier writing, … Read more

Creating function with variable number of arguments or parameters in Dart

You can’t do that for now. I don’t really know if varargs will come back – they were there some times ago but have been removed. However it is possible to emulate varargs with Emulating functions. See the below code snippet. typedef OnCall = dynamic Function(List arguments); class VarargsFunction { VarargsFunction(this._onCall); final OnCall _onCall; noSuchMethod(Invocation … Read more

How do you pass arguments from command line to main in Flutter/Dart?

There is no way to do that, because when you start an app on your device there are also no parameters that are passed. If this is for development, you can pass -t lib/my_alternate_main.dart to flutter run to easily switch between different settings where each alternate entry-point file calls the same application code with different … Read more

How do I build different versions of my Flutter app for qa/dev/prod?

Building on Seth’s idea, here’s an example that sets up a global representing the BuildEnvironment named env. env.dart import ‘package:meta/meta.dart’; enum BuildFlavor { production, development, staging } BuildEnvironment get env => _env; BuildEnvironment _env; class BuildEnvironment { /// The backend server. final String baseUrl; final BuildFlavor flavor; BuildEnvironment._init({this.flavor, this.baseUrl}); /// Sets up the top-level [env] … Read more

How to pass data from child widget to its parent

The first possibility is to pass a callback into your child, and the second is to use the of pattern for your stateful widget. See below. import ‘package:flutter/material.dart’; class MyStatefulWidget extends StatefulWidget { @override State<StatefulWidget> createState() => new MyStatefulWidgetState(); // note: updated as context.ancestorStateOfType is now deprecated static MyStatefulWidgetState of(BuildContext context) => context.findAncestorStateOfType<MyStatefulWidgetState>(); } class … Read more