Search code examples
fluttertooltip

Tooltip message text and background color


I have Image.asset wrapped on Tooltip

Padding(
     padding: edgeInsets,
     child: Tooltip(preferBelow: false, message: "Miejsca parkingowe ogólnodostępne", child: Image.asset("assets/icons/garage.png", width: _iconSize, height: _iconSize,)),
),

How to set tooltip message text color and background color?


Solution

  • I think, Tooltip does'not provide a parameter to change these behaviors but

    Tooltip takes the your theme

    final ThemeData theme = Theme.of(context);
        final ThemeData darkTheme = ThemeData(
          brightness: Brightness.dark,
          textTheme: theme.brightness == Brightness.dark ? theme.textTheme : theme.primaryTextTheme,
          platform: theme.platform,
        );
    

    so you can create theme in your MaterialApp (Text color works but backgroundColor does'not work in my app, you can give a try)

    return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            backgroundColor: Colors.amber,
            primaryTextTheme: TextTheme(
              body1: TextStyle(
                color: Colors.blue,
              )
            ),
            primarySwatch: Colors.blue,
          ),
          home: ......
    

    or

    This is Tooltip code may be you can change from the source code of the Tooltip which is an bad solution.

        return Positioned.fill(
          child: IgnorePointer(
            child: CustomSingleChildLayout(
              delegate: _TooltipPositionDelegate(
                target: target,
                verticalOffset: verticalOffset,
                preferBelow: preferBelow,
              ),
              child: FadeTransition(
                opacity: animation,
                child: Opacity(
                  opacity: 0.9,
                  child: ConstrainedBox(
                    constraints: BoxConstraints(minHeight: height),
                    child: Container(
                      decoration: BoxDecoration(
                        color: darkTheme.backgroundColor,
                        borderRadius: BorderRadius.circular(2.0),
                      ),
                      padding: padding,
                      child: Center(
                        widthFactor: 1.0,
                        heightFactor: 1.0,
                        child: Text(message, style: darkTheme.textTheme.body1),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        );