如何在下面这段代码中将 OrdeilistProvider 添加到 MultiProvider 中:class Tabs extends StatefulWidget { const Tabs({super.key}); @override State<Tabs> createState() => _TabsState(); } class _TabsState extends State<Tabs> { int _currentIndex = 0; final List<Widget> pages = [ HomePage( key: homePageKey, ), const OrderPage(), const ProfilePage() ]; @override Widget build(BuildContext context) { return Scaffold( body: IndexedStack( index: _currentIndex, children: pages, ), bottomNavigationBar: BottomNavigationBar( currentIndex: _currentIndex, type: BottomNavigationBarType.fixed, onTap: (index) { setState(() { _currentIndex = index; }); if (_currentIndex == 0) { homePageKey.currentState!.toMyLocation(); } }, items: const [ BottomNavigationBarItem(icon: Icon(Icons.home), label: ''), // BottomNavigationBarItem(icon: Icon(Icons.gas_meter), label: ''), BottomNavigationBarItem(icon: Icon(Icons.note_alt), label: ''), BottomNavigationBarItem(icon: Icon(Icons.person), label: ''), ], ), ); } }
时间: 2024-02-14 07:13:05 浏览: 59
可以使用 MultiProvider 包裹整个 MaterialApp,并将 OrdeilistProvider 添加到 providers 列表中,如下所示:
```dart
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => OrdeilistProvider()),
],
child: MaterialApp(
title: 'My App',
home: const Tabs(),
),
);
}
}
class Tabs extends StatefulWidget {
const Tabs({Key? key}) : super(key: key);
@override
State<Tabs> createState() => _TabsState();
}
class _TabsState extends State<Tabs> {
int _currentIndex = 0;
final List<Widget> pages = [
HomePage(
key: homePageKey,
),
const OrderPage(),
const ProfilePage()
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: pages,
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
onTap: (index) {
setState(() {
_currentIndex = index;
});
if (_currentIndex == 0) {
homePageKey.currentState!.toMyLocation();
}
},
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: ''),
// BottomNavigationBarItem(icon: Icon(Icons.gas_meter), label: ''),
BottomNavigationBarItem(icon: Icon(Icons.note_alt), label: ''),
BottomNavigationBarItem(icon: Icon(Icons.person), label: ''),
],
),
);
}
}
```
阅读全文