bilbo_bo
bilbo_bo

Reputation: 89

How to access a function from another class - Flutter

I have this function called getTextColor but I've had to declare it in another class. How do I access this function from a different class?

Here is how I'm trying to access it:

class _NextPageState extends State<NextPage> with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0,
        ),
        body: Center(
          child: Container(
            child: RichText(
                text: TextSpan(children: [
              TextSpan(
                  text: widget.result + '  ',
                  style: TextStyle(
                    fontSize: 20.0,
                    fontWeight: FontWeight.bold,
                    color: getTextColor(widget.result), // Here is the problem.
                    height: 2.5,
                    letterSpacing: 0.7,
                  )),

The function's logic is inside a class called class _MainScreenState extends State<MainScreen>

To sum up, how do I tell the code that getTextColor is from the other class?

Upvotes: 1

Views: 508

Answers (1)

Nazarii Kahaniak
Nazarii Kahaniak

Reputation: 511

You need to create object of another clas or make the function static in order to access it. If the function is static then you can call it like this:

ClassName.getTextColor();

If the function is not static then you need to create an object to call it like this:

var object = ClassName();
object.getTextColor();

Upvotes: 4

Related Questions