styled_widget icon indicating copy to clipboard operation
styled_widget copied to clipboard

name conflicts with the origin properties

Open ash0080 opened this issue 3 years ago • 6 comments

.textWidthBasic() .textDirection() conflict with the origin same name properties

Screen Shot 2022-04-24 at 13 07 52

Flutter 2.13.0-0.2.pre • channel beta • https://github.com/flutter/flutter.git Framework • revision 8662e22bac (4 days ago) • 2022-04-20 08:21:52 -0700 Engine • revision 24a02fa5ee Tools • Dart 2.17.0 (build 2.17.0-266.5.beta) • DevTools 2.12.2

ash0080 avatar Apr 24 '22 05:04 ash0080

I would actually not recommend using methods for properties of Text since each method creates a new Text widget. If you have any suggestions for a solution, I would like to hear!

ReinBentdal avatar Apr 24 '22 14:04 ReinBentdal

I would actually not recommend using methods for properties of Text since each method creates a new Text widget. If you have any suggestions for a solution, I would like to hear!

according dart document about const and here

If you can eventually generate a const Text(), there should be no runtime performance loss. But I don't think it can be reached unless dart modifies itself.

Cuz const in dart is not const as we understand it in other languages, I think it's better to think of it as a static block in memory, so it's understandable that it has so many restrictions, and any variables returned by method can't be a const, and for class constructor, it should be a pure data constructor ( with no body ), so you can't build a const copyWith(), because the compiling process doesn't execute any dart code.

So, how about fixing the known naming conflicts first? Otherwise, the alternative is to implement the code generator. But I personally hate code generator. I think it's a bit like introducing a big monster to fix a small flaw.

ash0080 avatar Apr 24 '22 23:04 ash0080

I agree. Don't want to deal with code generation if there are other alternatives. I have previously thought about creating a separate Text data class with non-mutable members. Then add a method for generating a text widget from the class.

StyledText("some text")
 .bold()
 .size(20)
 .toText()

or something like that.

Btw i do not really prioritize development of this library at the moment. I am open for discussion but I won't make large changes in the near future. If you would like to contribute I would be more than happy to accept contributions.

ReinBentdal avatar Apr 25 '22 13:04 ReinBentdal

Update: I learned this in an unrelated official video. If define a widget as a static final, can prevent it from rebuild, I guess this is the least expensive workaround so far

class Basic extends StatelessWidget {
  const Basic({Key? key}) : super(key: key);
  static final text = const Text(
    'some text',
  )
      .textScale(1.5)
      .italic()
      .bold()
      .fontWeight(FontWeight.w200)
      .fontSize(20)
      .fontFamily('sans-serif')
      .letterSpacing(5.0)
      .wordSpacing(20.0)
      .textShadow(
        color: Colors.black,
        blurRadius: 10.0,
        offset: const Offset(5.0, 5.0),
      )
      .textElevation(11)
      .textColor(Colors.green);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        text, 
      ],
    );
  }
}

ash0080 avatar Apr 26 '22 05:04 ash0080

that's an interesting approach!

But it is still somewhat unsatisfactory. It works well as long as the widget is final. If you later decide to include an animation on the text, its no longer as easy to include without impacting performance.

ReinBentdal avatar Apr 26 '22 18:04 ReinBentdal

that's an interesting approach!

But it is still somewhat unsatisfactory. It works well as long as the widget is final. If you later decide to include an animation on the text, its no longer as easy to include without impacting performance.

This reminds me that there is a hookBuilder in flutter_hooks, which is the equivalent of a widget scope, that is used to solve the problem you mentioned, as ref

ash0080 avatar May 05 '22 16:05 ash0080