在父级窗口小部件(MediaQuery.of(context).padding.top
中调用ProductsOverviewScreen
时,返回的值符合预期-24。但是从父级的嵌套窗口小部件(ProductsGrid
)调用相同属性时我们所说的值始终为0。这是正常现象吗?
products_overview_screen.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../widgets/app_drawer.dart';
import '../screens/cart_screen.dart';
import '../widgets/products_grid.dart';
import '../widgets/badge.dart';
import '../providers/cart.dart';
import '../providers/products_provider.dart';
enum FilterOptions {
showAll,
onlyFavorites,
}
class ProductsOverviewScreen extends StatefulWidget {
@override
_ProductsOverviewScreenState createState() => _ProductsOverviewScreenState();
}
class _ProductsOverviewScreenState extends State<ProductsOverviewScreen> {
bool _showFavorites = false;
bool _isLoading = false;
bool _isInit = false;
@override
void initState() {
// Future.delayed(Duration(seconds: 1), () {
// Provider.of<Products>(context).fetchAndSetProducts();
// });
super.initState();
}
@override
void didChangeDependencies() {
if (!_isInit) {
setState(() {
_isLoading = true;
});
Provider.of<Products>(context).fetchAndSetProducts().then((_) {
setState(() {
_isLoading = false;
});
});
}
_isInit = true;
super.didChangeDependencies();
}
Future<void> refreshProducts(BuildContext context) async {
await Provider.of<Products>(context, listen: false).fetchAndSetProducts();
}
@override
Widget build(BuildContext context) {
final scaffoldKey = GlobalKey();
final appBar = AppBar(
title: Text('My Shop'),
actions: <Widget>[
Consumer<Cart>(
builder: (ctx, cart, child) => Badge(
child: child,
value: cart.length.toString(),
),
child: IconButton(
onPressed: () {
final scaffoldState = scaffoldKey.currentState as ScaffoldState;
scaffoldState.hideCurrentSnackBar();
Navigator.of(context).pushNamed(CartScreen.routeName);
},
icon: Icon(
Icons.shopping_cart,
color: Theme.of(context).accentColor,
),
),
),
PopupMenuButton(
onSelected: (FilterOptions selectedValue) {
setState(() {
if (selectedValue == FilterOptions.onlyFavorites) {
_showFavorites = true;
} else {
_showFavorites = false;
}
});
},
icon: Icon(Icons.more_vert),
itemBuilder: (_) => [
PopupMenuItem(
child: Text('Only Favorites'),
value: FilterOptions.onlyFavorites),
PopupMenuItem(
child: Text('Show All'),
value: FilterOptions.showAll,
),
],
),
],
);
return Scaffold(
key: scaffoldKey,
appBar: appBar,
drawer: AppDrawer(),
body: _isLoading
? Center(child: CircularProgressIndicator())
: RefreshIndicator(
onRefresh: () => refreshProducts(context),
child: ProductsGrid(_showFavorites, appBar.preferredSize.height),
),
);
}
}
//56
products_grid.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/products_provider.dart';
import '../widgets/product_item.dart';
class ProductsGrid extends StatelessWidget {
final bool _showFavorites;
final double _appBarHeight;
ProductsGrid(this._showFavorites, this._appBarHeight);
@override
Widget build(BuildContext context) {
final productsData = Provider.of<Products>(context);
final products =
_showFavorites ? productsData.favoriteItems : productsData.items;
final mediaQuery = MediaQuery.of(context);
return productsData.items.isEmpty
? SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
child: Container(
width: mediaQuery.size.width,
height: mediaQuery.size.height -
mediaQuery.padding.top -
_appBarHeight,
// child: Center(
// child: Text(
// 'There are no products.',
// style: TextStyle(
// color: Colors.grey,
// fontSize: 16,
// ),
// ),
// ),
),
)
: GridView.builder(
padding: const EdgeInsets.all(15),
itemCount: products.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 3 / 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
),
itemBuilder: (ctx, index) => ChangeNotifierProvider.value(
value: products[index],
child: ProductItem(),
),
);
}
}
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
您可以使用MediaQueryData.fromWindow(window).padding.top
代码段
import 'dart:ui';
...
final statusbarHeight2 = MediaQueryData.fromWindow(window).padding.top;
工作演示
完整代码
import 'package:flutter/material.dart';
import 'dart:ui';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: ProductsOverviewScreen(title: 'Flutter Demo Home Page'),
);
}
}
class ProductsOverviewScreen extends StatefulWidget {
ProductsOverviewScreen({Key key, this.title}) : super(key: key);
final String title;
@override
_ProductsOverviewScreenState createState() => _ProductsOverviewScreenState();
}
class _ProductsOverviewScreenState extends State<ProductsOverviewScreen> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
final statusbarHeight1 = MediaQueryData.fromWindow(window).padding.top;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ProductsGrid(),
Text(
'$statusbarHeight1',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class ProductsGrid extends StatelessWidget {
const ProductsGrid({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final statusbarHeight2 = MediaQueryData.fromWindow(window).padding.top;
return Text(
'$statusbarHeight2',
);
}
}