#flutter #flutter-pageview
#flutter #flutter-просмотр страницы
Вопрос:
На странице каркаса с примерно следующей структурой
@override
Widget build(BuildContext context){
body: PageView(
controller: _controller;
children: <Widget>[Page1(), Page2(), Page3()];
);
bottomNavigationBar: BottomNavBar(
onItemSelected: (index) => _controller.animateToPage()
)
}
есть два способа перейти от Page2()
к Page1()
:
- Проведите пальцем по экрану слева направо
- Коснитесь значка Page1() на
bottomNavigationBar
, и, таким образом, вызов_controller.animateToPage(0)
Проблема в том, как я могу определить, изменена ли страница с помощью жеста или animateToPage()
функции прокрутки?
Спасибо.
Комментарии:
1.
PageView
имеет свойствоonPageChanged
. Этого будет достаточно?2. Как поведение перетаскивания
animateToPage()
onPageChanged(index)
, так и запуск функции, их трудно различить.3.
animateToPage()
вызывается вами в коде, другого нет, вы поняли мою точку зрения, приведенный ниже ответ объясняет это
Ответ №1:
Возможно, вы можете добавить флаг, чтобы установить animateToPage
, продолжается ли он или нет.
Пример:
import 'package:flutter/material.dart';
void main() => runApp(Root());
class Root extends StatelessWidget {
@override
Widget build(BuildContext context) {
final PageController controller = PageController();
bool isAnimateToPage = false;
controller.addListener(() {
print('LISTENER isAnimateToPage = $isAnimateToPage');
});
return MaterialApp(
home: Scaffold(
body: Column(
children: <Widget>[
Expanded(
child: PageView(
controller: controller,
onPageChanged: (int page) {
print(
'ONPAGECHANGED isAnimateToPage = $isAnimateToPage ; page = $page');
},
children: <Widget>[
const Center(child: Text('page 1')),
const Center(child: Text('page 2')),
const Center(child: Text('page 3')),
],
),
),
FlatButton(
onPressed: () async {
isAnimateToPage = true;
await controller.animateToPage(
controller.page.toInt() 1,
duration: const Duration(seconds: 1),
curve: Curves.easeIn,
);
isAnimateToPage = false;
},
child: const Text('Next'),
),
],
),
),
);
}
}
Комментарии:
1. @StevenLuo Рад, что я смог помочь. Удачи!