Home > Software engineering >  Is there a way of dynamically switching AppBar Title based on a condition?
Is there a way of dynamically switching AppBar Title based on a condition?

Time:08-11

I've tried a few different methods but nothing seems to work The idea is if the user is creating a new note then display new note else if tapping on existing note display Edit note/Existing note

      appBar: AppBar(
    title:
        //New note or Existing note
        Text(
      //_note.text
      _note?.text != null && _textController.text.isEmpty ||
              _note?.text != null && _textController.text.isNotEmpty
          ? 'New note'
          : 'Existing note',
    ),

CodePudding user response:

I think you do not have to check for the text field value. Just checking the _notes would do the trick.

appBar: AppBar(
title:
    Text(
        _note != null ? 'New note' : 'Existing note',
    ),
),

CodePudding user response:

Simple example:

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
  _counter  ;
});
}

@override
Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
      title: Text(_counter == 0 ? "Hello" : "GoodBye"),
    ),
    body: Container(),
    floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: const Icon(Icons.add),
  ), // This trailing comma makes auto-formatting nicer for build methods.
  );
 }
}
  • Related