Skip to content

Instantly share code, notes, and snippets.

@thenbe
Created April 17, 2021 10:13
Show Gist options
  • Save thenbe/b920c76e7c8be112acf4bdf07cdd30cb to your computer and use it in GitHub Desktop.
Save thenbe/b920c76e7c8be112acf4bdf07cdd30cb to your computer and use it in GitHub Desktop.
How to Access a Variable that is Associated with other Widget in Flutter?
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
String _currentEmoji = 'my initial emoji';
void _setEmoji() {
// My Random Code Goes Here. (Never Mind)
setState(() {
_currentEmoji = 'new emoji';
});
}
@override
Widget build(BuildContext context) {
return Column(children: [
ElevatedButton(
onPressed: _setEmoji,
child: Text('press me'),
),
Text(_currentEmoji),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment