Skip to content

Instantly share code, notes, and snippets.

@hongsw
Created May 17, 2023 03:05
Show Gist options
  • Save hongsw/9c45cca35b47a4070f7e9abcbe44c55e to your computer and use it in GitHub Desktop.
Save hongsw/9c45cca35b47a4070f7e9abcbe44c55e to your computer and use it in GitHub Desktop.
cylindrical-dart-2948

cylindrical-dart-2948

Created with <3 with dartpad.dev.

import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Form Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FirstPage(),
);
}
}
class FirstPage extends StatefulWidget {
@override
_FirstPageState createState() => _FirstPageState();
}
class MyClass {
String myVariable = "Initial Value";
void _changeMyVariable(String newValue) {
myVariable = newValue;
print("myVariable : $myVariable");
}
}
class _FirstPageState extends State<FirstPage> {
MyClass myClass = MyClass();
// void noneFuntion (){}
void callSetState(){
// setState(noneFuntion);
setState((){
// action
});// ui update in setState()
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('My Variable: ${myClass.myVariable}'),
ElevatedButton(
child: Text('Go to Second Page'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(
changeMyVariable: myClass._changeMyVariable,
callFirstPageSetState: callSetState
),
),
);
},
),
],
),
),
);
}
}
class SecondPage extends StatelessWidget {
final Function changeMyVariable;
final Function callFirstPageSetState;
SecondPage({required this.changeMyVariable, required this.callFirstPageSetState}) : super();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Second Page'),
),
body: Center(
child: ElevatedButton(
child: const Text('Change My Variable'),
onPressed: () {
changeMyVariable("New Value");
callFirstPageSetState();
// setState(() {
// // ui update of first page
// });
Navigator.pop(context);
},
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment