- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想调用一个简单的 GridWiget
在我的主页里面
这是 MyHomePage
StatefulWidget
:
import './app_theme.dart';
import 'package:flutter/material.dart';
import './widgets/properties_grid.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
var _showOnlyFavorites = false;
AnimationController animationController;
bool multiple = true;
@override
void initState() {
animationController = AnimationController(
duration: const Duration(milliseconds: 2000), vsync: this);
super.initState();
}
Future<bool> getData() async {
await Future<dynamic>.delayed(const Duration(milliseconds: 0));
return true;
}
@override
void dispose() {
animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppTheme.white,
body: FutureBuilder<bool>(
future: getData(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
} else {
return Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
appBar(),
Expanded(
child: FutureBuilder<bool>(
future: getData(),
builder:
(BuildContext context, AsyncSnapshot<bool> snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
} else {
return PropertiesGrid(_showOnlyFavorites);
}
},
),
),
],
),
);
}
},
),
);
}
Widget appBar() {
return SizedBox(
height: AppBar().preferredSize.height,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8, left: 8),
child: Container(
width: AppBar().preferredSize.height - 8,
height: AppBar().preferredSize.height - 8,
),
),
Expanded(
child: Center(
child: Padding(
padding: const EdgeInsets.only(top: 4),
child:
Image.asset('assets/images/logo.png', fit: BoxFit.contain),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 8, right: 8),
child: Container(
width: AppBar().preferredSize.height - 8,
height: AppBar().preferredSize.height - 8,
color: Colors.white,
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius:
BorderRadius.circular(AppBar().preferredSize.height),
child: Icon(
multiple ? Icons.dashboard : Icons.view_agenda,
color: AppTheme.dark_grey,
),
onTap: () {
setState(() {
multiple = !multiple;
});
},
),
),
),
),
],
),
);
}
}
PropertiesGrid
StatelessWidget
:
import 'package:aradi_online_vtwo/providers/properties.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/properties.dart';
import './property_item.dart';
class PropertiesGrid extends StatelessWidget {
final bool showFavs;
PropertiesGrid(this.showFavs);
@override
Widget build(BuildContext context) {
final productsData = Provider.of<Properties>(context);
final products = showFavs ? productsData.favoriteItems : productsData.items;
return GridView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: products.length,
itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
value: products[i],
child: PropertyItem(
),
),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
childAspectRatio: 3 / 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
),
);
}
}
provider
数据如下所示:
import 'package:flutter/material.dart';
import './property.dart';
class Properties with ChangeNotifier {
List<Property> _items = [
Property(
id: 'p1',
purpose: '',
title: 'House Title sdfsdffdgdfgdfs',
),
Property(
id: 'p3',
purpose: '',
title: 'land title',
),
];
List<Property> get items {
return [..._items];
}
List<Property> get favoriteItems {
return _items.where((propItem) => propItem.isFavorite).toList();
}
Property findById(String id) {
return _items.firstWhere((prop) => prop.id == id);
}
void addProperty(Property property) {
final newProperty = Property(
id: DateTime.now().toString(),
purpose: property.purpose,
title: property.title,
);
_items.add(newProperty);
notifyListeners();
}
void updateProperty(String id, Property newProperty) {
final propIndex = _items.lastIndexWhere((prop) => prop.id == id);
if (propIndex >= 0) {
_items[propIndex] = newProperty;
notifyListeners();
} else {
print('...');
}
}
void deleteProperty(String id) {
_items.removeWhere((prop) => prop.id == id);
notifyListeners();
}
}
GridItem
是下面的代码:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/property.dart';
class PropertyItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
final property = Provider.of<Property>(context, listen: false);
// final cart = Provider.of<Cart>(context, listen: false);
return InkWell(
onTap: () => {},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
elevation: 7,
margin: EdgeInsets.all(2),
child: Stack(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15),
topRight: Radius.circular(15),
),
// color: Colors.transparent,
child: Image.asset(
property.image,
fit: BoxFit.fill,
),
),
Positioned(
top: 8,
right: 8,
child: Consumer<Property>(
builder: (ctx, property, _) => IconButton(
icon: Icon(
property.isFavorite ? Icons.favorite : Icons.favorite_border,
),
color: Colors.red,
onPressed: () {
property.toggleFavoriteStatus();
},
),
),
),
Positioned(
right: 20,
top: 100,
child: Container(
width: 300,
color: Colors.black54,
padding: EdgeInsets.symmetric(
vertical: 5,
horizontal: 20,
),
child: Text(
property.title,
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
softWrap: true,
overflow: TextOverflow.fade,
),
),
)
],
),
),
);
}
}
ModelProvider
:
import 'package:flutter/foundation.dart';
class Property with ChangeNotifier {
final String id;
final String purpose;
final String title;
bool isFavorite;
Property({
@required this.id,
@required this.purpose,
@required this.title,
});
void toggleFavoriteStatus() {
isFavorite = !isFavorite;
notifyListeners();
}
}
return PropertiesGrid(_showOnlyFavorites);
最佳答案
您必须添加一个更改通知程序提供程序作为父级,以便从后代小部件访问属性模型。
ChangeNotifierProvider(
create: (context) => Property(),
child: PropertiesGrid(_showOnlyFavorites),
),
Provider.of<Property>(context, listen: false);
访问属性更改通知程序模型或使用
Consumer
.
关于flutter - 错误 : Could not find the correct Provider above this widget Flutter [PropertiesGrid],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62116714/
我正在构建一个基本的 Java 应用程序来将一些文件加载到 mysql 数据库中。我能够毫无问题地加载文件并填充我的表。然而,在与审查我的代码的人交谈后,我显然没有正确关闭我的连接并浪费资源。我在
我正在用 C++ 构建一个库(主要是为了好玩),我已经研究了一段时间(多年,哈哈,这只是一种爱好) 我最近将一些基础(阅读、库依赖)切换到了另一个库。不幸的是,该库根本不关心“const-correc
如果我绘制单个平面,则纹理坐标会正确映射。 (4 Verts, 4 TC, 6 Indices(2 polys)) 即使它被 segmentation ,(9 Verts, 9 TC, 27 Indi
我正在从文件系统上的 pfx 加载 x509 证书 new X509Certificate2(@"Resources\certificate.pfx", "Password123") 在本地,这工作正
我知道这个问题被问了一遍又一遍。我确实喜欢在与此相关的所有问题中提出建议,并且我在 this question that I put 中做了 BalusC 的操作。 告诉我,我还没有成功。 所以网络应
简而言之,我正在制作一个预订应用程序。预订 ID 需要从 10000 开始,并在每次新预订时增加 1。 我已经开始编写生成此预订编号的方法。我正在努力的是: 第一次运行时,不会有预订编号,所以我不能简
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我查看了 php.net 上的 switch 文档,据我所知,它检查了 switch 和 case 中的变量之间的相等性比较。但是,以下代码似乎适用于所有可能的值(int、null、数组、其他): $
我正在为以多种方式创建和作用于实体的服务编写 JUnit 测试。我希望我的测试能够尝试多种不同的事件组合。我有这样的东西: test1() { /** create entity **/ /** as
关于如何在 Delphi 程序中定义 ShortCut 的示例有很多,但是它们归结为两种不同的方式: 将任意 scCtrl、scShift 和 scAlt 常量添加到键的 Ord() 使用 Menus
我正在尝试学习如何在 Javascript 中创建类以及如何执行对象继承。我已经遵循了一些教程,但我不确定我的代码是否正确。 我是否正确创建了公共(public)函数和属性?如果不是,我应该改变什么?
任何写过 javascript/jquery 的人都知道,有很多不同的方法可以做同样的事情。我目前正在尝试通过表单提交和 AJAX 请求向服务器发送一些数据,我想知道执行此操作的“正确”方法是什么。
一条 200 字节的消息有一个随机字节损坏。 修复损坏字节的最有效方法是什么? A Hamming(255,247)代码有 8 个字节的开销,但实现起来很简单。 Reed-Solomon error
在C++中,将n -bit整数移位n是未定义的行为: std::uint64_t v = 1; v = v = 64 ? 0 : v > 6; uint64_t mask = (!!temp)
我对 MouseEvents 和 MouseListeners 非常陌生,最近我问了一个关于创建篮球投篮图表的问题。到目前为止我所拥有的是这个 import javax.swing.*; im
http://www.codechef.com/OCT14/problems/PRLADDU 这是当前的运行比赛。 我不想要它的答案,只是让我知道我的方法是否正确。 我遵循的方法是按交换方式添加人和恐
我正在用 Python(在 Linux 系统上的 Apache 服务器上)编写一个需要连接到 Postgres 数据库的 Web 应用程序。因此,它需要数据库服务器的有效密码。在我的 Python 文
我对 JS 和 HTML5 有点陌生。我正在创建一个简单的测验,只是为了好玩。我知道需要使每个问题都能够独立于其他问题而被标记为“正确”。我如何通过 JS,甚至 CSS/HTML5 来做到这一点?我感
我正在编写一个涉及大量 XML 操作的 HTML5 应用程序,其中部分操作涉及比较两个不同 XML 元素的版本。 我需要的是每个 Element、Attr 和 TextNode(所有这些都继承自 No
我正在使用 IBM RAD 开发一些 JPA 实体,并从中开发相应的 JPA Manager Bean。管理器 bean(由 RAD 生成)具有以下成员: @PersistenceUnit priva
我是一名优秀的程序员,十分优秀!