はじめに
flutter doctorでエラーの解消
- 久しぶりに動かそうとしたら、flutter doctorでエラーになった
C:\Users\user>flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.19043.1415], locale ja-JP)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
X cmdline-tools component is missing
Run `path/to/sdkmanager --install "cmdline-tools;latest"`
See https://developer.android.com/studio/command-line for more details.
X Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[√] Chrome - develop for the web
[√] Android Studio (version 2020.3)
[√] VS Code (version 1.62.3)
[√] Connected device (2 available)
! Doctor found issues in 1 category.
cmdline-tools component is missingエラー
- 下記の赤枠の箇所にチェックを入れて Applyボタンをクリックすることで解消できました。
Android license status unknown.エラー
- 下記のコマンドを実行するとことでエラーを解消することが出来ました。
flutter doctor --android-licenses
画面遷移するサンプル
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:winlogic/next_page.dart';
// よいサンプル
// https://github.com/kenta-wakasa/riverpod_sample
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'トップページ'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String _text ="";
void _incrementCounter() {
setState(() {
_counter++;
});
}
void _changeText(input) {
print(input);
setState(() {
_text = input;
});
}
final ButtonStyle style =
ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20));
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: <Widget>[Icon(Icons.add), Icon(Icons.share)],
),
body: Container(
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
//Image.network('https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg'),
Image.asset('images/sample.jpg', height: 200),
Icon(Icons.holiday_village,size:30),
Text(_text),
Text("テストテスト",
style: TextStyle(
fontSize: 40,
color: Colors.green,
fontWeight: FontWeight.w900,
fontStyle: FontStyle.italic,
)
),
Text("サンプルテキスト"),
ElevatedButton(
child: Text("次へ"),
onPressed: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => NextPage('1ページ目から渡すパラメータ'))
);
_text = result;
_changeText(result);
print(_text);
}
)
],
)
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
import 'package:flutter/material.dart';
class NextPage extends StatelessWidget {
NextPage(this.name);
final String name;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('2ページ目'),
actions: <Widget>[Icon(Icons.add), Icon(Icons.share)],
),
body: Container(
height: double.infinity,
color: Colors.greenAccent,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(name),
Center(
child: ElevatedButton(
child: Text('戻る'),
onPressed: () {
Navigator.pop(context, "2ページ目から渡すパラメータ");
}
)
),
]
)
)
);
}
}
- pubspec.yamlに下記の記述をしないとassetで画像を表示することが出来ない
flutter:
assets:
- images/sample.jpg