今回の記事はFlutterを用いてアプリを作成する際に、iconマーク(カメラやWifiなど)を動的に変更する手順をご紹介しています。
Flutterでiconマークの表示
基本的にはFlutterの基本プロジェクトでアプリバーにiconを表示させてみましょう。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: [
IconButton(
icon: Icon(Icons.flag),
color:Colors.white,
onPressed: () {},
),
]
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
これでアプリバーにフラッグを表示することができました。
Flutterで動的にiconマークを変更する
では本題のiconを動的に変更するコードを公開します。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
bool iconchange = false;
void _incrementCounter() {
if (_counter/2 == 0) {
iconchange = true;
} else if (_counter/2 == 1) {
iconchange = false;
}
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: [
if (iconchange==true)
IconButton(
icon: Icon(Icons.flag),
color:Colors.white,
onPressed: () {},
),
if (iconchange==false)
IconButton(
icon: Icon(Icons.flag_outlined),
color:Colors.red,
onPressed: () {},
),
]
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
少し解説を行っておきます。
...(省略)...
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
bool iconchange = false;
void _incrementCounter() {
if (_counter/2 == 0) {
iconchange = true;
} else if (_counter/2 == 1) {
iconchange = false;
}
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: [
if (iconchange==true)
IconButton(
icon: Icon(Icons.flag),
color:Colors.white,
onPressed: () {},
),
if (iconchange==false)
IconButton(
icon: Icon(Icons.flag_outlined),
color:Colors.red,
onPressed: () {},
),
]
),
body: Center(
...(省略)...
基本的には上記の部分の関数でBool値のFlagを変化させて、表示のところを変更しております。
Wiget内のActionに変化するif文などを入れ込めば良いと思います。
今回の記事は以上です。他にもFlutterの記事をいくつか記載していますので気になる方は是非参照してください。
コメント