1

I tried to implement a BottomNavigationBar to my app. But I don't get it to work.

So here's my code:

// Vertretungsplan Montag

class VPMontag extends StatelessWidget {
  String url;
  VPMontag(this.url);

final Completer _controller = Completer();



  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Vertretungsplan Montag'),
        ),
        body: Builder(builder: (BuildContext context) {
          return WebView(
            initialUrl: url,
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController webViewController) {
              _controller.complete(webViewController);
            },
            navigationDelegate: (NavigationRequest request) {
              if (request.url.startsWith('https://www.youtube.com/')) {
                print('blocking navigation to $request}');
                return NavigationDecision.prevent;
              }
              print('allowing navigation to $request');
              return NavigationDecision.navigate;
            },
            onPageStarted: (String url) {
              print('Page started loading: $url');
            },
            onPageFinished: (String url) {
              print('Page finished loading: $url');
            },
            gestureNavigationEnabled: true,
          );
        }));
  }
}



...

  vpmontag() {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => VPMontag('myurl')));
  }


  int _selectedIndex = 0;
  static TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static List<Widget> _widgetOptions = <Widget>[
    Text(
      'Index 0: Home',
      style: optionStyle,
    ),
    Center(
      child: Container(
        child: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: vpmontag,
              child: Text('Montag'),
            ),
          
            ),
          ],
        ),
      ),
    ),
    Text(
      'Index 2: School',
      style: optionStyle,
    ),
  ];



  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('VP App'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text('Startseite'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.view_headline),
            title: Text('Vertretungspläne'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.calendar_today),
            title: Text('Stundenpläne'),
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.blue[800],
        onTap: _onItemTapped,
      ),
    );
  }
}

But I get this Error:

Compiler message:
lib/main.dart:314:26: Error: Can't access 'this' in a field initializer to read 'vpmontag'.
              onPressed: vpmontag,
                         ^^^^^^^^

I have the class "VP Montag" five times but with other names.

How can I solve this? Do I have to add a "this.vpmontag" or so anywhere?

Any help is very appreciated.

5
  • Does this answer your question? What does the error "Can't access this in a field initializer mean"? Commented Jul 20, 2020 at 10:13
  • i have marked this as duplicate Commented Jul 20, 2020 at 10:14
  • @neuromancer Sorry but I don't get it. Commented Jul 20, 2020 at 10:21
  • Can nobody help me? Commented Jul 20, 2020 at 11:20
  • No one can help me? Commented Jul 20, 2020 at 12:48

1 Answer 1

2

just try to initialize _widgetOptions variable in initState() method of State class

List<Widget> _widgetOptions = [];

  @override
  void initState() {
    _widgetOptions = <Widget>[
      Text(
        'Index 0: Home',
        style: optionStyle,
      ),
      Center(
        child: Container(
          child: Column(
            children: <Widget>[
              RaisedButton(
                onPressed: vpmontag,
                child: Text('Montag'),
              ),
            ],
          ),
        ),
      ),
      Text(
        'Index 2: School',
        style: optionStyle,
      ),
    ];
    super.initState();
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.