解決したい課題
BottomNavigationBarのコード(tab_bar.dart)のタブ画面にカウンタアプリコード(main.dart)を呼び出したいです。
実行環境
デフォルトのカウンタアプリと公式ページのBottomNavigationBarのコードを使ってます。
コード
デフォルトのカウンタアプリ
maindart
import 'package:flutter/material.dart'; void main() { runApp(CountApp()); } class CountApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'CountApp', theme: ThemeData( 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), ), 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. ); } }
####公式ページのBottomNavigationBarのコード
tab_bar.dart
import 'package:flutter/material.dart'; import 'main.dart'; void main() => runApp(MyApp()); /// This is the main application widget. class MyApp extends StatelessWidget { static const String _title = 'Flutter Code Sample'; @override Widget build(BuildContext context) { return MaterialApp( title: _title, home: MyStatefulWidget(), ); } } /// This is the stateful widget that the main application instantiates. class MyStatefulWidget extends StatefulWidget { MyStatefulWidget({Key key}) : super(key: key); @override _MyStatefulWidgetState createState() => _MyStatefulWidgetState(); } /// This is the private State class that goes with MyStatefulWidget. class _MyStatefulWidgetState extends State<MyStatefulWidget> { int _selectedIndex = 0; static const List<Widget> _widgetOptions = <Widget>[ CountApp(), Text( 'Index 1: Business', ), Text( 'Index 2: School', ), ]; void _onItemTapped(int index) { setState(() { _selectedIndex = index; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('BottomNavigationBar Sample'), ), body: Center( child: _widgetOptions.elementAt(_selectedIndex), ), bottomNavigationBar: BottomNavigationBar( items: const <BottomNavigationBarItem>[ BottomNavigationBarItem( icon: Icon(Icons.home), label: 'Home', ), BottomNavigationBarItem( icon: Icon(Icons.business), label: 'Business', ), BottomNavigationBarItem( icon: Icon(Icons.school), label: 'School', ), ], currentIndex: _selectedIndex, selectedItemColor: Colors.amber[800], onTap: _onItemTapped, ), ); } }
現状
実行コマンドを押してもエラーは表示されませんが、__tab_bar.dart__の下記コードに赤線が表示されています。
CountApp(),
エミュレータの画面はデフォルトのカウンタアプリが表示されています。
クラスの呼び出し記述に間違いがありそうなのですが、どのように記述方法を修正すればいいのかわからず。。。
どなたかアドバイスいただけますと幸いです。
よろしくお願いいたします。
追記
import 'package:flutter/material.dart'; void main() { runApp(CountApp()); } class CountApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'CountApp', theme: ThemeData( visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } // ignore: must_be_immutable class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); String title; @override MyHomePageState createState() => MyHomePageState(); } class MyHomePageState extends State<MyHomePage> { int _selectedIndex = 0; static List<Widget> _widgetOptions = <Widget>[ ChangeClass(), Text( 'Index 1: Business', ), Text( 'Index 2: School', ), ]; void _onItemTapped(int index) { setState(() { _selectedIndex = index; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('BottomNavigationBar Sample'), ), body: Center( child: _widgetOptions.elementAt(_selectedIndex), ), bottomNavigationBar: BottomNavigationBar( items: const <BottomNavigationBarItem>[ BottomNavigationBarItem( icon: Icon(Icons.home), label: 'Home', ), BottomNavigationBarItem( icon: Icon(Icons.business), label: 'Business', ), BottomNavigationBarItem( icon: Icon(Icons.school), label: 'School', ), ], currentIndex: _selectedIndex, selectedItemColor: Colors.amber[800], onTap: _onItemTapped, ), ); } } class ChangeClass extends StatefulWidget { ChangeClassPage createState() => ChangeClassPage(); } class ChangeClassPage extends State<ChangeClass> { int _counter = 0; void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('narururu'), ), 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. ); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/28 10:14 編集
2021/01/28 15:07
2021/01/29 10:29 編集
2021/01/30 06:51
2021/01/30 07:02