Amila
Amila

Reputation: 63

How to write styles in flutter as classes

Let's say I have a souple of components using the same styles but I want it to write as classes like in html and css

Is there a way this is possible? By the way I'm a noob to coding started a year ago I'm 14 years old

Upvotes: 0

Views: 581

Answers (1)

Dark Sorrow
Dark Sorrow

Reputation: 1947

I have added an example for read only text field for your reference.

class ReadOnlyTextWidget extends StatefulWidget {
  final TextEditingController controller;
  final String label;
  final Function() onPressedCallback;
  const ReadOnlyTextWidget({
    required this.controller,
    required this.label,
    required this.onPressedCallback,
    Key? key,
  }) : super(key: key);

  @override
  State<ReadOnlyTextWidget> createState() => _ReadOnlyTextWidgetState();
}

class _ReadOnlyTextWidgetState extends State<ReadOnlyTextWidget> {
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: widget.controller,
      decoration: InputDecoration(
          label: Text(widget.label),
          hintText: 'Enter ${widget.label}',
          border: const OutlineInputBorder(),
          icon: TextButton.icon(
            onPressed: widget.onPressedCallback,
            label: const Text('Refresh'),
            icon: const Icon(Icons.refresh),
          )),
      readOnly: true,
    );
  }
}

Upvotes: 1

Related Questions