Home > OS >  Undefined name 'ui'. import 'dart:ui'; doesn't do the trick
Undefined name 'ui'. import 'dart:ui'; doesn't do the trick

Time:11-27

Dart ui doesn't import when I need to create a gradient, and so the debugger highlights the ui in ui.Gradient.Linear.
I tried importing dart:ui like it says to do in the docs but it doesn't seem to change anything.
my full code:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:html_editor_enhanced/html_editor.dart';
import 'dart:ui';

class Write extends StatelessWidget {
  // const Write({Key? key}) : super(key: key);

  HtmlEditorController controller = HtmlEditorController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Text(
              "write or copy/paste your beautiful creation",
              style: TextStyle(
                  height:5,
                  foreground: Paint()
                      ..shader = ui.Gradient.linear(
              const Offset(0, 20),
              const Offset(150, 20),
              <Color>[
                Colors.red,
                Colors.yellow,
              ],
            ))),
          const SizedBox(height: 40),
          HtmlEditor(
            controller: controller, //required
            htmlEditorOptions: const HtmlEditorOptions(
              hint: "Your text here...",
              // initialText: "and it came to pass...",
            ),
            otherOptions: const OtherOptions(
              height: 400,
            ),
          ),
        ],
      ),
    );
  }
}

CodePudding user response:

Credit to Jigar Patel in the comments!
I just needed to import 'dart:ui' as ui;

CodePudding user response:

There're two ways to solve this out:

  1. As jigar patel says to import 'dart:ui' as ui;.

  2. You just need to move out that ui from ui.Gradient.linear. and convert it like:

Text(
  "write or copy/paste your beautiful creation",
  style: TextStyle(
    height: 5,
    foreground: Paint()
      ..shader = Gradient.linear(
        const Offset(0, 20),
        const Offset(150, 20),
        <Color>[Colors.red, Colors.yellow],
      ),
  ),
),

But in your case the first way is most usefull. Because you need to import import 'package:flutter/material.dart'; for other material widgets. And that Gradient.linear is available for both import 'dart:ui' as ui; and 'package:flutter/material.dart';.

  • Related