skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. The New Shortcut Key to Extract Widget In Android Studio Chipmunk | 2021.2.1 is Ctrl + Alt + E

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search