Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Flutter packages widgets.dart, material.dart and cupertino.dart and which one to use?

Tags:

When using flutter, I often come around a type being exposed by different packages in the standard library.

For example the FlutterError but the same applies to other widgets. When writing it and using IntelliSense in VS Code to resolve the package to import so that FlutterError is available, IntelliSense offers me FlutterError in multiple different packages:

In this case, it's available from

  • package:flutter/widgets.dart
  • package:flutter/foundation.dart
  • package:flutter/material.dart
  • package:flutter/cupertino.dart
  • package:flutter/rendering.dart

My understanding is that material.dart and cupertino.dart offer widgets in the particular style - but what is the rest and which package should I import in a MaterialApp(..) that should be platform independent and also run on iOS?

And - if it makes a difference - which one should be used so that platform-specific widgets are automatically used on Android & iOS for native functionality like Date/Timepickers so that a Datepicker is automatically the platform specific variant on Android and the platform specific variant on iOS?

like image 543
sceee Avatar asked Jun 27 '19 19:06

sceee


People also ask

What is difference between material and Cupertino in Flutter?

When you write a Material app in Flutter, it has the Material look and feel on all devices, even iOS. If you want your app to look like a standard iOS-styled app, then you would use the Cupertino library.

What is Cupertino widget in Flutter?

The Material widget implements material design language for iOS, Android, web, and desktop applications. The Cupertino widget on the other hand is used to implement the current iOS design language based on Apple's human interface guidelines.

What are widgets and types of widgets in Flutter?

Widgets describe what their view should look like given their current configuration and state. It includes a text widget, row widget, column widget, container widget, and many more. Widgets: Each element on a screen of the Flutter app is a widget.

What is import package Flutter Cupertino Dart '; for?

Flutter widgets implementing the current iOS design language. To use, import package:flutter/cupertino. dart . This library is designed for apps that run on iOS. For apps that may also run on other operating systems, we encourage use of other widgets, for example the Material Design set.


2 Answers

What happens is, the class is defined in a lower part of the framework (here foundation for FlutterError)

And then higher layers of the framework reexport the content of the lower layers, using the export directive.

This is done mainly to make sure that developers don't have to import 5+ different things when they usually need them all.

For example material.dart looks roughltly like this:

export 'package:flutter/widget.dart';

class RaisedButton { }
like image 173
Rémi Rousselet Avatar answered Nov 10 '22 19:11

Rémi Rousselet


You can import generic classes from any library. This example:

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

class Foo extends InheritedWidget {
}

Will land to the same InheritedWidget implementation no matter which library you've imported it from. Try to comment out each import and see by Ctrl+Click on the InheritedWidget in AndroidStudio which file with the implementation will be opened.

From my point of view, it's better using one single import import 'package:flutter/material.dart'; rather than two (from material.dart and widgets.dart) if working with material design. If your code is neutral to the UI library then it's obvious to import from system libraries.

You can not use a generic class imported from a neutral system library in intention to switch automatically between Cupertino and Material libraries in compile time. You should use specific UI library widgets explicitly.

If you want to switch between libraries you should develop this logic by yourself. You can read this article https://medium.com/flutter/do-flutter-apps-dream-of-platform-aware-widgets-7d7ed7b4624d as a starting point.

like image 36
Alexander Pravdin Avatar answered Nov 10 '22 20:11

Alexander Pravdin