flutter的go_router中如何优化下面代码:GoRoute path pageBuilder context state = const MaterialPagevoid child GuidePage GoRoute path home_page pageBuilder context state = c
可以将MaterialPage的创建代码抽离成一个方法,然后在每个GoRoute中调用该方法,避免重复代码。
例如:
MaterialPage
然后将GoRoute中的pageBuilder修改为:
pageBuilder: (context, state) => _buildMaterialPage(GuidePage()),
或
pageBuilder: (context, state) => _buildMaterialPage(HomePage()),
这样可以使代码更简洁、易读。
原文地址: https://www.cveoy.top/t/topic/bqE9 著作权归作者所有。请勿转载和采集!