flutter - How do i use flutter variables from main.dart in other screen.dart files in the same lib folder?
问题描述
I wanted to create an app that randomly generates my lucky number when i press floatingActionButton. I wanted to do it within 2 dart files.. let me show you the code in dart.main and first_screen.dart.
dart.main
import 'package:demo/app_screens/first_screen.dart';
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: MyFlutterApp()
));
class MyFlutterApp extends StatefulWidget {
@override
_MyFlutterAppState createState() => _MyFlutterAppState();
}
class _MyFlutterAppState extends State<MyFlutterApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.yellow,
centerTitle: true,
title: Text('My first App',style: TextStyle(color: Colors.black, fontSize: 25.0),),
),
body: FirstScreen(),
floatingActionButton: FloatingActionButton(
onPressed: (){
setState(() {
int thatNum = generateLuckyNumber();
});},
child: Icon(Icons.add),
),
);
}
}
and first_screen.dart in the lib/screens directory
import 'dart:math';
import 'package:flutter/material.dart';
class FirstScreen extends StatefulWidget{
@override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
@override
Widget build(BuildContext context) {
return Material(color: Colors.lightBlue,
child: Center(
child: Text('My lucky number is ${thatNum}',style: TextStyle(
fontSize: 28,color: Colors.black,backgroundColor:Colors.white)),
),
);
}
}
int generateLuckyNumber() {
var random= Random();
int luckyNumber= random.nextInt(10);
return luckyNumber;
}
I would like to use the varibale thatNum declared in main.dart file in the first_screen.dart file.. How do you do that?
解决方案
The easiest way to do this, pass the thatNum
in FirstScreen
constructor. Do the required changes given below
class _MyFlutterAppState extends State<MyFlutterApp> {
int thatNum; // <- declare thatNum in the class
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.yellow,
centerTitle: true,
title: Text('My first App',style: TextStyle(color: Colors.black, fontSize: 25.0),),
),
body: FirstScreen(thatNum:thatNum), // <-- pass thatNum in constructor
floatingActionButton: FloatingActionButton(
onPressed: (){
setState(() {
thatNum = generateLuckyNumber(); /* <- generateLuckyNumber and assign to thatNum */
});},
child: Icon(Icons.add),
),
);
}
}
In the FirstScreen
declare thatNum
class FirstScreen extends StatefulWidget{
final thatNum; // <- declare thatNum
FirstScreen({this.thatNum});
@override
_FirstScreenState createState() => _FirstScreenState();
}
In _FirstScreenState
check whether the widget.thatNum
is null or not. If it is null assign Loading text or show thatNum
if not widget.thatNum
is not null.
class _FirstScreenState extends State<FirstScreen> {
@override
Widget build(BuildContext context) {
return Material(color: Colors.lightBlue,
child: Center(
child: Text('My lucky number is ${widget.thatNum??"Loading"}',style: TextStyle(
fontSize: 28,color: Colors.black,backgroundColor:Colors.white)),
),
);
}
}
Note: You can also use state management solution, but the above solution solves the issue easily. Still, you can check state management solution here
推荐阅读
- java - 通过基于 JMS 网络的 FAA 航行通告?
- swift - CKQueryNotification.recordID:无法识别的选择器发送到实例
- swift - 如何在 swift UITests 中的 XCUIApplication 中设置暗模式?
- firebase - nativescript-vue / firebase.query 中的错误:ReferenceError:找不到变量:FIRDatabase
- android - 在 @Query 的引用部分使用变量
- windows - 对任何 set /p 值使用全局变量?
- text - 如何在记事本++中的开始和结束括号之间快速跳转?
- python - 我怎样才能刮掉其他标签?
- arrays - 将值导入 Excel 加载项中的数组的最有效方法
- python - 如何在 RL 中获取 Q 值 - DDQN