在Flutter中,可以使用Flutter Reorderable ListView库来实现在颤动中向多个FloatingActionButton添加onPressed的功能。下面是一个完整且全面的答案:
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_reorderable_list/flutter_reorderable_list.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Reorderable FloatingActionButton',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Reorderable FloatingActionButton'),
),
body: MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> _fabList = [
'FAB 1',
'FAB 2',
'FAB 3',
];
Widget _buildFloatingActionButton(int index) {
return FloatingActionButton(
onPressed: () {
// TODO: Add onPressed action for each FloatingActionButton
},
child: Text(_fabList[index]),
);
}
void _onReorder(int oldIndex, int newIndex) {
setState(() {
if (oldIndex < newIndex) {
newIndex -= 1;
}
final String item = _fabList.removeAt(oldIndex);
_fabList.insert(newIndex, item);
});
}
@override
Widget build(BuildContext context) {
return ReorderableListView(
onReorder: _onReorder,
children: List.generate(_fabList.length, (index) {
return ListTile(
key: Key('$index'),
title: _buildFloatingActionButton(index),
);
}),
);
}
}
希望以上回答能够满足您的要求,如果还有其他问题,请随时提问。
领取专属 10元无门槛券
手把手带您无忧上云