我正在嘗試從歡迎頁面導航到螢屏選項卡,但由于上述錯誤而無法這樣做。
歡迎螢屏的一部分.dart
class WelcomePage extends StatelessWidget {
static const routeName = '/welcome-screen';
const WelcomePage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.black,
child: Stack(
children: [
Positioned.fill(
child: Opacity(
opacity: 0.3,
child: Image.asset('assets/images/of_main_bg.png', fit: BoxFit.cover),
),
),
ThemeButton(
label: "Get Started!",
labelColor: AppColors.MAIN_COLOR,
color: Colors.transparent,
highlight: AppColors.MAIN_COLOR.withOpacity(0.5),
borderColor: AppColors.MAIN_COLOR,
borderWidth: 4,
onPressed: () {Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return TabsScreen(**ERROR**#What should I write here);
},
),
main.dart 中的路由
initialRoute: '/', // default is '/'
routes: {
'/': (ctx) => WelcomePage(),
CategoryMealsScreen.routeName: (ctx) => CategoryMealsScreen(_availableMeals),
MealDetailScreen.routeName: (ctx) => MealDetailScreen(_toggleFavorite, _isMealFavorite),
FiltersScreen.routeName: (ctx) => FiltersScreen(_filters, _setFilters),
TabsScreen.routeName: (ctx) => TabsScreen(_favoriteMeals),
WelcomePage.routeName: (ctx) => WelcomePage(),
},
tabscreen.dart 中的類代碼
class TabsScreen extends StatefulWidget {
static const routeName = '/tabs-screen';
final List<Meal> favoriteMeals;
TabsScreen(this.favoriteMeals);
我應該如何從welcomepage 導航到tabs_screen 頁面?為了導航到相同的內容,我應該在welcome_screen.dart的那個錯誤位置寫什么。如果有另一種導航方法,請告訴。
uj5u.com熱心網友回復:
不要使用命名路由。
來自https://docs.flutter.dev/cookbook/navigation/named-routes
注意:命名路由不再推薦用于大多數應用程式。有關詳細資訊,請參閱導航概覽頁面中的限制。
uj5u.com熱心網友回復:
您的TabsScreen建構式正在使用位置引數。你可以在那里傳遞空串列([])。但是對我來說,使用可空資料制作名稱建構式似乎更好。
class TabsScreen extends StatefulWidget {
static const routeName = '/tabs-screen';
final List<Meal>? favoriteMeals;
TabsScreen({ this.favoriteMeals});
現在它是可選的,您可以通過 list likeTabsScreen(favoriteMeals: yourData)并在不想通過時跳過TabsScreen()。
更多關于using-constructors
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/527187.html
標籤:扑镖
