Home > Mobile >  In the Flutter application it dosent move to next page on button click
In the Flutter application it dosent move to next page on button click

Time:12-27

I am building a Flutter application. But it doesn't go from one page to another on a button click.

The Flutter application cannot move to next page on button click. The function is not working.

The main page in mentioned below.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/insert.dart';

void main()
{
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        backgroundColor: Colors.green,
      ),
      home: HomeScreen(),
    );
    
  }
}
class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.yellow,
      appBar: AppBar(
        backgroundColor: Colors.white,
      ),
      body: SafeArea(
        
        child: Padding(
          padding: const EdgeInsets.only(
            left: 20,
            right: 20,
          ),
          child: Center(
            child: Column(
              children: [
                ElevatedButton(onPressed: ()
                {
                  insert_data();
                  print('hii');
                },
                 child: Text('insert')),
                ElevatedButton(onPressed: (){}, child: Text('view')),
                ElevatedButton(onPressed: (){}, child: Text('remove')),
                ElevatedButton(onPressed: (){}, child: Text('delete')),
                
              ],
            ),
          ),
        ),
      ),
    );
    
  }
}

The insert() page is mentioned below. It doesn't go to next from one page to another on button click.

import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';

class insert_data extends StatelessWidget {
   insert_data({Key? key}) : super(key: key);

  final _textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(child: 
      Padding(
        padding: EdgeInsets.all(20),
        child: Container(
          color: Colors.green,
          child: Column(
            children: [
              TextField(
                controller: _textController,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: 'id'
                ),
              )
              
            ],
          ),
        ),
      ),
      ),
    );
    
  }
}

I am building a Flutter application. But it doesn't go from one page to another on a button click.

The insert() page doesn't go to next from one page to another on button click.

CodePudding user response:

Because you actually have to push the new widget as route. Try this out in elevated button

  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => insert_data()),
  );

CodePudding user response:

You should devide your pages in different files and use the built-in navigator from Flutter. It will basically "add a page" to your heap. If you need to go back to the home page you either click on the default back arrow or you can use Navigator.pop(context, true); function.

Here is an example:

import 'package:flutter_application_1/another_page.dart'; 

[...]

onPressed: () { 
  setState(() {
    Navigator.of(context).push(
      MaterialPageRoute(builder: (context) => AnotherPage()),
    );
  });
}

Hopefully this helps.

CodePudding user response:

ElevatedButton(
   onPressed: () { Navigator.push(context,
     MaterialPageRoute(builder: (context) => const insert_data()),
     ); 
   }, 
   child: Text('insert'),
),
  • Related