I use Android Studio. I have a few Flutter widgets in one file. Is it a way to extract a widget into its own DART file very quickly, like a shortcut?
For example, how to extract HomePage from such a file?
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'my_chart.dart';
import 'my_schedule.dart';
import 'my_slider.dart';
void main() => runApp(ChangeNotifierProvider(
create: (context) => MySchedule(),
child: HomePage(),
));
// TODO extract HomePage to a new file via a shortcut
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Center(
child: Text('Change Piechart Dynamically'),
),
),
body: Column(
children: <Widget>[
Expanded(
child: MyChart(),
),
Expanded(
child: MySlider(),
)
],
),
),
);
}
}
2
Answers
You propably extracted the widgets from your Widget Tree into extra Classes by using the extract widget function.
While I haven’t found a shortcut to extract the already made class to a new file, by installing the ‘Flutter Enhancement Suite’ plugin by Marius Höfler you will get a shortcut to extract the widget into a new file directly:
CTRL
+ALT
+E
The New Shortcut Key to Extract Widget In Android Studio Chipmunk | 2021.2.1 is Ctrl + Alt + E