开发心得

模拟器bug

E/ample.ordermea( 3265): Failed to send jdwp-handshake response.: Broken pipe

关了模拟器再连接

主题颜色

1
backgroundColor: Theme.of(context).colorScheme.inversePrimary,

这个可以使用之前主题的颜色

setsate的bug

1
2
3
4
5
6
void fun(){
setState(){
_cout++;
print(_cout);
}
}

上面这个是错的

1
2
3
4
5
void fun(){
setState(() {
_cout++;
});
}

这个才是对的

究其根本,函数原型

1
void setState(VoidCallback fn)

const

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Category {
final String id;
final String title;
final Color color;
Category({this.color = Colors.orange, required this.id, required this.title});
}

const List<Category> Dumm = const [
Category(id: "c1", title: "itali", color: Colors.blue),
Category(id: "c2", title: "Fruits", color: Colors.red),
Category(id: "c3", title: "Vegetables", color: Colors.green),
Category(id: "c4", title: "Desserts", color: Colors.yellow),
Category(id: "c5", title: "Beverages", color: Colors.purple),
];

上面这个会报错

要修改为

1
2
3
4
5
6
class Category {
final String id;
final String title;
final Color color;
const Category({this.color = Colors.orange, required this.id, required this.title});
}

加了const以后,后续不会被重构

下面看一个在gridview中使用padding并用const修饰的

1
2
body: GridView(
padding: const EdgeInsets.all(25),

我们页面之间的跳转,其实页面存储在一个堆栈里面,我们看到的页面就是在栈顶的,navigator可以实现push和pop

builder 的作用就是自动传入

查看原型

typedef WidgetBuilder = Widget Function(BuildContext context);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
InkWell(
onTap: () => selectCategory(context),
splashColor: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(15),
child: Container(
padding: EdgeInsets.all(15),
child: Text(title),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [color.withOpacity(0.7), color],
begin: Alignment.topLeft,
end: Alignment.bottomRight),
borderRadius: BorderRadius.circular(15)),
),
);
}
1
2
3
4
5
void selectCategory(BuildContext ctx) {
Navigator.of(ctx).push(MaterialPageRoute(builder: (_) {
return CategoryMeal(id,title,color);
}));
}