Search code examples
flutterflutter-dependencies

From the Navbar, im trying to route to anthother page using "Navigator.of(context).pushNamed('lib/first_page.dart')), but not working


Click on Listtile ontap link, doesnt go to firstpage enter image description here
First page code:
Not sure what i am doing wrong sorry all.

// TODO Implement this library.
import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(home: AddTwoNumbers()));
}

class AddTwoNumbers extends StatefulWidget {
  const AddTwoNumbers({super.key});

  @override
  State<AddTwoNumbers> createState() => _AddTwoNumbersState();
}

class _AddTwoNumbersState extends State<AddTwoNumbers> {
  List<TextEditingController> roundOneFields = [];
  List<TextEditingController> roundTwoFields = [];
  int roundOneResult = 0;
  int roundTwoResult = 0;

  @override
  void initState() {
    roundOneFields =
        List.generate(20, (index) => TextEditingController(text: '0'));
    roundTwoFields =
        List.generate(20, (index) => TextEditingController(text: '0'));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Twin City Bowmen Score Sheet'),
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.notifications),
            onPressed: () {},
          )
        ],
        backgroundColor: Colors.orangeAccent,
        leading: IconButton(
          icon: const Icon(Icons.menu),
          onPressed: () {},
        ),
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(25),
                bottomRight: Radius.circular(25))),
      ), // This is how you can add an appBar.
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Row(
              children: [
                Expanded(
                  child: ListView.builder(
                    itemCount: 20,
                    shrinkWrap: true,
                    itemBuilder: (context, index) => Row(
                      children: <Widget>[
                        Text("R1 Target ${index + 1}:"),
                        Flexible(
                          child: TextField(
                            decoration: const InputDecoration(
                                contentPadding:
                                    EdgeInsets.symmetric(horizontal: 8)),
                            keyboardType: TextInputType.number,
                            controller: roundOneFields[index],
                          ),
                        ),
                      ],
                    ),
                    physics: const BouncingScrollPhysics(),
                  ),
                ),
                const SizedBox(
                  width: 24,
                ),
                Expanded(
                  child: ListView.builder(
                    itemCount: 20,
                    shrinkWrap: true,
                    physics: const BouncingScrollPhysics(),
                    itemBuilder: (context, index) => Row(
                      children: <Widget>[
                        Text("R2 Target ${index + 1}:"),
                        Flexible(
                          child: TextField(
                            decoration: const InputDecoration(
                                contentPadding:
                                    EdgeInsets.symmetric(horizontal: 8)),
                            keyboardType: TextInputType.number,
                            controller: roundTwoFields[index],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                ElevatedButton(
                  child: const Text("Add"),
                  onPressed: () {
                    setState(() {
                      roundOneFields.forEach((e) =>
                          roundOneResult = roundOneResult + int.parse(e.text));
                      roundTwoFields.forEach((e) => roundTwoResult =
                          roundTwoResult + int.parse(e.text.toString()));
                    });
                  },
                )
              ],
            ),
            Text(
              "Result \n Round 1: $roundOneResult and Round 2: $roundTwoResult Total = ${roundOneResult + roundTwoResult}",
              textAlign: TextAlign.center,
              style: const TextStyle(
                fontSize: 30,
              ),
            ),
          ],
        ),
      ),
    );
  }
}





Navbar.dart-code:
import 'package:flutter/material.dart';
import 'package:tca/first_page.dart';

class Navbar extends StatelessWidget {
  const Navbar({super.key});

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        children: [
          UserAccountsDrawerHeader(
              accountName: const Text('TCB'),
              accountEmail: const Text('[email protected]'),
              currentAccountPicture: CircleAvatar(
                  child:
                      ClipOval(child: Image.asset('lib/images/tcbicon.jpg'))),
              decoration: BoxDecoration(
                color: Colors.orangeAccent,
              )),
          ListTile(
              leading: Icon(Icons.file_upload),
              title: Text('Rules'),
              onTap: () =>
                  Navigator.of(context).pushNamed('lib/first_page.dart')),
          ListTile(
            leading: Icon(Icons.card_membership),
            title: Text('Member NO'),
            onTap: () => print('Member NO'),
          ),
          ListTile(
            leading: Icon(Icons.file_upload),
            title: Text('Rules'),
            onTap: () => print('Rules'),
          ),
          ListTile(
            leading: Icon(Icons.file_upload),
            title: Text('Rules'),
            onTap: () => print('Rules'),
          ),
          ListTile(
            leading: Icon(Icons.file_upload),
            title: Text('Rules'),
            onTap: () => print('Rules'),
          ),
          ListTile(
            leading: Icon(Icons.file_upload),
            title: Text('Rules'),
            onTap: () => print('Rules'),
          ),
          ListTile(
            leading: Icon(Icons.file_upload),
            title: Text('Rules'),
            onTap: () => print('Rules'),
          ),
          ListTile(
            leading: Icon(Icons.file_upload),
            title: Text('Rules'),
            onTap: () => print('Rules'),
          ),
        ],
      ),
    );
  }
}

I tried these commands but nothing happens do i need something more on the main.dart page like a class or defining code that reconises the page its trying to route.

          ListTile(
              leading: Icon(Icons.file_upload),
              title: Text('Rules'),
              onTap: () =>
                  Navigator.of(context).pushNamed('lib/first_page.dart')),



But nothing happens 

I also put in at the top of the page help find the new page:
import 'package:tca/first_page.dart';
 Error was: 
Unused import: 'package:tca/main.dart'.
Try removing the import directive.







I also looked at this online not sure how to imlement it sorry:
  Widget _buildTiles(BuildContext context, Entry root) {
  ....
          child: ListTile(
            leading: Image.asset(root.imageUrl),
            title: Text(root.title),
            onTap: () => Navigator.of(context).pushNamed(root.routeName),
          ),
  ....
          children: root.children.map((e) => _buildTiles(context, e)).toList(),
  ....

  @override
  Widget build(BuildContext context) {
    return _buildTiles(context, entry);
  }

Solution

  • I found this worked my mistate was the named class "addtwonumbers"was the conector thanks all ListTile( leading: Icon(Icons.file_upload), title: Text('Rules'), onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context) => AddTwoNumbers()))),