Search code examples
androidgradleflutternavigator

Navigator.push in a ListView, ListTile not working


I am writing a flutter app with a navigational drawer, tabcontroller and a list view in the tabs. Now, from the items in the listview i want to navigate to a new screen and i can't get it to work. Here is my code:


class ChooseDiff extends StatelessWidget {
  static const String routeName = "ChooseDiff";

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new DefaultTabController(
        length: 3,
        child: new Scaffold(
          appBar: new AppBar(
            title: const Text('Tabbed AppBar'),
            bottom: new TabBar(isScrollable: true, tabs: [
              new Tab(text: 'Enkla',),
              new Tab(text: 'Svåra',),
              new Tab(text: 'Top 10',),
            ]),
          ),
          body: new TabBarView(
            children: [
              new ListView(
                children: list,
              ),
              new ListView(
                children: list1,
              ),
              new ListView(
                children: list3,
              )
            ],
          ),
        ),
      ),
    );
  }
}


  List<Widget> list = <Widget>[
    new ListTile(
      title: new Text('H&M',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Icon(
        Icons.add_to_home_screen,
        color: Colors.blue[500],
      ),
    ),
    new ListTile(
      title: new Text('Adidas',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Icon(
        Icons.add_to_home_screen,
        color: Colors.blue[500],
      ),
    )
  ];


List<Widget> list1 = <Widget>[

  new ListTile(
    title: new Text('H&M',
        style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
    leading: new Icon(
      Icons.add_to_home_screen,
      color: Colors.blue[500],
    ),
    onTap: () {
      @override
      BuildContext context;
      Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => HM()),
      );
    },
  ),
  new ListTile(
    title: new Text('Adidas',
        style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
    leading: new Icon(
      Icons.add_to_home_screen,
      color: Colors.blue[500],
    ),
    onTap: () {
      @override
      BuildContext context;
      Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => Adisas()),
    );
  }
  )
];



List<Widget> list3 = <Widget>[

  new ListTile(
    title: new Text('H&M',
        style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
    leading: new Icon(
      Icons.add_to_home_screen,
      color: Colors.blue[500],
    ),
    onTap: () {
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => HM()),
      );
    },
  ),
  new ListTile(
      title: new Text('Adidas',
          style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
      leading: new Icon(
        Icons.add_to_home_screen,
        color: Colors.blue[500],
      ),
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => Adisas()),
        );
      }
  )
];




//Lägg till alla Svåra Texter här

TextStyle defaultStyle = TextStyle(fontSize: 30, color: Colors.black);


class HM extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("H&M"),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Padding(
                padding: const EdgeInsets.fromLTRB(5, 50, 0, 0),
                child: RichText(
                  text: (TextSpan(
                      style: defaultStyle,
                      text: 'Vi är Truth About Business!'
                  )
                  ),
                )
            ),
          ],
        ),
      ),
    );
  }
}

class Adisas extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Adidas"),
        ),
          body: Container(
            child: Column(
            children: <Widget>[
              Padding(
              padding: const EdgeInsets.fromLTRB(5, 50, 0, 0),
                child: RichText(
                  text: (TextSpan(
                    style: defaultStyle,
                      text: 'Vi är Truth About Business!'
                )
               ),
              )
            ),
          ],
        ),
      ),
    );
  }
}

When trying to build this code i get this error:

Initializing gradle...
Resolving dependencies...
Running Gradle task 'assembleDebug'...
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)

Compiler message:
lib/Screens/Texter/TabController.dart:109:9: Error: Getter not found: 'context'.
        context,
        ^^^^^^^
lib/Screens/Texter/TabController.dart:123:11: Error: Getter not found: 'context'.
          context,
          ^^^^^^^
Compiler failed on C:\Users\tbsvst18tedbom\AndroidStudioProjects\tab_truth_true\lib\main.dart

FAILURE: Build failed with an exception.

* Where:
Script 'C:\Users\tbsvst18tedbom\Desktop\Appen\Flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 765

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebugArm64'.
> Process 'command 'C:\Users\tbsvst18tedbom\Desktop\Appen\Flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 29s
Finished with error: Gradle task assembleDebug failed with exit code 1

I don't know what else to try so I hope someone can help me with an alternative code or something to help me.

I appreciate the help alot! Thank you so, so very much!


Solution

  • Your lists are defined globally, and therefore they have no context. In order for the navigator to work, it needs to know your top-most context at the time of the tap. What you need to do is get your current context to lists.

    One way of doing that is to put your list definitions in ChooseDiff class and create the list on demand, something like:

    class ChooseDiff extends StatelessWidget {
      // code
      List<Widget> _list1(BuildContext context) => <Widget>[
        // your list here
      ];
    
      // your TabView code
      children: [
        ListView(children: _list1(context),
      ],