skip to Main Content

I am confused at the fact that, are the classes like TextStyle, MainAxisAlignment are widgets or just some classes?

If they are not widgets, how can I distinguish them?

4

Answers


  1. ///
    /// [TestWidget] is a widget which extends [StatelessWidget] class.
    /// [StatelessWidget] is a widget which is extended [Widget] class.
    ///
    class TestWidget extends StatelessWidget {
      const TestWidget({super.key});
    
      @override
      Widget build(BuildContext context) {
        ///
        /// [Column] is a widget which extends [Flex] class.
        /// [Flex] is a widget which extends [MultiChildRenderObjectWidget] class.
        /// [MultiChildRenderObjectWidget] is a widget which extends [RenderObjectWidget] class.
        /// [RenderObjectWidget] is a widget which extends [Widget] class.
        ///
        return const Column(
          ///
          /// [Column] has a property named [mainAxisAlignment] which is an enum of type [MainAxisAlignment].
          /// [MainAxisAlignment] is an enum which has 6 values.
          ///
    
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ///
            /// [TextStyle] is a class.
            ///
            Text("Test", style: TextStyle()),
          ],
        );
      }
    }
    
    Login or Signup to reply.
  2. Widget is just a kind of class. I will write some examples:

    • Model classes: ProfileModel, CartModel, ProductModel… These model classes are used to store somebody’s profile information, someone’s cart information, a product is buscuit or frozen meat…
    • Utility classes: contain some functions such as convert from string to int, convert from USD to Yen, format datetime…
    • Widget classes: In Flutter, they may be a StatefulWidget class, StatelessWidget class… The purpose is UI display.

    As I see, the frameworks nowaday use component styles, web or mobile applications are designed in components. Widget is a component, and it belongs to UI class.

    Login or Signup to reply.
  3. this is an example using TextStyle and Text widgets in Flutter.

    import 'package:flutter/material.dart';
    
    void main() {
    runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
     return MaterialApp(
       home: Scaffold(
         appBar: AppBar(
           title: Text('TextStyle Example'),
         ),
        body: Center(
          child: Text(
            'Hello, Flutter!',
            style: TextStyle(
              color: Colors.blue,
              fontSize: 24,
              fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ),
       );
      }
    }
    

    In this example:

    TextStyle is a configuration class used within the Text widget to define the style of the displayed text.
    Text is a widget that displays a string of text on the screen.
    The TextStyle class configures the appearance of the text displayed by the Text widget. In this case:

    color sets the text color to blue.

    fontSize sets the font size to 24.

    fontWeight sets the font weight to bold.

    While TextStyle itself is not a widget, it’s crucial for specifying how the Text widget should display its content. The Text widget is the actual UI element that renders text on the screen, and TextStyle is used as a property to style that text.

    Login or Signup to reply.
  4. All widgets are class, but not all class are widget.

    Those who (class) extend Widget class is also a widget.

    You can check widgets-library..

    StatefulWidget or StatelessWidget can be called both widget and class.

    If it is not directly extend the Widget class, you will find an ancestor doing it, e.g SingleChildRenderObjectWidget or MultiChildRenderObjectWidget extends the RenderObjectWidget which parent is Widget.

    There are many classes which is not a widget , DateTime,TextSpan,TextEditingController, Animation

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search