我正在嘗試創建一個帶有頁面滑塊輪播和進度條的頁面。當輪播移動到另一個頁面時,我希望進度條從一個值更新到另一個帶有影片的值。我試過了,LinearProgressIndicator但我不知道如何將影片從舊值設定為新值。這就是我所擁有的
LinearProgressIndicator(
minHeight: 2,
value: currentPageIndex/(pages.length - 1)
)
currentPageIndex使用setState()外部方法更新。
有什么辦法嗎?提前致謝。
uj5u.com熱心網友回復:
您應該setState(){}在頁面被滑動和currentPageIndex更新時使用。
uj5u.com熱心網友回復:
您可以嘗試使用這種方法
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
/// AnimationControllers can be created with `vsync: this` because of TickerProviderStateMixin.
class _MyStatefulWidgetState extends State<MyStatefulWidget>
with TickerProviderStateMixin {
late AnimationController controller;
late Animation _animation;
int count = 0;
@override
void initState() {
controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 5),
)..addListener(() {
if (_animation.value == count/5) {
controller.stop(canceled: false);
}
setState(() {});
});
// ignore: avoid_single_cascade_in_expression_statements
controller.addStatusListener((AnimationStatus status){
});
_animation = Tween<double>(
begin: 0,
end: 1,
).animate(controller);
//controller.repe
//controller.forward();
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
const Text(
'Linear progress indicator with a fixed color',
style: TextStyle(fontSize: 20),
),
LinearProgressIndicator(
value: _animation.value,
semanticsLabel: 'Linear progress indicator',
),
ElevatedButton(
child: const Text(
'count',
style: TextStyle(fontSize: 20),
),
onPressed: (){
setState(() {
if (count > 5) return;
count ;
controller.forward(from:count/5);
});
}
)
],
),
),
);
}
}
從這里運行代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/366994.html
