본문 바로가기
Dead Code/Flutter_Dart

[플러터] 연습장. 첫앱.

by JO_i 2019. 4. 12.

 

 

#1. Navigator, 첫번째 페이지로 pop하기

Navigator.popUntil(context, ModalRoute.withName(Navigator.defaultRouteName));

 

#2. 리스트 동적으로 생성하기

#3. 리스트 클릭 리스너 만들기

 


 

[main.dart]

더보기

import 'package:flutter/material.dart';

import 'package:flutter/cupertino.dart';

 

import './body_list.dart';

import './body_navigator.dart';

 

void main() => runApp(MyApp());

 

class MyApp extends StatelessWidget {

final String txt1 = 'firstText';

final String txt2 = 'SecondText';

final List _myList = [];

 

@override

Widget build(BuildContext context) {

return MaterialApp(

// initialRoute: '/',

routes: {

// '/': (context) => BodyList(txt1, txt2, _myList),

'/p1': (context) => BodyNavigatorPage1(),

'/p2': (context) => BodyNavigatorPage2(),

'/p3': (context) => BodyNavigatorPage3(),

'/main': (context) => MyApp(),

},

 

home: Scaffold(

bottomNavigationBar: BnbNavigation(1),

appBar: CupertinoNavigationBar(

middle: Text('titleBar'),

),

body: BodyList(txt1, txt2, _myList),

),

);

}

}

 

class BnbNavigation extends StatefulWidget {

final int myIndex;

BnbNavigation(this.myIndex);

 

@override

_BnbNavigationState createState() => _BnbNavigationState();

}

 

class _BnbNavigationState extends State<BnbNavigation> {

@override

Widget build(BuildContext context) {

return BottomNavigationBar(

items: <BottomNavigationBarItem>[

BottomNavigationBarItem(title: Text('Page1'), icon: Icon(Icons.alarm)),

BottomNavigationBarItem(title: Text('Page2'), icon: Icon(Icons.alarm)),

BottomNavigationBarItem(title: Text('Page3'), icon: Icon(Icons.alarm)),

],

onTap: bnbclick,

currentIndex: widget.myIndex,

);

}

 

bnbclick(int index) {

String _position = '';

 

setState(() {

print('BNB clicked $index');

 

if (index == 0) {

_position = '/p1';

} else if (index == 1) {

_position = '/p2';

} else {

_position = '/p3';

}

 

Navigator.pushNamed(context, _position);

});

}

}

 

[body_navigator.dart]

더보기

import 'package:flutter/material.dart';

 

import './main.dart';

 

class BodyNavigatorPage1 extends StatefulWidget {

@override

_BodyNavigatorPage1State createState() => _BodyNavigatorPage1State();

}

 

class _BodyNavigatorPage1State extends State<BodyNavigatorPage1> {

@override

Widget build(BuildContext context) {

return Scaffold(

bottomNavigationBar: BnbNavigation(0),

appBar: AppBar(

title: Text('Page1_titleBar'),

actions: <Widget>[

IconButton(

icon: Icon(

Icons.home,

color: Colors.red,

),

onPressed: () {

Navigator.popUntil(

context, ModalRoute.withName(Navigator.defaultRouteName));

},

)

],

),

);

}

}

 

class BodyNavigatorPage2 extends StatefulWidget {

@override

_BodyNavigatorPage2State createState() => _BodyNavigatorPage2State();

}

 

class _BodyNavigatorPage2State extends State<BodyNavigatorPage2> {

@override

Widget build(BuildContext context) {

return Scaffold(

bottomNavigationBar: BnbNavigation(1),

appBar: AppBar(

title: Text('Page2_titleBar'),

actions: <Widget>[

IconButton(

icon: Icon(

Icons.home,

color: Colors.red,

),

onPressed: () {

Navigator.popUntil(

context, ModalRoute.withName(Navigator.defaultRouteName));

},

)

],

),

);

}

}

 

class BodyNavigatorPage3 extends StatefulWidget {

@override

_BodyNavigatorPage3State createState() => _BodyNavigatorPage3State();

}

 

class _BodyNavigatorPage3State extends State<BodyNavigatorPage3> {

Widget build(BuildContext context) {

return Scaffold(

bottomNavigationBar: BnbNavigation(2),

appBar: AppBar(

title: Text('Page3_titleBar'),

actions: <Widget>[

IconButton(

icon: Icon(

Icons.home,

color: Colors.red,

),

onPressed: () {

Navigator.popUntil(

context, ModalRoute.withName(Navigator.defaultRouteName));

},

)

],

),

);

}

}

 

[body_List.dart]

더보기

import 'package:flutter/material.dart';

 

class BodyList extends StatefulWidget {

final String txt1;

final String txt2;

final List meList;

BodyList(this.txt1, this.txt2, this.meList);

 

@override

_BodyListState createState() => _BodyListState();

}

 

class _BodyListState extends State<BodyList> {

String _textViewText = '';

 

@override

Widget build(BuildContext context) {

return Column(

crossAxisAlignment: CrossAxisAlignment.center,

children: <Widget>[

SizedBox(

height: 24,

),

FlatButton(

child: ClipOval(

clipBehavior: Clip.antiAlias,

child: Image.asset(

'./images/down.jpg',

)),

onPressed: () {

setState(() {

widget.meList.clear();

_textViewText = '';

});

},

),

Text(

_textViewText,

textScaleFactor: 0.8,

textAlign: TextAlign.center,

),

RaisedButton(

child: Text('Adding List'),

onPressed: () {

setState(() {

widget.meList.add(widget.txt1 + '&' + widget.txt2);

print(widget.meList.toString());

});

},

),

Divider(),

Expanded(

child: ListView.builder(

itemCount: widget.meList.length,

itemBuilder: (BuildContext context, index) {

return ListTile(

// dense: true,

trailing: Icon(Icons.cake),

subtitle: Text('${widget.meList[index]}'),

leading: CircleAvatar(

radius: 24.0,

child: Text(

'Item ${index + 1}',

textScaleFactor: 0.8,

),

),

title: Text('item index : $index'),

onTap: () {

setState(() {

_textViewText = 'You were clicked : item index : $index';

});

},

);

},

),

),

],

);

}

}