sarathi
sarathi

Reputation: 1069

abstract class function not able to access anywhere in flutter

I m trying to use text style using abstract function like this

import 'dart:ui';
abstract class AppStyles {
  TextStyle getNameStyle() {
  return  TextStyle(
    fontSize: 40.0,
   );
}
}

this style I m trying to access like this

const Text(
          'Welcome to Flutter app',
          style: AppStyles.getNameStyle(),
        ),

Refer to this example also

Declaring a Styles file in Flutter

But its says this error

enter image description here

Upvotes: 3

Views: 643

Answers (2)

Artem Zelinskiy
Artem Zelinskiy

Reputation: 2210

Make function static:

  abstract class AppStyles {
     static TextStyle getNameStyle() {
       return  TextStyle(
         fontSize: 40.0,
       );
     }
  }

import correct library for TextStyle,

'package:flutter/material.dart'

not

'dart:ui' 

Upvotes: 0

Autocrab
Autocrab

Reputation: 3757

Either create function static

abstract class AppStyles {
     static TextStyle getNameStyle() {
       return  TextStyle(
         fontSize: 40.0,
       );
     }
  }

or create subclass of AppStyles and instantiate it:

class AppStylesImpl extends AppStyles {}

const Text(
      'Welcome to Flutter app',
      style: AppStylesImpl().getNameStyle(),
    ),

Upvotes: 2

Related Questions