我知道有一吨这样的问题,但我是全新的Flutter,我还没有找到一个职位,适合我的需要,或者也许我只是想错了。
在使用Flutter进行开发时,您可能已经看到几乎每个屏幕都需要将数据从一个屏幕传递到另一个屏幕,并且可能还需要从另一个屏幕接收数据。
**家. dart **
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'applicationbar.dart';
import 'navigationdrawer.dart';
import 'bluetooth.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
String _string = '0m'; //so if _isFeetChecked is true in settings.dart this is '0ft'
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[900],
appBar: ApplicationBar(title: 'Luggage Follower'),
drawer: NavigationDrawer(),
body:Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 10,horizontal: 0),
child: Text('good Status', style: Theme.of(context).textTheme.body2),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 10,horizontal: 0),
child: Text('Paired', style: Theme.of(context).textTheme.body1),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 20,horizontal: 0),
child: Divider(height: 3.0, color: Colors.pinkAccent, indent: 150, endIndent: 150),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 10,horizontal: 0),
child: Text('Distance to Luggage', style: Theme.of(context).textTheme.body2),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 10,horizontal: 0),
child: Text(_string, style: Theme.of(context).textTheme.body1), //dynamic text here
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 10,horizontal: 0),
child: LuggageFollow(),
),
],
),
);
}
}
设置.省道
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
import 'applicationbar.dart';
import 'navigationdrawer.dart';
class SettingsData {
bool feet;
bool metres;
SettingsData({this.feet, this.metres});
}
class Settings extends StatefulWidget {
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
static bool _isFeetChecked = false;
static bool _isMetersChecked = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: ApplicationBar(title: 'Settings'),
drawer: NavigationDrawer(),
body: Column(
//crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0,horizontal: 20),
child: Text('Distance Units')
),
Divider(
height: 3.0,
color: Colors.pink,
),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0,horizontal: 0),
child: CheckboxListTile(
title: Text('Distance in feet', style: Theme.of(context).textTheme.body1),
value: _isFeetChecked,
onChanged: (bool value) {
setState(() {
_isFeetChecked = value; _isMetersChecked = !value;});
},
checkColor: Colors.white,
activeColor: Colors.pink,
subtitle: Text('1 foot ~ 0.3 metres',style: Theme.of(context).textTheme.display2),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0,horizontal: 0),
child: CheckboxListTile(
title: Text('Distance in metres', style: Theme.of(context).textTheme.body1),
value: _isMetersChecked,
onChanged: (bool value) {
setState(() { _isMetersChecked = value; _isFeetChecked = !value;updateData();});
},
checkColor: Colors.white,
activeColor: Colors.pink,
subtitle: Text('1 metre ~ 3 feet',style: Theme.of(context).textTheme.display2),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 30.0,horizontal: 20),
child: Text('Notifications')
),
Divider(
height: 3.0,
color: Colors.pink,
),
),
),
],
),
);
}
}
3条答案
按热度按时间mzmfm0qo1#
在setting.dart文件中声明Function,并在Home.dart文件中创建Function及其实现,然后传递给setting.dart构造函数,并根据需要从setting.dart页面调用函数以取回传递数据。
qco9c6ql2#
建议使用flutter团队推荐的
go_router
并参考:扑动:go_router如何将多个参数传递给其他屏幕?
4bbkushb3#
所以,如果你想从一个类到另一个类访问数据,你只需要在类构造函数中传递变量,你就可以在另一个类中使用它。
例如:
你可以像这样使用,在另一个类中你只需要传递变量。