import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; // .yaml file provider: ^3.1.0+1 void main() { runApp(MaterialApp( title: 'Navigation Basics', home: FirstRoute(), )); } class FirstRoute extends StatelessWidget { @override Widget build(BuildContext context) { return ChangeNotifierProvider( builder: (context) => Data(), child: Scaffold( appBar: AppBar( title: Text('First Route'), ), body: Column( children: [ Container( width: 256, margin: const EdgeInsets.only(bottom: 8), child: TextField( decoration: InputDecoration( hintText: 'Enter user name', labelText: 'User Name'), onChanged: (String newUserName){ Provider.of(context).changePassword(newUserName); print(newUserName); }, ), ), Container( width: 256, margin: const EdgeInsets.only(bottom: 8), child: TextField( obscureText: true, decoration: InputDecoration( hintText: 'Enter password', labelText: 'Password'), onChanged: (String newPassword){ Provider.of(context).changePassword(newPassword); print(newPassword); }, ), ), Center( child: RaisedButton( child: Text('Open route'), onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => SecondRoute()), ); }, ), ), ], ), ), ); } } class SecondRoute extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Second Route"), ), body: Column( children: [ Center( child: RaisedButton( onPressed: () { Navigator.pop(context); }, child: Text('Go back!'), ), ), Text( Provider.of(context).userName, // Provider.of(context), style: TextStyle( color: Colors.blue, fontSize: 20.0 ), ), Text( Provider.of(context).password, // Provider.of(context), style: TextStyle( color: Colors.green, fontSize: 20.0 ), ), ], ), ); } }