1
votes

sorry to bother you with this question. But im trying to get back into programming and i just cant get the following (a code example from flutter)

class MyAppBar extends StatelessWidget {
  MyAppBar({this.title});

  // Fields in a Widget subclass are always marked "final".

  final Widget title;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 56.0, // in logical pixels
      padding: const EdgeInsets.symmetric(horizontal: 8.0),
      decoration: BoxDecoration(color: Colors.blue[500]),
      // Row is a horizontal, linear layout.
      child: Row(
        // <Widget> is the type of items in the list.
        children: <Widget>[
          IconButton(
            icon: Icon(Icons.menu),
            tooltip: 'Navigation menu',
            onPressed: null, // null disables the button
          ),
          // Expanded expands its child to fill the available space.
          Expanded(
            child: title,
          ),
          IconButton(
            icon: Icon(Icons.search),
            tooltip: 'Search',
            onPressed: null,
          ),
        ],
      ),
    );
  }
}

class MyScaffold extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Material is a conceptual piece of paper on which the UI appears.
    return Material(
      // Column is a vertical, linear layout.
      child: Column(
        children: <Widget>[
          MyAppBar(
            title: Text(
              'Example title',
              style: Theme.of(context).primaryTextTheme.title,
            ),
          ),
          Expanded(
            child: Center(
              child: Text('Hello, world!'),
            ),
          ),
        ],
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    title: 'My app', // used by the OS task switcher
    home: MyScaffold(),
  ));
}

So I got couple of Questions in my head and some answers i just dont know if they're true. So it would be nice i u can help me and correc me.

  1. in the beginning of the class MyAppBar

    class MyAppBar extends StatelessWidget { MyAppBar({this.title});

    // Fields in a Widget subclass are always marked "final".

    final Widget title;

Why is this necessary ? Do we "tell", that everytime this widget is called a title is passed (and the title is a Widget) ?

  1. And whats the logic behind calling the same Widget from the MyScaffold (I know, that the logic is that u can change the title etc.). Is it like we build the Widget, with another Column Widget and than pass the MyAppBar to the Column Widget ? If yes, how does the Widget know, what to expect from the MyAppBar-Widget

    return Material( // Column is a vertical, linear layout. child: Column( children: [ MyAppBar( title: Text( 'Example title',

(why does it know that the title is "Searched")

Is it the reason it expects only a title cause we wrote

MyAppBar({this.title});

in the beginning ?

So am i right if i would write in the MyAppBar

MyAppBar({this.title});
MyAppNumber({this.number});

everytime the widget is called it expects 2 inputs ?

Hope my problem i understandable, thanks a lot for your help !!!

Source: https://flutter.dev/docs/development/ui/widgets-intro

maybe easier to read

EDIT: Or how could I add another AppBar below the other AppBar, maybe i understand it than better.

1

1 Answers

2
votes
  1. All fields are marked final because that class is immutable, so by definition, any and all fields must be marked final.

  2. When you extend StatelessWidget, you create a Widget, so MyAppBar is now a Widget just like Column or Center, so all rules regarding Widgets are in effect. It also expects a title because you referenced a title variable in your constructor for MyAppBar.

Hope this clarifies any questions you had!