我正在构建一个从API获取数据的Flutter应用。
如何将快照数据从Flutter屏幕传递到小部件构建器上下文之外的浮动操作按钮?
然后,浮动按钮会将数据传递给PDF构建器函数。
这是我的代码。
@override 小部件build(BuildContext context){ 返回脚手架( appBar:AppBar( backgroundColor:Colors.blue, automaticImplyLeading:否, centerTitle:是的,
title: Text(widget.bill.billNumber.toString(),
style: TextStyle(color: Colors.white)
),
leading: IconButton(icon:Icon(Icons.arrow_back),
onPressed:() =>
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) =>
BillsScreen(table: widget.table)), (Route<dynamic> route) => false)
)
),
body: SafeArea(
child: Container(
child:
StreamBuilder<SalesResponse>(
stream: salesBloc.subject.stream,
builder: (context, AsyncSnapshot<SalesResponse> snapshot) {
if (snapshot.hasData) {
return _buildSalesListWidget(snapshot.data);
}
else if (snapshot.hasError) {
return
_buildErrorWidget(snapshot.error);
}
},
)
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FloatingActionButton(
heroTag: 1,
backgroundColor: Colors.yellow,
onPressed: () {
Printing.layoutPdf(onLayout:(format)=>
buildReceipt(widget.table,
widget.bill,
data.sales));
},
child: Icon(FontAwesomeIcons.print),
),
],
),
)
);
}
Widget _buildSalesListWidget(SalesResponse data) {
return ListView.builder(
itemCount: data.sales.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
data.sales[index].item,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20,
),
),
subtitle: Text('qty' + ' ' + data.sales[index].qty.toString() + ', ' + 'price' + ' ' + data.sales[index].price.toStringAsFixed(2)),
leading: Icon(
FontAwesomeIcons.beer,
color: Colors.blue[500],
),
onTap: () {},
);
},
);
}
}
答案 0 :(得分:1)
class Screen extends StatefulWidget {
@override
_ScreenState createState() => _ScreenState();
}
class _ScreenState extends State<Screen> {
String snapshotData = "";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder(
future: Future.delayed(const Duration(seconds: 1), () => "data to pass"),
builder: (context, AsyncSnapshot<String> snapshot) {
if(!snapshot.hasData) {
return Text("NO Data");
} else {
snapshotData = snapshot.data;
return Text("Data loaded");
}
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => print(snapshotData),
),
);
}
}