Is there a way to show image inside text in flutter

纵然是瞬间 提交于 2021-01-28 14:10:55

问题


I have this code to show an image inside text in flutter but when I'm using ImageShader. I'm getting this error The method 'asset' isn't defined for the type 'Image'. Try correcting the name to the name of an existing method, or defining a method named 'asset'

I have also referred to this Repo. This repo link was given in previously asked question on StackOverflow about ImageShader.
I know "dart:ui" doesn't contain any asset method. So I used Image.asset but when I used this method I got this error.

The argument type 'Image (where Image is defined in /Users/username/Developement/flutter/packages/flutter/lib/src/widgets/image.dart)' can't be assigned to the parameter type 'Image (where Image is defined in /Users/username/Developement/flutter/bin/cache/pkg/sky_engine/lib/ui/painting.dart)'

Link to the similar question asked on stackoverflow

 import 'dart:ui' as ui;    
    class _MyAppState extends State<MyApp> {
  Float64List matrix4 = new Matrix4.identity().storage;
  ui.Image img;

  Future<ui.Image> getImage() async{
    img = await ui.Image.asset("assets/images/ob_girl_cropped.png"); // This Line Is showing error. Please Help.
  }
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: Center(
          child: FutureBuilder(
            future: getImage(),
            builder: (context, snapshot)
            {
              if(snapshot.hasData){
                return Text(
                  'Greetings, planet!',
                  style: TextStyle(
                    fontSize: 40,
                    foreground: Paint()
                      ..shader = ImageShader(
                          img,
                          TileMode.clamp,
                          TileMode.clamp,
                          matrix4),
                  ),
                );
              }
              else{
               return CircularProgressIndicator();
              }
            },
          ),
        ),
      ),
    );
  }
}

回答1:


Two problems:

  1. The way you load image, which I wrote a loadImageFromFile helper funtion
  2. You should not put getImage() directly in to future field, because flutter will create a new future every time you rebuild the page (so load the image multiple times)

Full Code:

import 'dart:typed_data';
import 'package:flutter/services.dart' show rootBundle;

import 'package:flutter/material.dart';
import 'dart:ui' as ui;

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Float64List matrix4 = new Matrix4.identity().storage;
  Future<ui.Image> imgFuture;

  // New helper function
  Future<ui.Image> loadImageFromFile(String path) async {
    var fileData = Uint8List.sublistView(await rootBundle.load(path));
    return await decodeImageFromList(fileData);
  }

  @override
  void initState() {
    imgFuture = loadImageFromFile("assets/images/ob_girl_cropped.png"); // Works now
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: Center(
          child: FutureBuilder(
            future: imgFuture,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Text(
                  'Greetings, planet!',
                  style: TextStyle(
                    fontSize: 40,
                    foreground: Paint()..shader = ImageShader(snapshot.data, TileMode.clamp, TileMode.clamp, matrix4),
                  ),
                );
              } else {
                return CircularProgressIndicator();
              }
            },
          ),
        ),
      ),
    );
  }
}


来源:https://stackoverflow.com/questions/62937882/is-there-a-way-to-show-image-inside-text-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!