Skip to content

Commit 1086056

Browse files
kenvandineSergio Schvezov
andauthored
flutter v1 plugin: run pub get during build (#3204)
Spread test was also enhanced to have actual dependencies and to pull from a source-subdir. Co-authored-by: Sergio Schvezov <sergio.schvezov@canonical.com>
1 parent b795baa commit 1086056

20 files changed

Lines changed: 558 additions & 187 deletions

File tree

snapcraft/internal/project_loader/_extensions/_flutter_meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def __init__(self, *, extension_name: str, yaml_data: Dict[str, Any]) -> None:
134134
"layout": {
135135
"/usr/share/xml/iso-codes": {
136136
"bind": "$SNAP/gnome-platform/usr/share/xml/iso-codes"
137-
},
137+
}
138138
},
139139
}
140140

snapcraft/plugins/v1/flutter.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ def schema(cls) -> Dict[str, Any]:
5353
"type": "string",
5454
"default": "lib/main.dart",
5555
}
56-
schema["properties"]["flutter-revision"] = {
57-
"type": "string",
58-
"default": None,
59-
}
56+
schema["properties"]["flutter-revision"] = {"type": "string", "default": None}
6057
schema["required"] = ["source"]
6158

6259
return schema
@@ -97,11 +94,12 @@ def pull(self) -> None:
9794
check=True,
9895
cwd=work_path,
9996
)
100-
subprocess.run(["flutter", "pub", "get"], check=True, cwd=work_path)
10197

10298
def build(self) -> None:
10399
super().build()
104100

101+
self.run(["flutter", "pub", "get"])
102+
105103
self.run(
106104
[
107105
"flutter",
@@ -111,7 +109,7 @@ def build(self) -> None:
111109
"-v",
112110
"-t",
113111
self.options.flutter_target,
114-
],
112+
]
115113
)
116114

117115
bundle_dir_path = pathlib.Path(self.builddir) / "build/linux/release/bundle"

tests/spread/plugins/v1/flutter/snaps/flutter-hello/lib/main.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
import 'package:flutter/material.dart';
2+
import 'package:url_launcher/url_launcher.dart';
3+
4+
final _linkStyle = TextStyle(
5+
color: Colors.lightBlue[100],
6+
fontStyle: FontStyle.italic,
7+
fontSize: 18.0,
8+
fontWeight: FontWeight.bold,
9+
decoration: TextDecoration.underline,
10+
);
11+
12+
Future<void> _maybeLaunch(String url) async {
13+
if (await canLaunch(url))
14+
await launch(
15+
url,
16+
forceWebView: true,
17+
enableJavaScript: true,
18+
);
19+
}
220

321
void main() {
422
runApp(MyApp());
@@ -104,6 +122,10 @@ class _MyHomePageState extends State<MyHomePage> {
104122
'$_counter',
105123
style: Theme.of(context).textTheme.headline4,
106124
),
125+
InkWell(
126+
child: Text("https://snapcraft.io", style: _linkStyle),
127+
onTap: () => _maybeLaunch("https://snapcraft.io"),
128+
),
107129
],
108130
),
109131
),

tests/spread/plugins/v1/flutter/snaps/flutter-hello/pubspec.lock

Lines changed: 0 additions & 153 deletions
This file was deleted.

tests/spread/plugins/v1/flutter/snaps/flutter-hello/pubspec.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ dependencies:
2424
flutter:
2525
sdk: flutter
2626

27-
2827
# The following adds the Cupertino Icons font to your application.
2928
# Use with the CupertinoIcons class for iOS style icons.
3029
cupertino_icons: ^0.1.3
3130

31+
url_launcher: 5.4.11
32+
33+
url_launcher_fde:
34+
git:
35+
url: git://github.com/google/flutter-desktop-embedding.git
36+
path: plugins/flutter_plugins/url_launcher_fde
37+
ref: b0794faf2c000576515aee56ca6bb5bee64cece4
38+
3239
dev_dependencies:
3340
flutter_test:
3441
sdk: flutter

