Flutter:超类变量到大写而不触及子类

Flutter: Superclass variable to upper case without touching subclass

这里我有一些关于这个场景的伪代码。

UpperCaseElement 超类具有 text 变量。如果 upperCase 为 true,文本变量应该变成 upperCase。

我试过在构造函数中设置它,但如果没有定义它只是默认值。

import "package:flutter/material.dart";

abstract class UpperCaseElement extends StatelessWidget {
  // Should be uppercase if upperCase = true
  final String text;

  final bool upperCase;

  // My attempt to set this.text to upperCase, but it doesn't work.
  const UpperCaseElement({Key? key, this.text = text.toUpperCase(), this.upperCase = false})
      : super(key: key);
}

class TestWidget extends UpperCaseElement {
  const TestWidget({
    Key? key,
    super.text,
    super.upperCase,
  }) : super(key: key);

  @override
  Widget build(context) {
    // Yes, the checking and converting to upper case can be done here. But doing that on all subclasses would be a pain. That's why I want to do it on the superclass.
    return Text(text);
  }
}

class TestPage extends StatelessWidget {
  const TestPage({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(context) {
    return Scaffold(
      body: Column(children: const [
        // Should be TEST
        TestWidget(
          text: "test",
          upperCase: true,
        )
      ]),
    );
  }
}

我的解决方案将定义一个 getter 函数,它将 return 基于 bool

的文本
abstract class UpperCaseElement extends StatelessWidget {
  // Should be uppercase if upperCase = true
  final String text;

  final bool upperCase;

  get getText => upperCase ? text.toUpperCase() : text;

  // My attempt to set this.text to upperCase, but it doesn't work.
  const UpperCaseElement({Key? key, required this.text, this.upperCase = false})
      : super(key: key);
}

class TestWidget extends UpperCaseElement {
  const TestWidget({
    Key? key,
    required String text,
    required bool upperCase,
  }) : super(key: key, text: text, upperCase: upperCase);

  @override
  Widget build(context) {
    // calling getText function from superClass.
    return Text(getText);
  }
}

class TestPage extends StatelessWidget {
  const TestPage({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(context) {
    return Scaffold(
      body: Column(children: const [
        // Should be TEST
        TestWidget(
          text: "test",
          upperCase: true,
        )
      ]),
    );
  }
}

更新:强制使用抽象 class,您不能使用 const 构造函数。

@immutable
abstract class UpperCaseElement extends StatelessWidget {
  String text;
  final bool upperCase;
  UpperCaseElement({Key? key, required this.text, this.upperCase = false})
      : super(key: key) {
    text = upperCase ? text.toUpperCase() : text;
  }
}

因为UpperCaseElement是一个抽象class,我们可以在TestWidget.

里面处理conditional
class TestWidget extends UpperCaseElement {
  const TestWidget({
    Key? key,
    required super.text,
    super.upperCase,
  }) : super(key: key);

  @override
  Widget build(context) {
    return Text(upperCase ? text.toUpperCase() : text);
  }
}

abstract class UpperCaseElement extends StatelessWidget {
  final String text;
  final bool upperCase;
  const UpperCaseElement({
    Key? key,
    required this.text,
    this.upperCase = false,
  }) : super(key: key);
}