VisualDensity is undefined?

Viewed 5183

I've installed this package login_screen package in flutter that should unlock the visualDensity in Theme Data in flutter but It doesn't but I checked the import and package version and everything but It always give me error that It is undefined. Here is my code : pubspec.yaml file: environment: sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_svg: ^0.17.4
  firebase_core: ^0.4.3+1
  firebase_auth: ^0.15.3
  modal_progress_hud: ^0.1.3
  confetti: ^0.4.0
  login_screen: ^0.6.0

dev_dependencies:
  flutter_test:
    sdk: flutter

and my main.dart file:

import 'package:flutter/material.dart';
import 'package:loginkit/ui/login/login.dart';
import 'package:login_screen/login_screen.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        fontFamily: 'Poppins',
        visualDensity:
      ),
      debugShowCheckedModeBanner: false,
      home: Login(),
    );
  }
}
3 Answers

For me, to fix this issue, I had to run Tools, Flutter, Flutter Upgrade in Android Studio.

visualDensity property

The density value for specifying the compactness of various UI components.

Density, in the context of a UI, is the vertical and horizontal "compactness" of the elements in the UI. It is unitless, since it means different things to different UI elements. For buttons, it affects the spacing around the centered label of the button. For lists, it affects the distance between baselines of entries in the list.

Typically, density values are integral, but any value in range may be used. The range includes values from VisualDensity.minimumDensity (which is -4), to VisualDensity.maximumDensity (which is 4), inclusive, where negative values indicate a denser, more compact, UI, and positive values indicate a less dense, more expanded, UI. If a component doesn't support the value given, it will clamp to the nearest supported value.

The default for visual densities is zero for both vertical and horizontal densities, which corresponds to the default visual density of components in the Material Design specification.

As a rule of thumb, a change of 1 or -1 in density corresponds to 4 logical pixels. However, this is not a strict relationship since components interpret the density values appropriately for their needs.

A larger value translates to a spacing increase (less dense), and a smaller value translates to a spacing decrease (more dense).

having this in mind visualDensity is a constructor of ThemeData which you can implement as follows visualDensity: VisualDensity.adaptivePlatformDensity,

I had the same problem, apparently it was because the ThemeData class had not been loaded correctly in memory, my solution was the following:

  • I clicked on a ThemeData nomenclature that if it appeared fine, I right-clicked it and selected Go to definition a primarySwatch: Colors.blue,

  • I opened the file C: \ BACKUP \ flutter \ packages \ flutter \ lib \ src \ material \ theme_data.dart.

  • I looked up the VisualDensity definition to see if it was there and found it.

  • After I had done that it automatically worked, like it automatically loaded the file into memory.

Before that I had already run the classic, flutter clean, flutter pub cache repair etc, however it hadn't worked for me.

Cheers!!!

Related