tests/spread/plugins/v1/flutter/snaps/flutter-hello/snap/snapcraft.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ apps:
1616
parts:
1717
flutter-hello:
1818
source: .
19+
source-subdir: src
1920
plugin: flutter
2021
flutter-revision: 3ce26910923b6a5e784df17a8a9a17b50598df4a
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:url_launcher/url_launcher.dart';
3+
4+
final _linkStyle = TextStyle(
5+
color: Colors.lightBlue[100],
6+
fontStyle: FontStyle.italic,
7+
fontSize: 18.0,
8+
fontWeight: FontWeight.bold,
9+
decoration: TextDecoration.underline,
10+
);
11+
12+
Future<void> _maybeLaunch(String url) async {
13+
if (await canLaunch(url))
14+
await launch(
15+
url,
16+
forceWebView: true,
17+
enableJavaScript: true,
18+
);
19+
}
20+
21+
void main() {
22+
runApp(MyApp());
23+
}
24+
25+
class MyApp extends StatelessWidget {
26+
// This widget is the root of your application.
27+
@override
28+
Widget build(BuildContext context) {
29+
return MaterialApp(
30+
title: 'Super Cool App',
31+
theme: ThemeData(
32+
// This is the theme of your application.
33+
//
34+
// Try running your application with "flutter run". You'll see the
35+
// application has a blue toolbar. Then, without quitting the app, try
36+
// changing the primarySwatch below to Colors.green and then invoke
37+
// "hot reload" (press "r" in the console where you ran "flutter run",
38+
// or simply save your changes to "hot reload" in a Flutter IDE).
39+
// Notice that the counter didn't reset back to zero; the application
40+
// is not restarted.
41+
primarySwatch: Colors.blue,
42+
// This makes the visual density adapt to the platform that you run
43+
// the app on. For desktop platforms, the controls will be smaller and
44+
// closer together (more dense) than on mobile platforms.
45+
visualDensity: VisualDensity.adaptivePlatformDensity,
46+
),
47+
home: MyHomePage(title: 'Super Cool App'),
48+
);
49+
}
50+
}
51+
52+
class MyHomePage extends StatefulWidget {
53+
MyHomePage({Key key, this.title}) : super(key: key);
54+
55+
// This widget is the home page of your application. It is stateful, meaning
56+
// that it has a State object (defined below) that contains fields that affect
57+
// how it looks.
58+
59+
// This class is the configuration for the state. It holds the values (in this
60+
// case the title) provided by the parent (in this case the App widget) and
61+
// used by the build method of the State. Fields in a Widget subclass are
62+
// always marked "final".
63+
64+
final String title;
65+
66+
@override
67+
_MyHomePageState createState() => _MyHomePageState();
68+
}
69+
70+
class _MyHomePageState extends State<MyHomePage> {
71+
int _counter = 0;
72+
73+
void _incrementCounter() {
74+
setState(() {
75+
// This call to setState tells the Flutter framework that something has
76+
// changed in this State, which causes it to rerun the build method below
77+
// so that the display can reflect the updated values. If we changed
78+
// _counter without calling setState(), then the build method would not be
79+
// called again, and so nothing would appear to happen.
80+
_counter++;
81+
});
82+
}
83+
84+
@override
85+
Widget build(BuildContext context) {
86+
// This method is rerun every time setState is called, for instance as done
87+
// by the _incrementCounter method above.
88+
//
89+
// The Flutter framework has been optimized to make rerunning build methods
90+
// fast, so that you can just rebuild anything that needs updating rather
91+
// than having to individually change instances of widgets.
92+
return Scaffold(
93+
appBar: AppBar(
94+
// Here we take the value from the MyHomePage object that was created by
95+
// the App.build method, and use it to set our appbar title.
96+
title: Text(widget.title),
97+
),
98+
body: Center(
99+
// Center is a layout widget. It takes a single child and positions it
100+
// in the middle of the parent.
101+
child: Column(
102+
// Column is also a layout widget. It takes a list of children and
103+
// arranges them vertically. By default, it sizes itself to fit its
104+
// children horizontally, and tries to be as tall as its parent.
105+
//
106+
// Invoke "debug painting" (press "p" in the console, choose the
107+
// "Toggle Debug Paint" action from the Flutter Inspector in Android
108+
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
109+
// to see the wireframe for each widget.
110+
//
111+
// Column has various properties to control how it sizes itself and
112+
// how it positions its children. Here we use mainAxisAlignment to
113+
// center the children vertically; the main axis here is the vertical
114+
// axis because Columns are vertical (the cross axis would be
115+
// horizontal).
116+
mainAxisAlignment: MainAxisAlignment.center,
117+
children: <Widget>[
118+
Text(
119+
'You have pushed the button this many times:',
120+
),
121+
Text(
122+
'$_counter',
123+
style: Theme.of(context).textTheme.headline4,
124+
),
125+
InkWell(
126+
child: Text("https://snapcraft.io", style: _linkStyle),
127+
onTap: () => _maybeLaunch("https://snapcraft.io"),
128+
),
129+
],
130+
),
131+
),
132+
floatingActionButton: FloatingActionButton(
133+
onPressed: _incrementCounter,
134+
tooltip: 'Increment',
135+
child: Icon(Icons.add),
136+
), // This trailing comma makes auto-formatting nicer for build methods.
137+
);
138+
}
139+
}

0 commit comments

Comments
 (0)