flutter 如何通过从另一个类调用来从一个类获取控制器偏移量

7hiiyaii  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(127)

我在这里有两节课,
第一节课:

BottomNavigationBar(
        currentIndex: currentIndex,
        items: [
          BottomNavigationBarItem(
              icon: const Icon(Icons.home),
              label: 'Another Screen'),
          BottomNavigationBarItem(
              icon:  Icon(Icons.now_widgets_rounded),
              label: 'Login Screen'),
        ],
        onTap: (index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),

字符串
另一类:

class AnotherScreen extends StatefulWidget {
const AnotherScreen({Key? key}) : super(key: key);

@override
State<AnotherScreen> createState() => AnotherScreenState();
}

class AnotherScreenState extends State<AnotherScreen> {
ScrollController controller = ScrollController();

List<Color> colors = [
Colors.black12,
Colors.red,
];

@override
Widget build(BuildContext context) {
 return Scaffold(
  backgroundColor: Colors.white,
  body: ListView.builder(
    itemCount: 2,
    controller: controller,
    itemBuilder: (___, index) => Container(
      color: colors[index],
      child: Text(
        index.toString(),
        ),
      ),
    ),
  ),
);


我想要的是,当我改变了第一个类中标签的索引时,我想在第一个类中的onTap方法中获取另一个类的控制器,然后再将当前索引更改为新索引。

kr98yfug

kr98yfug1#

在AnotherScreenState类中为ScrollController创建Riverpod提供程序:

import 'package:flutter_riverpod/flutter_riverpod.dart';

final scrollControllerProvider = Provider<ScrollController>((ref) {
  return ScrollController();
});

字符串
在AnotherScreenState类中,使用scrollControllerProvider:

class AnotherScreenState extends ConsumerStatefulWidget {
  @override
  _AnotherScreenStateState createState() => _AnotherScreenStateState();
}

class _AnotherScreenStateState extends ConsumerState<AnotherScreenState> {
  List<Color> colors = [
    Colors.black12,
    Colors.red,
  ];

  @override
  Widget build(BuildContext context) {
    final scrollController = ref.read(scrollControllerProvider);

    return Scaffold(
      backgroundColor: Colors.white,
      body: ListView.builder(
        itemCount: 2,
        controller: scrollController,
        itemBuilder: (___, index) => Container(
          color: colors[index],
          child: Text(
            index.toString(),
          ),
        ),
      ),
    );
  }
}


修改您的BottomNavigationBar以使用Riverpod:

Consumer(
  builder: (_, ref, __) {
    return BottomNavigationBar(
            currentIndex: currentIndex,
            items: [
              BottomNavigationBarItem(
                  icon: const Icon(Icons.home),
                  label: 'Another Screen'),
              BottomNavigationBarItem(
                  icon:  Icon(Icons.now_widgets_rounded),
                  label: 'Login Screen'),
            ],
            onTap: (index) {
              final scrollController = ref.read(scrollControllerProvider); // You can use the controller now

              setState(() {
                currentIndex = index;
              });
            },
          );
  }
),


您可以随意调整解决方案以适应您所选择的状态管理方法。关键概念是一样的:在更改选项卡索引以执行任何必要的操作之前,访问ScrollController。

相关问题